• Unity3d中UnityEngine.Object


    UnityEngine.Object继承自system.Object,是Unity所涉及所有物体的基类。

    Static Functions 静态函数   下面的都是静态函数

    Destroy Removes a gameobject, component or asset.
    删除一个游戏对象、组件或资源
    DestroyImmediate Destroys the object obj immediately. You are strongly recommended to use Destroy instead.
    立即销毁物体obj,强烈建议使用Destroy代替。
    DontDestroyOnLoad Makes the object target not be destroyed automatically when loading a new scene.
    加载新场景的时候使目标对象不被自动销毁。
    FindObjectOfType Returns the first active loaded object of Type type.
    返回Type类型第一个激活的加载的对象。
    FindObjectsOfType Returns a list of all active loaded objects of Type type.
    返回Type类型的所有激活的加载的物体列表。
    Instantiate Clones the object original and returns the clone.
    克隆原始物体并返回克隆物体。

    先看一下Object重载的运算符:

    Operators
    bool Does the object exist?
    operator != Compares if two objects refer to a different object.
    operator == Compares if two objects refer to the same.

    下面几个注意点是我在最近使用发现的。

    (1)Object类重载了类型隐式转换运算符“bool”。这个如果不注意,在某些情况下可能会造成意料外的调用。

    class Test:MonoBehaviour
    {

    void Start()
    {
    Fun(transform);//此函数目的是为了调用 void Fun(system.Object objFlag)

    }

    void Fun(bool value)
    {
    Debug.Log(“call the bool param fun,the value is:”+value.ToString();)
    }

    void Fun(system.Object objFlag)
    {
    Debug.Log(“call the param system.Object fun,the value is:”+value.ToString();)
    }

    }

    打印的日志是:call the bool param fun,the value is: true

    可以看出,与我们的目的不一致。通过追溯Transform父类,其顶层基类是UnityEngine.Object。这就找到原因了,在这样的情况下这两个Fun函数存在调用歧义。transform先被判断是否存在的引用,然后调用了void Fun(bool value)

    建议:同一个类尽量不要使用同名函数,如有要使用同名函数时要特别注意这两种情况。

    (2)Object类重载了类型隐式转换运算符“==”。这个在与null比较时要特别注意,既使是有效的引用结果有可能是true的。

    例:

    GameObject go = new GameObject(); 
    Debug.Log (go == null); // false
    
    Object obj = new Object();
    Debug.Log (obj == null); // true

    看一下官方的说明(英文不好翻译不来):

    Instatiating a GameObject adds it to the scene so it’s completely initialized (!destroyed). Instantiating a simple UnityEngine.Object has no such semantics, so the it stays in the ‘destroyed’ state which compares true to null.

  • 相关阅读:
    谈谈团队文化
    ubifs性能优化分析
    ubifs总体设计分析
    分层网络模型(二)
    哎,老了之display-box
    http协议
    box-shadow,text-shadow
    nth-child,nth-last-child,after,before,tab-highlight-color,first-child,last-child
    转载之html特殊字符的html,js,css写法汇总
    一天学习一点之express demo
  • 原文地址:https://www.cnblogs.com/kanekiken/p/7533513.html
Copyright © 2020-2023  润新知