腳本操作對象

GameObject屬性
GameObject.activeSelf 是否活動
該屬性是一個只讀屬性我們可以通過打印日誌的方式看到運行的結果
GameObject.tag 標籤
該屬性是一個可讀可寫的屬性
如果要通過代碼對遊戲對象修改tag值,確保已經在標籤列表中添加過要修改的標籤
gameObject.tag = “Tag01”;
Debug.Log(gameObject.tag);

GameObject.layer 層
訪問或修改遊戲對象所在的層,該屬性也是可讀可寫。代碼操作方式可tag操作相同
GameObject.name名字
訪問或修改遊戲對象的名字
訪問遊戲對象的名字
Debug.Log(gameObject.name);
修改遊戲對象的名字 不用提前預設 在遊戲運行的時候會自行修改
gameObject.name = “BS”;
克隆對象
克隆遊戲對象從效率上講,克隆一個對象要比創建一個對象效率要高。常用於一些完全相同並且數量龐大的遊戲對象,比如發射的子彈對象,每一顆子彈對象是完全一樣的,每一次發射子彈都會克隆一個子彈對象,並且讓克隆的子彈對象完成自己的生命週期。
克隆對象:
public GameObject prefabObj;(克隆對象的模板)
GameObject 是Object的子類
對某個對象進行克隆(切記Unity之中要把模塊填進去)
GameObject table = GameObject.Instantiate(prefabObj);
table.transform.position = new Vector3(i*15,0,0);
和上面的兩行代碼是一樣的
GameObject table = GameObject.Instantiate(prefabObj, new Vector3(15 * i, 0, 0), Quaternion.identity) as GameObject;

激活對象
SetActive(true);
關閉對象
SetActive(false);
創建對象
在Unity場景中出現的所有實體都屬於遊戲對象
使用腳本來調用遊戲對象的方式有兩種:
一、將腳本綁定在一個遊戲對象上;
二、在代碼中動態綁定腳本和刪除腳本。
任何一個遊戲對象都可以同時綁定多條遊戲腳本,並且這些腳本互不干涉,各自完成各自的生命週期。
GameObject.CreatePrimitive(PrimitiveType.Cube);

1.GameObject.Find 查找
2.GameObject.FindWithTag 查找標籤
3.GameObject.FindGameObjectsWithTag 查找相同標籤的遊戲物體列表static GameObject[] FindGameObjectsWithTag (string tag) 返回一個用tag做標識的活動的遊戲物體的列表.如果沒有找到則 爲 空。標籤必須在使用之前到標籤管理器裏面聲明。

        GameObject obj01 = GameObject.Find("Capsule");
        //訪問obj01遊戲對象的名字和tag
        Debug.Log("obj01.name = " +obj01.name +"  obj01.tag ="+obj01.tag);
        GameObject obj2 = GameObject.FindWithTag("Player");
        //獲取tag值爲Player 的遊戲對象
        Debug.Log("obj2.name = " + obj2.name);
        GameObject obj03 = GameObject.FindGameObjectWithTag("Player");
        Debug.Log("obj03.name = " + obj03.name);
        //獲取所有標籤爲Person的遊戲對象
        GameObject[] objArr = GameObject.FindGameObjectsWithTag("Person");
        //查看對象數組的中每一個遊戲對象的信息
        for (int i = 0; i < objArr.Length; i++)
        {
            Debug.Log("name = " + objArr[i].name + "  tag =" + objArr[i].tag);
        }

銷燬遊戲對象
銷燬對象
public static void Destroy(Object obj);
在t秒後銷燬對象
public static void Destroy(Object obj, t(時間));
立刻銷燬遊戲對象
public static void DestroyImmediate(Object obj);

對象添加組件
GameObject myCamera = new GameObject();
myCamera.name = “myCamera”;
myCamera.AddComponent();(爲對象添加Camera組件)
獲取某一組件/腳本的字段,要放到Awake裏初始化(賦值);這樣才能獲取到想要的字段值。
test = myCamera.AddComponent();(獲取TestAddComponent腳本)

獲得組件:GetComponent
Component(組件)
(三種獲得組件的方法)
Component com = m_Camera.GetComponent(typeof(Transform));(獲得Transform);
Camera camera_Test = m_Camera.GetComponent();(獲得Camera);
Component com_Test = m_Camera.GetComponent(“Camera”);(獲得Camera);
Component trans = testobj.GetComponentInChildren();(獲得父子孫錄中想要的組件)
Component[] components = testobj.GetComponentsInChildren();(獲得父子全部的組件)

刪除遊戲對象組件
刪除遊戲對象組件的時候一般都不是單獨存在的,我們要向刪除,必須知道是刪除的那個遊戲對象身上的遊戲組件
void Start ()
{
//獲取遊戲對象
GameObject m_light = GameObject.Find(“Directional Light”);
//獲取組件
Light component = m_light.GetComponent();
//刪除組件
Destroy(component);

發佈了33 篇原創文章 · 獲贊 3 · 訪問量 9876
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章