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

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

原文地址:https://www.cnblogs.com/chongxin/p/4119627.html