Unity3d中UnityEngine.Object

來源:互聯網
上載者:User

標籤:min   href   list   ngui   object類   函數   add   存在   nal   

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); // falseObject 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.

Unity3d中UnityEngine.Object

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.