In Unity I have the following code in a script attached to a generic Game Object that has a Cube in it:
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);
}
}
As you can see there is a SlowJob function that is executing on a different thread. Now, I want to update something in unity (the cube in this case) So I tried four things:
- Move the cube in the main thread. This works well, as it should
- Move the cube from the secondary thread. This of course, does not work. The error says "get_transform can only be called from the main thread. This of course is expected.
- Move the cube using InvokeOnAppThread. The (scarce) documentation says that this invokes a delegate on application thread. I am thinking that this should work. But it throws the same error
- Just to try I also tried InvokeOnUIThread with the same error.
My question is why Option 3 does not work? I thought InvokeOnAppThread was used for these cases
(Just in case in SO there is only one other question regarding InvokeOnAppThread. Asked by me. And this is a different question so this is not a duplicate question)
My question is not on how to call unity stuff from another thread (although it solves that as well). My question is Why InvokeOnAppThread does not work for this?
I am aware there are convoluted ways to solve this. This is not what this question is about
I repeat This is not a duplicate question.
Any useful advice greatly appreciated