Object類
Unity目前有三個命名空間,UnityEngine、UnityEditor、Unity,當然還有一個Other是做補充說明的,Object類是一個基類,可以查閱本教程第一篇文檔的圖片,做直觀瞭解,下面詳細解析。
1. 官方定義
class in UnityEngine/Implemented in:UnityEngine.CoreModule
屬於UnityEngine命名空間下的核心模塊類
2. 官方描述
Base class for all objects Unity can reference.
Any public variable you make that derives from Object gets shown in the inspector as a drop target,allowing you to set the value from the GUI. UnityEngine.Object is the base class of all built-in Unity objects.
Although Object is a class it is not intended to be used widely in script. However as an exampleObject is used in the Resources class. See Resources.LoadAll which has [[Object[]]] as areturn.
This class doesn’t support the null-conditional operator (?.) and the null-coalescing operator (??).
上述說明Unity 可以引用所有對象的基類。
Object類的派生類定義的公共(public)變量可以在inspector面板中顯示出來,並且設置相關的值,UnityEngine.object 是所有內置 Unity 對象的基類。
Object類不推薦在腳本中廣泛使用,但可作爲示例對象在Resources類中使用,實際開發過程中是基本不會直接用的。在Resources類中使用的情況,我們後期根據Resources.LoadAll文檔進行說明,此處不做贅述。
Object類不支持 null 條件運算符 (?. ) 和空合併運算符 (?? )
3. 屬性
hideFlags:Should the object be hidden, saved with the Scene or modifiable by the user?
hideFlag屬性用於對象隱藏,場景保存和修改,具體使用我們在HideFlags類中進行敘述,這裏也不做實例演示。
name:The name of the object.
語法: public string name;
該屬性十分簡單,直接賦值即可,演示如下:
using UnityEngine;
public class nameTest:MonoBehaviour
{
public GameObject go;
void Start()
{
go.name="icestring";
Debug.Log("The name is"+go.name);
}
}
4. 共有方法
GetInstanceID:Returns the instance id of the object.
語法:public int GetInstanceID();
Unity中每個物體都有自己的ID,可通過該方法獲取對象ID,演示如下:
using UnityEngine;
public class IDTest : MonoBehaviour
{
void Start()
{
GameObject go=new GameObject();
Debug.Log(go.GetInstanceID());
}
}
ToString:Returns the name of the object.
語法:public string ToString();
該方法將返回對象的名稱,演示如下:
using UnityEngine;
public class TSTest : MonoBehaviour
{
GameObject go=new GameObject();
void Start()
{
Debug.Log(go.ToString());
}
}
5. 靜態方法
Destroy:Removes a GameObject, component or asset.
語法:public static void Destroy(Object obj,float t = 0.0F);
刪除遊戲對象、組件或資源。有兩個形參,一個對象一個時間,時間爲可選項。對象在當前 Update 循環後下一幀渲染之前立即銷燬, 如果指定時間,則從現在開始幾秒鐘銷燬該對象。如果是組件,此方法將從GameObject中刪除組件並銷燬它。演示如下:
using UnityEngine;
public class DTest: MonoBehaviour
{
GameObject go=new GameObject();
void Update()
{
Destroy(go,2.0f);
}
}
DestroyImmediate:Destroys the object obj immediately. You are strongly recommended to use Destroy instead.
語法:public static void DestroyImmediate(Object obj,bool allowDestroyingAssets = false);
此方法可以立即銷燬對象,含有兩個參數,對象和一個bool值,bool值決定是否立即銷燬,該方法風險很高,可能會將資源刪除,需謹慎使用。演示如下:
using UnityEngine;
public class DITest : MonoBehaviour
{
GameObject go=new GameObject();
void Update()
{
DestroyImmediate(go,true);
}
}
DontDestroyOnLoad:Do not destroy the target Object when loading a new Scene.
語法:public static void DontDestroyOnLoad(Object target);
該方法允許在加載新場景時不銷燬一些物體,含有一個參數,指定該對象不被銷燬,因涉及到場景切換,會使用SceneManagement類,我們在SceneManagement類中再討論該方法,本篇不做代碼演示。
FindObjectOfType:Returns the first active loaded object of Type type.
語法:public static Object FindObjectOfType(Type type);
該方法通過類型查找單個物體,只含有一個參數,用於返回類型,演示如下。
using UnityEngine;
using System.Collections;
public class FGTest : MonoBehaviour
{
void Start()
{
Camera cam = (Camera)FindObjectOfType(typeof(Camera));
if (cam)
Debug.Log("Camera object found: " + cam.name);
else
Debug.Log("No Camera object could be found");
}
}
FindObjectsOfType:Returns a list of all active loaded objects of Type type.
語法:public static T[] FindObjectsOfType();
public static Object[] FindObjectsOfType(Type type);
該方法有兩種寫法,通過類型查找一組物體,返回的是一個數組,可以是一個泛型數組或者是Object類數組,演示如下:
using UnityEngine;
public class FGSTest : MonoBehaviour
{
private const int count = 10;
void Start()
{
var gameObjects = new GameObject[count];
var expectedTextMeshObjects = new TextMesh[count];
var expectedCanvasObjects = new CanvasRenderer[count];
for (var i = 0; i < count; ++i)
{
gameObjects[i] = new GameObject();
expectedTextMeshObjects[i] = gameObjects[i].AddComponent<TextMesh>();
expectedCanvasObjects[i] = gameObjects[i].AddComponent<CanvasRenderer>();
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
var foundCanvasObjects = FindObjectsOfType<CanvasRenderer>();
var foundTextMeshObjects = FindObjectsOfType(typeof(TextMesh));
Debug.Log(foundCanvasObjects + " : " + foundCanvasObjects.Length);
Debug.Log(foundTextMeshObjects + " : " + foundTextMeshObjects.Length);
}
}
}
以上代碼講述了,先定義三個容量爲10的不同類型數組,然後在for循環裏gameObjects 實例化,expectedTextMeshObjects和expectedCanvasObjects獲取到對應組件,最後在Update函數裏通過ifxuanze結構進行尋找並通過Debug.Log方法進行打印,沒有輸出gameObjects的原因是組件時掛載在gameObjects上的,控制檯打印時也應用了兩種類型的寫法。
Instantiate:Clones the object original and returns the clone.
語法:
public static Object Instantiate(Object original);
public static Object Instantiate(Object original,Transform parent);
public static Object Instantiate(Object original,Transform parent,bool instantiateInWorldSpace);
public static Object Instantiate(Object original,Vector3 position,Quaternion rotation);
public static Object Instantiate(Object original,Vector3 position,Quaternion rotation,Transform parent);
Instantiate函數是unity3d中進行實例化的函數,也就是對一個對象進行復制操作的函數,這個函數共有五個重載(overloaded)函數,參數解釋如下:
original:源對象
position:位置座標
rotation:旋轉座標(旋轉四元數)
parent:實例化對象的父對象,就是給這個實例化對象找的爹,在完成實例化函數處理後,實例化對象將在父對象下,是父對象的子對象
instantiateWorldSpace:這個值爲True,表示實例化對象相對於世界座標系的位置(是位置,不是座標值,比如實例化前在Canvas左上角,實例化後還在左上角)不變,相對於父對象的座標值變了。爲false,表示實例化對象相對於父對象的座標值不變,但在世界座標系中的位置變了。
演示如下:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform prefab;
void Start()
{
for (int i = 0; i < 10; i++)
{
Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
}
}
}
因爲實例化的函數可以重載,在這裏只給出一種寫法,避免贅述,其實並不難,調用時根據實際需求更改函數的參數即可。
6. 運算符
bool:Does the object exist?
operator !=:Compares if two objects refer to a different object.
operator ==:Compares two object references to see if they refer to the same object.
以上三個運算符很簡單,分別是bool值,不等於和等於,用法上,bool表示判斷對象石佛營存在,!=表示比較兩個對象是否引用其他對象,==表示比較兩個對象引用以查看它們是否引用同一對象。
Object類的解析就到這裏,有些地方可能說的不是十分詳細,其實不用太過於糾結,知道方法和屬性是做什麼的,拿着用就可以了,如果C#語言基礎紮實的話,代碼應該都是看的懂的,有些代碼我參考了官方文檔,過於複雜的我會給解釋。