zoukankan      html  css  js  c++  java
  • Unity3d

    我们知道Start() Update() 等之类的 事件函数 在Unity 主线程中是依次调用的。至于调用的顺序可以查手册。

    由此继承机制也会发生一些改变。

    测试一:

    public class MyTest2 : MonoBehaviour
    {
    
        void Start () {
            //EventDelegate.Add(tween.onFinished, Test, true);
            Debug.Log(" MyTest2 Start ");
        }
    
        void Update()
        {
            Debug.Log(" MyTest2 Update ");
        }
    }
    public class MyTest3 : MyTest2
    {
    
        // Use this for initialization
        void Start()
        {
            Debug.Log(" MyTest3 Start ");
        }
        
        void Update()
        {
            Debug.Log(" MyTest3 Update ");
        }
    }

    运行:

    测试二:

    那么 如果把MyTest3 的Start Update 注释掉会怎样了。

    public class MyTest2 : MonoBehaviour
    {
    
        void Start () {
            //EventDelegate.Add(tween.onFinished, Test, true);
            Debug.Log(" MyTest2 Start ");
        }
    
        void Update()
        {
            Debug.Log(" MyTest2 Update ");
        }
    }
    
    public class MyTest3 : MyTest2
    {
    
        // Use this for initialization
        //void Start()
        //{
        //    Debug.Log(" MyTest3 Start ");
        //}
        
        //void Update()
        //{
        //    Debug.Log(" MyTest3 Update ");
        //}
    }

    运行:

    熟悉c# 的 都知道,不加有修饰符 默认是 private 的。看来 unity 对这类 函数 应该是 特殊 处理了。

    测试三:

    既然这样 我们再看看 别的继承特性。

    public class MyTest2 : MonoBehaviour
    {
        //virtual or abstract members cannot be private
        public virtual void Start () {
            //EventDelegate.Add(tween.onFinished, Test, true);
            Debug.Log(" MyTest2 Start ");
        }
    
        void Update()
        {
            Debug.Log(" MyTest2 Update ");
        }
    }
    public class MyTest3 : MyTest2
    {
    
        // Use this for initialization
        public override void Start()
        {
            Debug.Log(" MyTest3 Start ");
        }
    
        void Update()
        {
            Debug.Log(" MyTest3 Update ");
        }
    }

    结果看来和测试一是一样的。也就是说先会找最终实例化的有没有,然后再找父类有没有。

  • 相关阅读:
    MyGeneration的NHibernate代码生成模版
    ASP.NET页面控制回车触发按钮
    操作NHibernate进行多事务并发处理的一些小经验
    mysql之sql_mode =only_full_group_by 设置问题
    1、一维数组排序
    使用正则表达式构造定制的HTML5输入框
    JavaScript加密库CryptoJS的使用
    安全密码存储,该怎么做,不该怎么做?
    google 站内搜索
    导入导出xls数据
  • 原文地址:https://www.cnblogs.com/chongxin/p/4119627.html
Copyright © 2011-2022 走看看