Use Unity API from other threads or call functions in the main thread
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
<code class="language-csharp">UnityThread.executeInUpdate(() => { // 在主线程中执行的代码 });</code>
How to use
<code class="language-csharp">Action rot = Rotate; UnityThread.executeInUpdate(rot); void Rotate() { // 函数代码 }</code>
Initialize in :
<code class="language-csharp">UnityThread.executeCoroutine(myCoroutine()); IEnumerator myCoroutine() { // 协程代码 }</code>
Awake()
Perform in : UnityThread
<code class="language-csharp">UnityThread.initUnityThread();</code>
Update
<code class="language-csharp">UnityThread.executeInUpdate(() => { // Update 代码 });</code>
Update
<code class="language-csharp">Action rot = Rotate; UnityThread.executeInUpdate(rot); void Rotate() { // 方法代码 }</code>
LateUpdate
Additional description
<code class="language-csharp">UnityThread.executeInLateUpdate(() => { // LateUpdate 代码 });</code>
FixedUpdate
This solution uses a single <code class="language-csharp">UnityThread.executeInFixedUpdate(() => { // FixedUpdate 代码 });</code>
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!