Unity2019.3API教程(四)Component類

Component類

1. 官方定義
class in UnityEngine/Inherits from:Object/Implemented in:UnityEngine.CoreModule
屬於UnityEngine命名空間下,繼承自Object類,與之前的Object類和GameObject類一樣屬於核心模塊。

2. 官方描述
Base class for everything attached to GameObjects.
Note that your code will never directly create a Component. Instead, you write script code, and attach the script to a GameObject. See Also: ScriptableObject as a way to create scripts that do not attach to any GameObject.
此類要對比下GameObject類,GameObject類是場景中所有實體的基類,而Component類是掛載在實體身上所有組件的基類,官方強調腳本是一個組件,但是腳本不可以直接掛載到實體上,當然官方介紹的ScriptableObject類是一種不附加到實體上的方法,後續再做介紹。

3. 屬性
gameObject:The game object this component is attached to. A component is always attached to a gameobject.
語法:public GameObject gameObject;
獲取自身物體,Component類比較特殊,除了gameobject這個屬性,其他的與GameObject類的屬性和方法相同,我這邊全部裂解液出來,再在最後說明原因。

tag:The tag of this game object.

transform:The Transform attached to this GameObject.

4. 共有方法
BroadcastMessage:Calls the method named methodName on every MonoBehaviour in this game object or any of its children.

CompareTag:Is this game object tagged with tag ?

GetComponent:Returns the component of Type type if the game object has one attached, null if it doesn’t.

GetComponentInChildren:Returns the component of Type type in the GameObject or any of its children using depth first search.

GetComponentInParent:Returns the component of Type type in the GameObject or any of its parents.

GetComponents:Returns all components of Type type in the GameObject.

GetComponentsInChildren:Returns all components of Type type in the GameObject or any of its children.

GetComponentsInParent:Returns all components of Type type in the GameObject or any of its parents.

SendMessage:Calls the method named methodName on every MonoBehaviour in this game object.

SendMessageUpwards:Calls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour.

TryGetComponent:Gets the component of the specified type, if it exists.

以上就是官方2019.3版本Component類的全部內容,看過第三期教程的朋友會發現屬性和方法和GameObject類基本相同,這是爲什麼呢,因爲Unity在設計時,通過GameObject類創建場景實體,而實體的屬性和功能通過組件來完成,而組件時掛載在實體上的,所以Component類給出gameobject屬性(注意此處爲小寫),gameobject時Component類的屬性,很多朋友在學習API時對於Object,GameObject以及gameobject發生混淆,Object和GameObject時類,具有父子關係,他們同屬於UnityEngine命名空間下,而gameobject屬於Component類的一個屬性,Component類和Object也是一種父子關係,Component類和GameObject類在設計上屬於同級關係,但是基於Unity的設計,在實現上,GameObjec類是實現的基礎,而Component類是實現的行爲外觀,這裏還要提一個Object類,根據Unity結構,他不屬於UnityEngine命名空間下,而是System.Object,屬於最終基類,因爲我們用UNity寫的腳本現在都是C#語言,這裏只做概述不做具體介紹。所以,我們可以通過組件訪問掛載物體,從而實現訪問,示例如下:

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    
    void Start()
    {
        GameObject go = new GameObject();
        go.AddComponent(typeof(Rigidbody));
        Rigidbody rg = go.GetComponent(typeof(Rigidbody))as Rigidbody;
        rg.gameObject.AddComponent(typeof(Mesh));
    }
}

後面官方給的是繼承的內容,此處不做概述,本期教程到此結束,歡迎大家在評論區給出意見和指正,謝謝瀏覽。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章