代码之家  ›  专栏  ›  技术社区  ›  rootpanthera

使用接口来利用基本的统一方法[[关闭]

  •  0
  • rootpanthera  · 技术社区  · 6 年前

    我的想法是删除那些仅仅因为我需要执行一次性任务而存在的单行为脚本,比如在它们的“Start”方法中分配一个事件或者初始化一个对象,。。

    下面是我的完整示例:

    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();
    
        }
    }
    

    关于这种方法的想法?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Ruzihm aks786    6 年前

    你需要确保 Awake() 之前不能调用 list[0..n-1] 都被定义了,或者包含了一些能正确处理那个案子的东西

    一旦处理好了,就应该如您所期望的那样工作(时间方面)。

    OnDestroy OnDisable 方法。