. Net4.5 Async and await and simulate in Unity3d

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.