我有一个想法,那就是必须有一种将类附加到预制件的方法--基本上是将我自己的方法添加到代码中。然后,当我在运行时实例化我的预置文件时,我希望它是这个类的一个实例。
现在,我可以将一个脚本附加到预制件上,它可以开始和更新工作,但是我的想法是这样的:
public class PowerupWidget : MonoBehaviour {
void Start() {
Debug.Log("PowerupWidget created!!!");
}
void SetTitle(string title) {
// ... set the prefab's title widget text ...
}
}当我实例化预置文件时,会调用Start方法。但是实例当然是GameObject类型。
// in my scene script:
// prefab is my PowerupWidget
GameObject p = Instantiate(prefab, transform.position, Quaternion.identity, this.transform);
// here p is GameObject of course是否有一种方法可以访问PowerupWidget类的实例,或者我的假设完全错了?
发布于 2022-04-15 09:45:10
结构统一:
当实体是游戏对象,组件是附加到这个游戏对象的类(脚本)时,
你只需要这么做
PowerupWidget yourWidget = p.GetComponent<PowerupWidget>();https://stackoverflow.com/questions/71881269
复制相似问题