unity常用API(一)
個人英語不好,所以看的是2018.1 的中文API 部分代碼和解釋都來源於此文檔:原文鏈接
視頻鏈接:點擊鏈接
unity自帶的一些函數
-
Awake:始終在任何 Start 函數之前並在實例化預制件之后調用此函數。(如果游戲對象在啟動期間處於非活動狀態,則在激活之后才會調用 Awake。)
-
Start:不是很緊急的初始化,一般放在Start里面來做。僅在Update函數第一次被調用前調用
-
Reset:調用 Reset 可以在腳本首次附加到對象時以及使用 Reset 命令時初始化腳本的屬性
-
Update:每幀調用一次 Update。這是用於幀更新的主要函數。
-
FixedUpdate:以相同時間間隔調用,用在力學更新效果中。執行在Update之前。
-
LateUpdate:在Update和FixedUpdate調用之后調用。一般人物的移動放在Update中,而攝像機的跟進變化放到FixedUpdate中。確保兩個獨立。
-
OnDestory:物體被刪除時調用。
-
OnEnable:(僅在對象處於激活狀態時調用)在啟用對象后立即調用此函數。在創建 MonoBehaviour 實例時(例如加載關卡或實例化具有腳本組件的游戲對象時)會執行此調用。
-
OnDisable:物體被禁用時調用。
調用順序:
Time類
-
deltaTime: 完成上一幀所用的時間(以秒為單位)(只讀)。
-
fixedDeltaTime:執行物理和其他固定幀率更新(如 MonoBehaviour 的 FixedUpdate)的時間間隔(以秒為單位)。
-
fixedTime:最近一次 FixedUpdate 已啟動的時間(只讀)。此為自游戲啟動以來的時間(以秒為單位)。
-
frameCount:已經過的總幀數(只讀)。
-
realtimeSinceStartup:游戲開始以來的實際時間(只讀)。
-
smoothDeltaTime:經過平滑處理的 Time.deltaTime(只讀)。
-
time:該幀開始的時間(只讀)。此為自游戲啟動以來的時間(以秒為單位)。
-
timeScale:時間流逝的縮放。可用於慢動作效果。
-
timeSinceLevelLoad:該幀開始以來的時間(只讀)。此為自加載上一個關卡以來的時間(以秒為單位)。
測試性能
float timeStart = Time.realtimeSinceStartup;
for (int i = 0; i < 10000; i++)
{
Method();
}
float timeEnd = Time.realtimeSinceStartup;
Debug.Log(timeEnd-timeStart);
使物體向前移動
cube.transform.Translate(Vector3.forward * Time.deltaTime);
GameObject 類
-
創建物體的三種方式
// 第一種 GameObject go= new GameObject("cube"); // 克隆一個已有的 GameObject.Instantiate(prefab); // 創建基本的物體 GameObject go =GameObject.CreatePrimitive(PrimitiveType.Cube);
-
添加組件
// 添加剛體 go.AddComponent<Rigidbody>(); // 添加 腳本 go.AddComponent<API01Event>(); // API01Event 是腳本
-
activeInHierarchy:定義 GameObject 在 Scene 中是否處於活動狀態。
Debug.Log(go.activeInHierarchy); // True go.SetActive(false); Debug.Log(go.activeInHierarchy); // False
-
Tag:此游戲對象的標簽。
-
name:對象的名稱。
Debug.Log(go.name); Debug.Log(go.GetComponent<Transform>().name)//獲取組件的名字其實獲取的是物體的名字
-
layer: 該游戲對象所在的層。
-
scene:該 GameObject 所屬的場景。
Public Functions
-
AddComponent : 將名為 className 的組件類添加到該游戲對象。
go.AddComponent<Rigidbody>();
-
ComPareTag : 如果游戲對象附加了類型為 type 的組件,則將其返回,否則返回 null。
-
SetActive : 激活/停用此 GameObject。
-
SendMessage : 調用此游戲對象中的每個 MonoBehaviour 上名為 methodName 的方法。
-
BroadcastMessage : 調用此游戲對象或其任何子項中的每個 MonoBehaviour 上名為 methodName 的方法。
target.BroadcastMessage("Attack",null,SendMessageOptions.DontRequireReceiver);//如果有接受者則發送,如果沒有不報錯 void Attack() //當一個物體的腳本里擁有此方法則會被調用(子類也會調用) { Debug.Log(this.gameObject + "正在攻擊"); }
-
SendMessageUpwards : 調用此游戲對象中的每個 MonoBehaviour 上或此行為的每個父級上名為 methodName 的方法。
-
GetCompont:如果游戲對象附加了類型為 type 的組件,則將其返回,否則返回 null。
Cube cube = target.GetComponent<Cube>(); //類 Transform t = target.GetComponent<Transform>();
-
GetComponts: 返回 GameObject 中類型為 type 的所有組件。
Cube[] cubes = target.GetComponents<Cube>(); cubes = target.GetComponentsInChildren<Cube>(); foreach(Cube c in cubes) { Debug.Log(c); }
-
GetComponentsInChildren: 使用深度首次搜索返回 GameObject 或其任何子項中類型為 type 的組件。
-
GetComponentsInParant: 返回 GameObject 或其任何父項中類型為 type 的組件。
Cube cube = target.GetComponent<Cube>(); // 獲取單個
Transform t = target.GetComponent<Transform>();
Debug.Log(cube);
Debug.Log(t);
Debug.Log("---------------------------------");
Cube[] cubes = target.GetComponents<Cube>(); // 獲取多個
Debug.Log(cubes.Length);
Debug.Log("---------------------------------");
cubes = target.GetComponentsInChildren<Cube>(); // 自己以及子類
foreach (Cube c in cubes)
{
Debug.Log(c);
}
Debug.Log("---------------------------------");
cubes = target.GetComponentsInParent<Cube>();
foreach (Cube c in cubes)
{
Debug.Log(c);
}
Static Functions
-
CreatePrimitive : 創建一個具有原始網格渲染器和相應碰撞體的游戲對象.
-
Find : 按 name 查找 GameObject,然后返回它。
GameObject go = GameObject.Find("Main Camera");
-
FindGameObjectWithTag : 返回標記為 tag 的活動 GameObject 的列表。如果未找到 GameObject,則返回空數組。
GameObject[] gos= GameObject.FindGameObjectsWithTag("MainCamera");
-
Destroy : 刪除 GameObject、組件或資源。 不會被立即回收
Destroy(this);(一般是腳本)(可以銷毀物體也可以銷毀組件) Destroy(gameObject,5); //(5秒后銷毀)
-
DontDestroyOnLoad:加載新場景時,不自動銷毀對象 /target/。
-
FindObjectOfType:返回第一個類型為 type 的已加載的激活對象。
-
FindObjectsOfType:返回所有類型為 type 的已加載的激活對象的列表。
Light light =FindObjectOfType<Light>(); //獲取所有的燈 light.enabled = false;
MonoBehaviour
-
Invoke : 在 time 秒后調用 methodName 方法。
Invoke("Attack", 3); void Update () { bool res = IsInvoking("Attack"); print(res); } void Attack() { Debug.Log("正在攻擊目標"); }
-
InvokeRepeating : 在 time 秒后調用 methodName 方法,然后每 repeatRate 秒調用一次。
InvokeRepeating("Attack", 4, 2);//第四秒開始調用第一次,之后每2秒調用一次
-
CancleInvoke : 取消該 MonoBehaviour 上的所有 Invoke 調用。
CancelInvoke("Attack");
協程
-
在使用協程的時候需要有以下幾個關鍵詞:
- 函數返回值是IEnmerator
- 函數返回時要使用 yield return xxx
- 調用協程方法時 我們要使用StartCoroutine(xxx())
public GameObject cube; private void Update() { if (Input.GetKeyDown(KeyCode.Space)) { StartCoroutine(Fade()); } } IEnumerator Fade() { for (float i = 0; i <= 1; i += 0.1f) { // 第一種方式 //cube.GetComponent<MeshRenderer>().material.color = new Color(i,i,i,i); // 第二種方式 Color color = cube.GetComponent<MeshRenderer>().material.color; Color newColor = Color.Lerp(color, Color.black,0.1f); // 向黑色逐漸靠近 cube.GetComponent<MeshRenderer>().material.color = newColor; yield return new WaitForSeconds(0.1f); // 暫停 } }
- StopCoroutine : 停止在該行為上運行的第一個名為 methodName 的協同程序或存儲在 routine 中的協同程序
// 第一種停止方式 Coroutine A = StartCoroutine(coroutineA()); StopCoroutine(A); // 第二種停止方式 IEnumerator DemoFuntion(){ ... } IEnumerator coroutine = DemoFuntion(); StartCoroutine(coroutine); StopCoroutine(coroutine);
- **StopAllCoroutines : 停止在該行為上運行的所有協程。