首页 > 编程语言 > 详细

Unity3d - 初学篇 Event Functions 的 继承 机制

时间:2014-11-24 22:09:14      阅读:313      评论:0      收藏:0      [点我收藏+]

 

我们知道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 ");
    }
}

 

运行:

bubuko.com,布布扣

 

测试二:

那么 如果把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 ");
    //}
}

运行:

bubuko.com,布布扣

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

bubuko.com,布布扣

 

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

Unity3d - 初学篇 Event Functions 的 继承 机制

原文:http://www.cnblogs.com/chongxin/p/4119627.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!