close
說到Unity,就不能不知道GetComponent這個系列的方法,為什麼這麼說呢?
大部分遊戲中物件的操作都會牽扯到屬性值的改變,或是使用屬性類別的公開方法來執行動作,那麼要如何取到這些屬性的值或方法呢?如果你用Inspector大可以設定一些屬性值,但對於遊戲中要去進行改變,那妳就必須用到程式來做到,因此要改變物件的屬性值,妳就必須先抓到這個屬性。
GetComponent 方法是用來取得的物件屬性元件的方法,回傳取用的屬性類別,算是很常使用到的方法,官方也提供了幾種大同小異的 GetComponent 方法來更快速方便地取用物件屬性。
要取得的類別必須在物件有套用或繼承,否則會回傳 Null Reference 例外狀況。
Cube物件有套用 Transform、 MeshFilter、BoxCollider、MeshRenderer等屬性元件,但有一些值或方法是屬於繼承類別的話也可以取得到,如GameObject底下的 name、tag、enable 等等。(如下圖)
 
 
 
 
 
 
 
 
 
 
 
 
以下列舉來說明不同的系列方法:
  • GetComponent 取得物件的屬性元件 (單個)
  • GetComponentInChildren 取得物件的子物件的屬性元件 (單個)
  • GetComponentInParent 取得物件的父物件的屬性元件 (單個)
  • GetComponents 取得物件的屬性元件 (多個)
  • GetComponentsInChildren 取得物件的子物件的屬性元件 (多個)
  • GetComponentsInParent 取得物件的父物件的屬性元件 (多個)

 

參數說明:
● type 是泛型類別,可以是官方提供的類別,也可以是自定義的類別,會影響回傳的型態
  • 注意:

GetComponentsInChildren 和 GetComponentsInParent 方法可以多載,由於預設只會取得有包含此 Type 屬性有啟用的物件並回傳,因此若有物件包含此 Type 的屬性元件,但並未啟用的話就不會取得,若要取得未啟用屬性的物件也可以使用布林參數來實現。

  • 多載:
參數說明:
● type 是泛型類別,可以是官方提供的類別,也可以是自定義的類別,會影響回傳的型態。
● includeInactive 為布林值,取元件時是否包含未啟用的元件,預設為 false。
  • 用法:(以下三種寫法)

一般來說我們會用 物件.GetComponent<類別>() 的寫法,但也可以用 物件.類別 的方法來實現,取決於物件是否有繼承到這個類別,有繼承的話才能直接用後者,不過如果是要取父物件或是子物件的屬性時,就無法使用後者。

//取得物件的 Transform 屬性
this.transform;
this.GetComponent<Transform>();
this.GetComponent(typeof(Transform));

//取得物件的 Button 屬性
this.button;
this.GetComponent<Button>();
this.GetComponent(typeof(Button));

//取得 canvas 物件底下的所有圖片子物件 (包含沒啟用的)
canvas.GetComponentsInChildren<Image>(true);
canvas.GetComponentsInChildren<Image>(true);
canvas.GetComponentsInChildren(typeof(Image),true);
  • 實做 (一):取得玩家物件的座標
using UnityEngine;
using System.Collections;
public class GetObjectPosition: MonoBehaviour
{    
    public GameObject player;
    void Start()
    {
        Vector3 position = player.GetComponent<Transform>().position;
        Debug.Log(position);
    }}
  • 實做 (二):為按鈕加入點擊事件
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ButtonEvent: MonoBehaviour
{    
    void Start()
    {
        Button button = GetComponent<Button>();
        button.onClick.AddListener(click);
    }
    void click()
    {
        Debug.Log("Button Click ! ");
    }
}
  • 實做 (三):取得自定義類別的屬性及方法
using UnityEngine;
using System.Collections;

//自定義方法
public class Circle
{
    //半徑
    public float radius = 2;
    //取得圓周長
    public float getCircumference()
    {
        return radius * 2 * Mathf.PI;
    }
    //取得圓面積
    public float getArea()
    {
        return redius * redius * Mathf.PI;
    }
}
以上類別套用到圓形物件上,再用以下按鈕取屬性值和方法。
using UnityEngine;
using System.Collections;
public class ButtonEvent: MonoBehaviour
{    
    public Circle circle;

    void Start()
    {
        Button button = GetComponent<Button>();
        button.onClick.AddListener(ShowInfomation);
    }
    void ShowInfomation()
    {
        var radius = circle.GetComponent<Circle>().radius;
        var circumference = circle.GetComponent<Circle>().getCircumference();
        var area = circle.GetComponent<Circle>().getArea();
        Debug.Log("半徑 = " + radius);
        Debug.Log("週長 = " + circumference );
        Debug.Log("面積 = " + area );
    }
}
//執行結果:
//半徑 = 2
//週長 = 12.56
//面積 = 12.56
  • 結語:
  • GetComponent 開頭的方法是回傳單一結果,而 GetComponents 開頭的方法會回傳多個結果的陣列,回傳型態依取得的屬性元件類別而定。
  • 用 GetComponent 系列方法取得的屬性元件不存在時會回傳 Null。
  • 用 GetComponents 系列方法取值,即便只有一個回傳值也會用陣列儲存。
  • 取得屬性元件之後,可以使用其類別底下的公開屬性及方法,如實做所示。
 

Try it and have fun!

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Evia.H 的頭像
    Evia.H

    Evia WorkStation 伊維亞工作室

    Evia.H 發表在 痞客邦 留言(0) 人氣()