從下面這張大佬做的圖,
我們知道GameObject類和Component都繼承于一個Object類,而元件類又派生出許多種元件。
而元件執行個體化,必須添加到遊戲對象中。
遊戲對象的許多屬性和特征都要由元件表現。
即Component類執行個體化的對象,必須依附GameObject類的對象而存在。
---------------------------------------------------------------
元件的類方法,也是通過GameObject類的對象來展現出現了
這張圖是從http://blog.csdn.net/duzixi來的
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLycGVPVzYE1UMNpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLwMTM5UjNyIjM4IDMxkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
Component屬性
Component.gameObject【元件-對象】
------------------------------------------------------
public GameObject gameObject;
------------------------------------------------------
//Component如何實作與GameObject的關聯
一般情況下,定義了一個"GameObject對象"的同時,
也定義了一個"Component對象"。
而每定義一個"Component對象"又會建立一個沒有Component元件的"GameObject的對象",
名字為"gameObject"。
名字為"gameObject"的純空對象,
和最開始的"GameObject對象"通過引用的方式,
使他們指向同一個對象。
這樣就輕輕松松的實作了元件和對象的關聯了。
//向對象添加元件的方式是一樣的
這也是為什麼我們可以在腳本内通過"gameObject"這個名稱,調用到腳本挂載到的對象。
------------------------------------------------------
//unity官方例子
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
print(gameObject.name);
}
}
------------------------------------------------------
Component.tag【元件标簽】
------------------------------------------------------
public string tag;//标簽的定義,本質是字元串
------------------------------------------------------
//unity官方例子
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
Debug.Log("元件的标簽: " + gameObject.tag);
//從這個例子可以看出,GameObject類執行個體化對象的标簽
//就是元件的标簽
}
}
Component.name【對象層級名】
------------------------------------------------------
public string name;//層級名的定義,本質是字元串
------------------------------------------------------
Component【元件名】
------------------------------------------------------
Debug.Log(元件對象名); //這個傳回的是元件在檢查器中的名字
------------------------------------------------------
Component.transform【位置元件】
------------------------------------------------------
public Transform transform; //定義一個位置元件
------------------------------------------------------
"Transform"類是一個"Component"元件派生類,是一個"元件類"
//那麼為什麼要把一個位置元件歸為元件屬性的一部分?
是因為這玩意太重要了,每一個遊戲對象在場景中都要有一個明确的位置,
不然這個元件該如何出現在場景中。
是以,雖然Transform類是一個元件類,但也歸為元件和對象的基本屬性。
//由于是一個專門位置的元件,這裡就不專門細分開講了
------------------------------------------------------
//unity官方例子
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
transform.Translate(1, 1, 1);
}
}
公有類方法-(PublicMethods)
其實這些方法GameObject類裡都有,但是我偷懶沒仔細看。
Component.SendMessage消息傳遞類方法
Component.BroadcastMessage 自己及其子對象收得到
Component.SendMessage 僅自己收得到
Component.SendMessageUpwards 自己及其父級對象收到的
------------------
"注意:"未激活對象收不大,沒有子父關系的對象收不到
Component.BroadcastMessage【廣播消息】:自己及其子對象收得到
------------------------------------------------------
//函數原型
public void BroadcastMessage(
string methodName,
object parameter = null,
SendMessageOptions options = SendMessageOptions.RequireReceiver
);
public void BroadcastMessage(string methodName, SendMessageOptions options);
------------------------------------------------------
//參數說明
"methodName":需要調用的函數名(用字元串表示)
"parameter" :發送的參數值【可選參數】
"options" :如果有對象沒有接手到消息是否報錯【可選參數】
//SendMessageOptions.RequireReceiver //這個參數表示傳回(預設是傳回的)
//SendMessageOptions.DontRequireReceiver//這個表示不傳回
------------------------------------------------------
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
BroadcastMessage("ApplyDamage", 5.0 ,SendMessageOptions.DontRequireReceiver);
//調用所有(自身、子)對象中名為"ApplyDamage"的函數,并接受5.0這個參數
//SendMessageOptions.DontRequireReceiver表示如果對象沒有這個函數,
//不傳回錯誤資訊
}
//這個函數可以放在任意一個子對象中,用于接收消息。
void ApplyDamage(float damage)
{
print(damage);
}
}
Component.SendMessage【發送消息】僅自己收得到
------------------------------------------------------
//函數原型
public void SendMessage(string methodName);
public void SendMessage(string methodName, object value);
public void SendMessage(string methodName, object value, SendMessageOptions options);
public void SendMessage(string methodName, SendMessageOptions options);
------------------------------------------------------
//參數說明
"methodName":需要調用的函數名(用字元串表示)
"value" :發送的參數值【可選參數】
"options" :如果有對象沒有接手到消息是否報錯【可選參數】
//SendMessageOptions.RequireReceiver //這個參數表示傳回(預設是傳回的)
//SendMessageOptions.DontRequireReceiver//這個表示不傳回
------------------------------------------------------
//使用方法和上面那個完全一樣,隻是發送的對象不一樣。
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
SendMessage("ApplyDamage", 5.0);
}
void ApplyDamage(float damage)
{
print(damage);
}
}
Component.SendMessageUpwards【向上發送消息】自己及其父級對象收到的
------------------------------------------------------
//函數原型
public void SendMessageUpwards(string methodName, SendMessageOptions options);
public void SendMessageUpwards(
string methodName,
object value = null,
SendMessageOptions options = SendMessageOptions.RequireReceiver
);
------------------------------------------------------
//參數說明
"methodName":需要調用的函數名(用字元串表示)
"value" :發送的參數值【可選參數】
"options" :如果有對象沒有接手到消息是否報錯【可選參數】
//SendMessageOptions.RequireReceiver //這個參數表示傳回(預設是傳回的)
//SendMessageOptions.DontRequireReceiver//這個表示不傳回
------------------------------------------------------
//使用方法和上面那個完全一樣,隻是發送的對象不一樣。
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
SendMessageUpwards("ApplyDamage", 5.0);
}
void ApplyDamage(float damage)
{
print(damage);
}
}
Component.CompareTag标簽
Component.CompareTag
------------------------------------------------------
//函數原型
public bool CompareTag(string tag);
------------------------------------------------------
//參數說明
傳回值 :bool (布爾值)
string tag:接受一串字元串
------------------------------------------------------
//官方例子
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
Destroy(other.gameObject);
}
}
}
Component.GetComponent擷取元件
Component.GetComponent【擷取元件】
------------------------------------------------------
//函數原型
public Component GetComponent(Type type);
public T GetComponent();
public Component GetComponent(string type);
------------------------------------------------------
//參數說明
傳回值:傳回元件
Type type :typeof(元件類型) //這個參數要接受一個函數,表示元件類型
------------------------------------------------------
//官方示例
using UnityEngine;
public class ScriptExample : MonoBehaviour
{
void Start()
{
Transform xyz = GetComponent<Transform>();
Transform xyz2 = GetComponent("Transform") as Transform;
//這兩個都一樣
}
}
Component.GetComponents【擷取目前對象所有的元件】
------------------------------------------------------
//函數原型
public Component[] GetComponents(Type type);
public T[] GetComponents();
------------------------------------------------------
//參數說明
"傳回值" :Component[] //傳回元件組
Type type : typeof(元件類型) //這個參數要接受一個函數,表示元件類型
------------------------------------------------------
"功能" :傳回所有類型為Type的元件
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Component[] comp = gameObject.GetComponents<Component>();
for (int i = 1; i < comp.Length; i++)
{
Debug.Log("\n層級名:" + gameObject.name);
Debug.Log("\n元件名稱:" + comp[i]); //傳回元件的名字
}
}
}
Component.GetComponentInChildren
------------------------------------------------------
//函數原型
public Component GetComponentInChildren(Type t);
------------------------------------------------------
//
這個函數感覺有點迷,按官方的文檔上說,應該以深度搜尋然後傳回子對象的元件。
但我在實際測試中,效果卻和GetComponent一樣。
//應該是我沒搞懂這個怎麼用
Component.GetComponentInParent
------------------------------------------------------
//函數原型
public Component GetComponentInParent(Type t);
------------------------------------------------------
//這個類方法,官方連示例都懶得寫了
但經過測試,這個函數也和上面那個類方法一樣迷
Component.GetComponentsInParent【傳回自己和父級的全部元件】
------------------------------------------------------
public Component[] GetComponentsInParent(Type t, bool includeInactive = false);
public T[] GetComponentsInParent(bool includeInactive);
public T[] GetComponentsInParent();
------------------------------------------------------
//參數說明
傳回值 :傳回所有自己及其子級元件。
bool includeInactive:
是否傳回未激活的元件。
//true :傳回
//false:不傳回
Type t :typeof(元件類型) //這個參數要接受一個函數,表示元件類型
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
//這兩句是一樣的
//Component[] comp = GetComponentsInParent(typeof(Component),false);
Component[] comp = GetComponentsInParent<Component>();
for(int i = 0; i < comp.Length; i++)
{
Debug.Log("\n層級名:" + (comp[i]).name);
Debug.Log("\n元件名稱:" + (comp[i]));
}
}
}
------------------------------------------------------
Component.GetComponentsInChildren【傳回自己和子級的全部元件】
------------------------------------------------------
public Component[] GetComponentsInChildren(Type t, bool includeInactive);
------------------------------------------------------
//參數說明
傳回值 :傳回所有自己及其子級元件。
bool includeInactive:
是否傳回未激活的元件。
//true :傳回
//false:不傳回
Type t :typeof(元件類型) //這個參數要接受一個函數,表示元件類型
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
//這兩句是一樣的
//Component[] comp = GetComponentsInChildren(typeof(Component),false);
Component[] comp = GetComponentsInChildren<Component>();
for(int i = 0; i < comp.Length; i++)
{
Debug.Log("\n層級名:" + (comp[i]).name);
Debug.Log("\n元件名稱:" + (comp[i]));
}
}
}
Component.TryGetComponent【嘗試擷取元件,如果元件不存在,不傳回類型】
------------------------------------------------------
public bool TryGetComponent(Type type, out Component component);
public bool TryGetComponent(out T component);
------------------------------------------------------
Type type:尋找參數類型
out Comonent component :定義一個元件,并把找到的元件與其相引用
out T component:
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityGameOject : MonoBehaviour
{
void Start()
{
if(gameObject.TryGetComponent<Transform>(out Transform xyz) == false)
{
Debug.Log("元件擷取失敗");
}
}
}
繼承公共類方法
(繼承自Object類的類方法)
Object.ToString 傳回層級中的名字
Object.ToString
------------------------------------------------------
public string ToString();
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityGameOject : MonoBehaviour
{
void Start()
{
//對象名.ToString();
//可以傳回對象在層級中的名字。
Debug.Log("name:" + gameObject.ToString());
}
}
Object.GetInstanceID() 傳回對象ID
Object.GetInstanceID
------------------------------------------------------
public int GetInstanceID();
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityGameOject : MonoBehaviour
{
void Start()
{
//可以傳回對象的ID值
Debug.Log("ID:" + gameObject.GetInstanceID());
}
}
繼承靜态類方法
(繼承自Object類的靜态類方法)
//所謂靜态,就是不必通過對象執行個體化就可以使用的對象。
Object.Destroy
------------------------------------------------------
public static void Destroy(Object obj, float t = 0.0F);
------------------------------------------------------
//參數說明:
Object obj :删除的元件或者對象
float t = 0.0f :延時幾秒後删除
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityGameOject : MonoBehaviour
{
void Start()
{
//删除元件
Destroy(gameObject.GetComponent<BoxCollider>());
Destroy(gameObject); //直接删除對象
Destroy(gOj, 10f); //延時10秒後删除
}
}
Object.DontDestroyOnLoad
------------------------------------------------------
暫時不懂,留着以後補充
按類型查找對象
Object.FindObjectOfType
------------------------------------------------------
public static Object FindObjectOfType(Type type);
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
Transform xyz = FindObjectOfType<Transform>();
//從場景中尋找 Transform元件,并傳回
}
}
Object.FindObjectsOfType
------------------------------------------------------
public static Object FindObjectsOfType(Type type);
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
Transform[] xyz = FindObjectsOfType<Transform>();
for(int i = 0 ; i < xyz.Length; i++)
{
//傳回所有用于Transform元件的對象的層級名
Debug.Log("\n層級名:" + xyz[i].ToString());
}
}
}
(菜鳥的學習筆記,如果發現問題,希望各位能指正)