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

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

  • 相关阅读:
    Centos7上安装docker
    docker部署mysql5.6.40
    centos7上部署spring boot并保存日志
    [转载]Ocelot简易教程(一)Ocelot是什么
    浅谈Surging服务引擎中的RabbitMQ组件和容器化部署
    [转载]Surging教学视频资源汇总
    [转载]netcore 使用surging框架发布到docker
    [转载]Surging 分布式微服务框架使用入门
    [转载]Surging Demo 项目之一
    [转载]剥析surging的架构思想
  • 原文地址:https://www.cnblogs.com/chongxin/p/4119627.html
Copyright © 2011-2022 走看看