Solution
Call the Unity APIImplement the following steps:
Create a staticcalled
to store the operation that needs to be performed in the main thread.actionQueuesUpdateFunc
) and perform these operations. List<Action>
actionQueuesUpdateFunc
actionCopiedQueueUpdateFunc
noActionQueueToExecuteUpdateFunc
Update
The function to call the function from a separate thread: or, pass the function as a parameter:
Operating corporation in the main thread
UnityThread.executeInUpdate(() => { // 在主线程中执行的代码 });
How to use <使用>
Action rot = Rotate; UnityThread.executeInUpdate(rot); void Rotate() { // 函数代码 }
Initialize in <初> :
UnityThread.executeCoroutine(myCoroutine()); IEnumerator myCoroutine() { // 协程代码 }
Awake()
Perform in <执>: UnityThread
UnityThread.initUnityThread();
Update
UnityThread.executeInUpdate(() => { // Update 代码 });
Update
Action rot = Rotate; UnityThread.executeInUpdate(rot); void Rotate() { // 方法代码 }
LateUpdate
Additional description <附>
UnityThread.executeInLateUpdate(() => { // LateUpdate 代码 });
FixedUpdate
This solution uses a single UnityThread.executeInFixedUpdate(() => { // FixedUpdate 代码 });
The above is the detailed content of How to Safely Call Unity API Functions from Non-Main Threads?. For more information, please follow other related articles on the PHP Chinese website!