Unity奇葩现象汇总

来源:互联网 发布:淘宝的排行榜在哪里 编辑:程序博客网 时间:2024/06/06 03:54

接口的使用

大伙先看下面的代码

public interface Interface  {    void Println();}public class TestInstance : MonoBehaviour,Interface {    private float num = 4;    // Use this for initialization    void Start () {        num = 5;    }    private void OnDestroy()    {        Debug.LogWarning ("Im destroyed");    }    // Update is called once per frame    void Update () {        num++;        Debug.Log ("Im still alive");    }    public void Println()    {        Debug.Log ("ddddddddddddddddddddddddddddddddd" + num.ToString());        Update ();    }}

如果把挂TestInstance脚本的gameobject销毁掉,Interface 对象是否为空呢?
结果是这样:调用toString()方法后,显示为null。但是,调用该该接口的Println()方法,却没有报错,并正常打印了。有时间大伙可以试一下。
现在判断接口是否为空,都应该这样判断了

staic public bool IsNull(object obj){    return obj == null || obj.ToString().ToLower()=="null";}

协同的使用

再看下面的代码

public class c1 :MonoBehaviour{    private void Start()    {        StartCoroutine(Test());    }    private Ienumator Test()    {        while(true)        {            Debug.Log("---------------------")            yield return null;        }    }}

将脚本c1挂在GameObject上,运行游戏
将脚本c1disable掉,发现log一直还在打。
如果将相关的GameObject,disable掉,发现log就没有了。重新enable,log也不会再有。
由此说明,协同程序不是靠相关的脚本驱动的,而是靠gameobject驱动的。