c# - 使用接口来利用基本的 Unity 方法?
问题描述
我的想法是删除仅因为我需要执行一次性任务而存在的 Monobehavior 脚本,例如在他们的“开始”方法中分配一个事件或初始化一个对象,..
可以使用基本方法(Awake、Start、..)创建接口并从我唯一的 Monobehavior 中调用所有对象吗?
这是我的完整示例:
public interface IMain {
void OnMyAwake();
void OnMyStart();
void OnMyUpdate();
}
private void Awake()
{
Debug.Log("I'm awake");
for (int x = 0; x < list.Length; x++) {
list[x].OnMyAwake();
}
}
// Use this for initialization
void Start () {
Debug.Log("I'm Start");
for (int x = 0; x < list.Length; x++)
{
list[x].OnMyStart();
}
}
// Update is called once per frame
void Update () {
for (int x = 0; x < list.Length; x++)
{
list[x].OnMyUpdate();
}
}
对这种方法的想法?
解决方案
您需要确保在所有定义Awake()
之前不能调用,或者包含一些东西来正确处理这种情况list[0..n-1]
一旦处理好,这应该可以按您的预期工作(时间方面)。
您可能还想包含OnDestroy
和OnDisable
方法。
推荐阅读
- python - 使用 h5py 打开和写入 h5 文件时,是否清理了现有组或数据集?
- ios - 无法加载捆绑包“MyProjectUITests”,因为它已损坏或缺少必要的资源。尝试重新安装捆绑包
- django - 从默认用户模型 django 更新字段
- meteor - 通过铁路由器、流星将对象从一个模板发送到另一个模板
- javascript - 如何为 Redux ORM 构建模型
- r - 每行带有格式表的动态百分比条
- c# - Entity Framework Core 中主键中的 Guid 字符串
- asp.net-web-api - 从 Autofac AsWebApiAuthenticationFilterFor 中排除特定方法
- java - 安全停止线程
- operator-overloading - Eclipse Xtend:覆盖 equals() 与 operator_equals()