zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    Java之JVM调优案例分析与实战(4)
    Qt浅谈之四十九俄罗斯方块(代码来自网络)
    自作聪明的开发
    Visual Studio 连接 SQL Server 的connectionStringz和
    删除反复行SQL举例
    一起学android之怎样设置TextView中不同字段的字体颜色(22)
    A008-drawable资源
    android 自己定义组件随着手指自己主动画圆
    一个简单的HTML5摇一摇实例
    关于事件的传递机制。
  • 原文地址:https://www.cnblogs.com/kanekiken/p/7533513.html
Copyright © 2011-2022 走看看