在Unity我已經連接到一個通用的遊戲對象的腳本中有一個多維數據集下面的代碼:InvokeOnAppThread調用的應用程序線程拋出一個錯誤
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
//using System.Runtime.Tasks;
public class ExampleScript : MonoBehaviour {
// Use this for initialization
void Start()
{
Debug.Log ("Start():: Starting");
//SlowJob();
Thread myThread = new Thread (SlowJob);
myThread.Start();
Debug.Log ("Start():: Done");
}
// Update is called once per frame
void Update() {
//Option 1) This will move the cube in the application thread
//this.transform.Translate (Vector3.up * Time.deltaTime);
}
void SlowJob()
{
Debug.Log ("ExampleScript::SlowJob() --Doing 1000 things, each taking 2ms ");
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
{
//Option 2) We are trying to move the cube in the secondary thread! ooops!!
//this.transform.Translate (Vector3.up * 0.002f);
//get_transform can only be called from the main thread
//Option 3) We are going to try to use a delegate on application thread
UnityEngine.WSA.Application.InvokeOnAppThread (()=>{
this.transform.Translate (Vector3.up * 0.002f);
},false);
//Option4
/* UnityEngine.WSA.Application.InvokeOnUIThread (()=>{
this.transform.Translate (Vector3.up * 0.002f);
},false);
*/
Thread.Sleep (2); //sleep for 2ms
}
sw.Stop();
Debug.Log ("ExampleScript::SlowJob() --Done! Elapsed Time:" + sw.ElapsedMilliseconds/1000f);
}
}
正如你可以看到有一個SlowJob功能即在另一個線程上執行。 現在,我想更新團結的東西(在這種情況下,立方體) 於是,我四件事情:
- 移動在主線程的立方體。這很好,因爲它應該
- 從輔助線程移動多維數據集。這當然不起作用。錯誤提示「get_transform只能從主線程調用,這當然是可以的
- 使用InvokeOnAppThread移動多維數據集(稀少)文檔說這調用了應用程序線程上的一個委託我認爲這個應該的工作,但它會引發同樣的錯誤
- 只是爲了試試我也試過InvokeOnUIThread具有相同的錯誤。
我的問題是,爲什麼選擇3不起作用?我以爲InvokeOnAppThread用於這些案件
(以防萬一在這裏只有一個關於InvokeOnAppThread的問題。我問。這是一個不同的問題,所以這不是重複的問題)
我的問題不是如何從另一個線程調用統一的東西(雖然它解決了爲好)。我的問題是爲什麼InvokeOnAppThread不適用於此?
我知道有很複雜的方法來解決這個問題。這不是什麼這個問題是關於
我重複這不是一個重複的問題。
任何有用的建議非常讚賞
這兩個函數僅適用於Windows。查看如何從另一個'Thread'調用Unity API的副本。 – Programmer
'this.transform'只能由創建它的線程訪問。我不知道'InvokeOnAppThread'是幹什麼的,但即使調用的線程沒有創建'this.transform'對象,也使用它,那麼它就無法訪問它。 – CodingYoshi
@CodingYoshi不幸的是InvokeOnAppThread是一個記錄不完善的函數。你說的是真的(因此選項2不起作用),但我認爲這個選項是爲了完成那個 – KansaiRobot