Question description
Because Unity's design aims to prevent such operations, accessing Unity API directly from individual threads will cause abnormalities. UnityThread script solves this problem by promoting communication between threads.This script provides a method or corporate method in the update, Lateupdate, and Fixedupdate functions of the main thread.
Initialization:
Execute operations in the main thread:
Using unitythread.executeinUpdate () to perform operations in update (). Using unityThread.executeinLeUpdate () to perform operations in LateUpdate ().
UnityThread.executeInUpdate(() => transform.Rotate(new Vector3(0f, 90f, 0f)));
Action rot = Rotate; UnityThread.executeInUpdate(rot); void Rotate() { transform.Rotate(new Vector3(0f, 90f, 0f)); }
UnityThread.executeInLateUpdate(() => { /* 相机移动代码 */ });
UnityThread.executeCoroutine(myCoroutine()); IEnumerator myCoroutine() { Debug.Log("Hello"); yield return new WaitForSeconds(2f); Debug.Log("Test"); }
The above is the detailed content of How Can I Safely Access the Unity API from Separate Threads?. For more information, please follow other related articles on the PHP Chinese website!