Let's go for a while. NET4.5 native Asynchronous
classprogram{Static voidMain (string[] args) {Executeasync (); Console.WriteLine ("Start"); Console.read (); } Static Async voidExecuteasync () {awaitTask.run (() ={System.Threading.Thread.Sleep ( -); Console.WriteLine ("Async Executed"); }); Console.WriteLine ("End"); }}
View Code
So unity is only supported by the castrated version of Mono. Net4.0, so you can only write a class to simulate the
The evening whim, in the success of the simulation. Net4.5 asynchronous, slightly awkward, here are the results:
Public class_atasync_monotestexample:monobehaviour{voidStart () {varAsyncop =NewAtasync (); varMoveleftvalue = asyncop.await<BOOL>(MoveLeft ()); Asyncop.async (()= { if(Moveleftvalue.value = =true) {asyncop.await (MoveRight ()); Asyncop.await (Backorigin ()); Asyncop.async (()={Debug.Log ("All finished!"); }); Debug.Log ("Start execute!"); } }); }...
Execution Result:
Because it is impossible to implement the global asynchronous invocation of static class, it will produce garbage task, which is more troublesome to solve. So it's a handy way to operate as an object.
The asynchronous message is placed in the Async method, and the wait is placed in the await.
So why split into await and async, you can use await (...) A method. Because await (...) The parameters that are received are specific information about the asynchronous task, including how the task begins to end.
Async (...) is the normal operation, the incoming parameter is an action, in fact, it is convenient to call
The specific implementation of the task function, the use of interfaces to solve the problem of generics, or directly with the lambda, without the function encapsulation
Iattask MoveLeft () { varnew attask<bool> (handle = { New gotweenconfig (). Position (new Vector3 (00)). OnComplete (tween = handle. Finished (true)); }); return result;}
. Net4.5 Async and await and simulate in Unity3d