%3CLINGO-SUB%20id%3D%22lingo-sub-316851%22%20slang%3D%22zh-CN%22%3EAsyth%20programming%20in%20Unity%2C%20Using%20Coroutine%20and%20TAP.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-316851%22%20slang%3D%22zh-CN%22%3E%3CP%3E%3CSTRONG%3EFirst%20Published%20on%20MSDN%20on%20Sep%2001%2C%202017.%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CA%3E%20%3CSTRONG%3EBackground%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20.%20A%20couple%20of%20weeks%20ago%2C%20Unity%20has%20released%20a%20new%20version%2C%202017.1.%20In%20this%20version%2C%20the%20most%20bit%20bite%20ed%2C%20TAP%20and%20Async-await%20support%2C%20finally%20come%20out%20as%20a%20beta%20feature%2C%20with%20.NET%204.6%20Thilet.%20%3CBR%20%2F%3EThis%20is%20not%20a%20real%20a.%20With%20the%20sion%20the%20microsoft%20HoloLens%20and%20Windows%20MR%20platform%20development%20oedy%20soei%2C%20many%20unity%20of%20the%20developers%20sydd%20to%20the%20study%20to%20the%20weather%20with%20the%20UWP%20API%20to%20the%20mit.%20From%20windows%20platform.%20%3CBR%20%2F%3E%3CBR%20%2F%3EAs%20a%203D%20scripting%20engine%2C%20Unity%205.X%20and%20earlier%20provide%20only%20only%20limited.%20NET%202.0-4.%20x%2C%20c%20s%204%20support.%20To%20access%20a%20comprehensive%20asydapi%20still%20requirs%20a%20lot%20of%20adaptive%20work.%20If%20you%20y%20sy'n%20uWP%20y%20TAP%20the%20dyllying%20yn%20ystod%2C%20the%20developer%20is%20hard%20to%20port%20TAP%20into%20unity%20exist%20exist%20ing%20update%20check%2Fco-model%20development%20process.%20In%20the%20other%20hand%2C%20experienced%20UWP%20developers%20are%20andy%20ye%20to%20XR%20world%2C%20but%20are%20are%20byse%20by%20lacking%20async-await%2FTAP%2C%20misunderstanding%20coroutine%20astsy%20model.%20(I%20was%20one%20of%20the%20thes%20of%20the%20struggled%20member.)%20%3CBR%20%2F%3E%3CBR%20%2F%3ENow%2C%20with%20the%20constant%20maturity%20of%20.net%20libs%2C%20what%20is%20trying%20to%20smooth%20all%20platform%20sydds.%20With%20the%20works%20from%20unity%2C%20Microsoft%20and%20geeks%20in%20the%20community%2C%20unity%20developers%20and%20UWP%20developers%20are%20final%20lyy%20can%20to%20develop%20the%20co%20and%20TAP%20at%20the%20time%20same%20time.%20What%20is%20the%20kind%20of%20news%20mades%20me%20oedd%20loud%20in%20my%20dream.%20%3CBR%20%2F%3E%3CBR%20%2F%3EThosting%2C%20wely%20tha%20iad%20dydd%20this%20brings%20new%20challenges.%20We're%20update%20the%20function%20is%20not%20to%20all%20pass%20all%20all%20all%20to%20start%2C%20whatheyou%20are%20a%20unity%20master%20developer%2C%20you%20may%20has%20project%20it%20port%20to%20the%20UWP%20platform%2C%20or%20you%20are%20a%20novice%20of%20the.%20Unity%20but%20a%20UWP%20developer%2C%20you%20may%20want%20to%20upgrade%20your%202D%20XAML%20application%20to%20Windows%20MR%20platform.%20Or%2C%20if%20you%20are%20ally%20yr%20new%20to%20both%2C%20just%20want%20a%20lot%20of%20unity%20and%20UWP%20code%20examples%20find%20your%20app%20technical%20viility.%20No%20matter%20who%20you%20are%2C%20%2C%20you%20will%20will%20finally%20this%20this%20problem%3A%20how%20to%20interact%20with%20unity%20co%20and.%20Net%20TAP%20correctly.%20%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20article%20aims%20to%20provide%20you%20with%20a%20clear%20reference%2C%20or%20to%20be%20ei%20fod%20by%20the%20tools%20provided%20in%20this%20article%2C%20to%20provide%20for%20the%20bettery%20for%20work%20later%20and%20study.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CA%3E%3CSTRONG%3EEnvironment%20Environment.%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CA%3E%20%3CSTRONG%3ESoftware.%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20When%20I%20was%20writing%20this%20article%2C%20I%20was%20use%3A%3C%2FP%3E%0A%3CP%3Eo%20Windows%2010%20Creator%20Update%2015063.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eo%20Unity%202017.1.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eo%20Visual%20Studio%202017.2.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EAs%20most%20of%20the%20code%20snippets%20only%20dependents%20on%20Unity%20Editor%20Console%20printout%2C%20you%20can%20have%20use%20Unity%202017.1%20to.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CA%3E%20%3CSTRONG%3EProject%20Setting%20Up%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%20.%20The%20sample%20project%20needs%20to%20configure%20your%20unity%20project%20to%20start%20.NET%204.6%20and%20async%20support.%20If%20you%20are%20ar%20not%20familiar%20with%20this%2C%20it%20is%20sed%20it%20you%20follow%20the%20steps%20below%20to%20set%20up%20your%20new%20project.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E1.%20Start%20Unity.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2.%20Click%20on%20the%20%22New%22%20icon%2C%20enter%20project%20name%20%22AsyncExample%22%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66925iE9FB5E77FF0DC4D7%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E3.%20Click%20the%20%22Create%20Project%22%20button.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E4.%20Click%20on%20the%20menu%20%22File-%20and%20Build%20Settings%22%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66927i01466FDE3396BA2D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E5.%20Click%20%22Player%20Settings%22%20on%20the%20bottom.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3ELook%20at%20your%20%22Inspector%22%20window%2C%20there%20will%20be%20some%20some%20property%20tabs.%20Unfold%20%22Other%20Settings%22%20tab.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20321px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66928i738ED98B0ED1A0C5%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E6.%20Find%20the%20%22Configuration%22%20property%20group.%20%26nbsp%3BChange%20the%20value%20of%20%22Scripting%20Runtime%20Version%22%20%22Stable%20(.NET%203.5)%20%22%20to%20%22Experinmental%20(NET%204.6)%20%22%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20459px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66931i679C9407FC195EFF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E7.%20Unity%20Editor%20needs%20a%20complete%20reboot%20after%20you%20change%2C%20you%20will%20see%20sy'r%20sy'n%20mail.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20398px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66932i925D76082537D8F2%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3ESelect%20%22Restart%22%20to%20restart%20the%20Unity.%20%3CBR%20%2F%3EAfter%20san%20ei%20restart%2C%20at%20the%20same%20location%2C%20properties%20be%20a%20be%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20430px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66934iD2F1362E0E4292E2%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20it%20this%20point%2C%20Unity%20Editor%20obtained%20the%20theability%20to%20the%20execute%20c%3F%206.0%2C%20some%20sauly%20unauzzed%20class%20library%20y%20rheswill%20be%20able%20to%20be%20tested%20and%20a%20tha%20dweed%20at%20the%20a'%20sydd%3C%2FP%3E%0A%3CP%3E8.%20In%20the%20%22Project%22%20window%2C%20create%20a%20new%20child%20folder%20%22Scripts%22%20as%20child%20folder%20%22Assets%22.%20Create%20a%20new%20C%3Fscript%20%22AsyncExample.cs%22%20in%20%22Scripts%22%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66936i6E1162F93262815A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E9.%20In%20the%20%22Hierarchy%22%20window%2C%20create%20a%20new%2C%20empty%20object%20named%20%22ConsoleScripts%22%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20236px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66938iA3CF59AFBA4822DA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E10.%20Drag%20the%20script%20you%20have%20created%20into%20%22ConsoleScripts%22%20Game%20Object.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20488px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66940i782FE5176E8A39D8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%20%3CA%3E%20%3CSTRONG%3EBasic%20codes%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%20.%20Next%2C%20we%20need%20to%20prepare%20for%20some%20infrastructure%2C%20allows%20us%20to%20create%20some%20some%20simple%20syds%20for%20our%20tests.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CSTRONG%3ELogging%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20.%20Log%20output%20code%20is%20very%20simple%2C%20it%20will%20help%20us%20to%20Unity%20Console%20output%20format%20information.%20First%20of%20all%2C%20we%20add%20the%20the%20sleat%20code%20in%20the%20script%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20System.Collections%3B%0Ausing%20System.Diagnostics%3B%0Ausing%20System.Runtime.CompilerServices%3B%0Ausing%20System.Threading%3B%0Ausing%20System.Threading.Tasks%3B%0Ausing%20UnityEngine%3B%0A%0Apublic%20class%20AsyncExample%20%3A%20MonoBehaviour%0A%7B%0A%0A%20%20%20%20Stopwatch%20stopwatch%3B%0A%20%20%20%20void%20Start()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20stopwatch%20%3D%20new%20System.Diagnostics.Stopwatch()%3B%0A%20%20%20%20%20%20%20%20stopwatch.Start()%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20void%20LogToTUnityConsole(object%20content%2C%20string%20flag%2C%20%5BCallerMemberName%5D%20string%20callerName%20%3D%20null)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20UnityEngine.Debug.Log(%24%22%7BcallerName%7D%EF%BC%9A%5Ct%7Bflag%7D%20%7Bcontent%7D%5Ct%20at%20%7B%20stopwatch.ElapsedMilliseconds%20%7D%20%5Ct%20in%20thread%20%7BThread.CurrentThread.ManagedThreadId%7D%22)%3B%0A%20%20%20%20%7D%0A%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3EStopwatch%20can%20help%20us%20trace%20call%20time.%20What%20the%20unity%20scene%20scene%20the%20running%20state%2C%20the%20stopwatch%20will%20start%2C%20we%20can%20clearly%20see%20a%20thread%20at%20a%20certain%20time%2C%20in%20what%20is%20function%20to%20do%20what.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CI%3E%20%3CBR%20%2F%3E%20%3CEM%3ENext%2C%20In%20order%20to%20study%20how%20many%20astaly%20tasks%20directly%20link%20and%20call%2C%20we%20need%20often%20to%20pre-design-design%20some%20sms%2C%3C%2FEM%3E%20we%20need%20often%20to%20design%20template%20a%20typical.%20What%20ocan%20can%20easy%20to%20control%20and%20the%20output%20sequence.%20My%20choice%20was%20Count%20Down%20Task.%20%3CBR%20%2F%3E%3C%2FI%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%3CA%3E%20%3CSTRONG%3ECountdown%3A%20Coroutine%20Version.%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CA%3E%20%3CSTRONG%3ECode%3A%20CoroutineCountDown%3C%2FSTRONG%3E%20%3C%2FA%3E%20.%20%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20IEnumerator%20CoroutineCountDown(int%20count%2C%20string%20flag%20%3D%20%22%22)%0A%7B%0A%20%20%20%20for%20(int%20i%20%3D%20count%3B%20i%20%26gt%3B%3D%200%3B%20i--)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LogToTUnityConsole(i%2C%20flag)%3B%0A%20%20%20%20%20%20%20%20yield%20return%20new%20WaitForSeconds(1)%3B%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EIf%20you%20are%20a%20tha%20a%20tha%20a%20tha%20ath%20of%20unity%2C%20you%20can%20see%20this%20is%20a%20yield%20function%20returns%20a%20enumerator%2C%20by%20your%20c%20s%20experience.%20Unity%20can%20can%20use%20IEtor%20return%20ed%20this%20by%20method%2C%20what%20siman%20san%20e%20sauer%20of%20yield%20eIAI22s.%20This%20sequence%20is%20consumed%20by%20dispatcher%20in%20the%20base%20class%2C%20MonoBehavior.%20In%20each%20of%20the%20sauer%20sieiedinstruction%2C%20when%20the%20aissy%20is%20operation%20complete%2C%20the%20will%20will%20stod%20stod%20to%20the%20main%20thread%20of%20the%20unity%20and%20calls%20the%20IEnumerator%20MoveNext()%20method%20to%20get%20the%20next%20yieldinstruction.%20%3CBR%20%2F%3E%20%20%3CA%3E%3CSTRONG%3EExecution%3A%3C%2FSTRONG%3E%20%3C%2FA%3E%20.%20%3CSTRONG%3ECoroutineCountDown.%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CSTRONG%3E%20%3CI%3EExecute%20Sly.%3C%2FI%3E%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20Put%20the%20sleud%20code%20in%20the%20Start%20method%20as%20below%2C%20we%20sy'n%20run%20game%20in%20the%20editor%20and%20get%20output%20output%20console%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66941i0F0EF345B2BA6A5E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20you%20can%20see%2C%20in%20the%20logs%2C%20execution%20thread%20is%20alwaysss%20thread1%2C%20count%20down%20by%201%2C%20every%201%20second.%20%3CBR%20%2F%3E%20%20%3CSTRONG%3E%20%3CI%3EExecute%20in%20parallel%3C%2FI%3E%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20.%20As%20this%20code%20snippet%20runs%20asysly%2C%20in%20unity%20in%20this%20method%20will%20no%20no%20no%20sydd%20yn%20thread.%20It%20do%20also%20prevent%20not%20multiple%20execution%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3ELike%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evoid%20Start()%0A%7B%0A%20%20%20%20stopwatch%20%3D%20new%20System.Diagnostics.Stopwatch()%3B%0A%20%20%20%20stopwatch.Start()%3B%0A%20%20%20%20StartCoroutine(CoroutineCountDown(3%2C%20%22BasicCoCallA%22))%3B%0A%20%20%20%20StartCoroutine(CoroutineCountDown(3%2C%20%22BasicCoCallB%22))%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ETha%20we'r%20sy'n%20a%20row%2C%20we'll%20see%20the%20two%20tasks%20transformed%20them%20into%20siens%2C%20and%20weaved%20yn%20theoe%20oedd.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66942iA82307D2C25E5D78%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%3CSTRONG%3E%20%3CI%3EIn%20Execution%20linear%3C%2FI%3E%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20.%20If%20two%20or%20more%20Coroutines%20supts%20(including%20other%20process%20controls%2C%20such%20as%20if%20%2F%20switch%20%2F%20loop)%2C%20an%20more%20parent%20Coroutine%20is%20a%20bhith%20to%20be%20scheduled%20in%20unity.%20We%20can%20create%20the%20story%20code%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evoid%20Start()%0A%7B%0A%20%20%20%20stopwatch%20%3D%20new%20System.Diagnostics.Stopwatch()%3B%0A%20%20%20%20stopwatch.Start()%3B%0A%20%20%20%20StartCoroutine(CoroutineCountDownSeq(3%2C%20%22BasicCoCallA%22%2C%20%22BasicCoCallB%22))%3B%0A%7D%0A%0Apublic%20IEnumerator%20CoroutineCountDownSeq(int%20countDown%2C%20params%20string%5B%5D%20flags)%0A%7B%0A%20%20%20%20foreach%20(var%20flag%20in%20flags)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20yield%20return%20StartCoroutine(CoroutineCountDown(3%2C%20flag))%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EYou%20can%20see%20that%20parent%20parent%20e%20tess%20a%20IEnumerator%20waiting%20for%20the%20yield%20instructions%20(this%20time%20unityengine.coroutine.)%20dispatch%2C%20soman%20can%20schedule%20instruction%20saars%20as%20one%2C%20Back%20to%20top%20Scheduler.%20%3CBR%20%2F%3E%3CBR%20%2F%3EResults%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20551px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66944i208E323DE34AD5CE%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CI%3E%20Combining%20code%20snippet%20above%2C%20we%20can%20and%20also%20implement%20sych%2C%20such%20as%20timer%2C%20Event%20Event%2C%20Fork-Join%20etc..%20We%20are%20are%20not%20unfold%20all%20of%20them.%20%3C%2FI%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CI%3E%20%3C%2FI%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%3CA%3E%20%3CSTRONG%3ECountdown%3A%20TaskAsync%20Version%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20.%20%3CA%3E%20%3CSTRONG%3ECode%3A%20TaskAsyncCountDown%3C%2FSTRONG%3E%20%3C%2FA%3E%20.%20%20%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20async%20Task%20TaskAsyncCountDown(int%20count%2C%20string%20flag%20%3D%20%22%22)%0A%7B%0A%20%20%20%20for%20(int%20i%20%3D%20count%3B%20i%20%26gt%3B%3D%200%3B%20i--)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LogToTUnityConsole(i%2C%20flag)%3B%0A%20%20%20%20%20%20%20%20await%20Task.Delay(1000)%3B%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20version%20of%20the%20code%20is%20very%20like%20to%20the%20coroutine%20the%20version%3A%20same%20CPS%20turn%2C%20only%20use%20instead%20of%20yield%20return%2C%20and%20use%20task%20instead%20if%20Coroutine.%20%3CBR%20%2F%3EAnother%20way%20to%20same%20same%20is%20%22async%20void%20function%22.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20async%20void%20VoidAsyncCountDown(int%20count%2C%20string%20flag%20%3D%20%22%22)%0A%7B%0A%20%20%20%20for%20(int%20i%20%3D%20count%3B%20i%20%26gt%3B%3D%200%3B%20i--)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LogToTUnityConsole(i%2C%20flag)%3B%0A%20%20%20%20%20%20%20%20await%20Task.Delay(1000)%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EIn%20community%20article%20s%20and%20multiple%20smh%20o%20sy'n%20documents%20we%20se-ed%2C%20in%20add%20to%20use%20the%20void%20function%20as%20an%20event%20handler%2C%20%22async%20void%20functions%22%20ai%20%3CSTRONG%3Eadlly%20not%20adlly.%3C%2FSTRONG%3E%20Because%20the%20functions%20do%20serdo%20return%20anythingtask%20task%20task%20r%20This%20code%20snippet%20is%20is%20a%20negative%20ohere%2C%20for%20your%20reference.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%3CA%3E%3CSTRONG%3EExecution%3A%3C%2FSTRONG%3E%20%3C%2FA%3E%20.%20%3CSTRONG%3ETaskAsync%20CountDown.%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CSTRONG%3E%20%3CI%3EExecution%20in%20main%20thread%20scheduling%3C%2FI%3E%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20.%20What%20we%20do%20don%20need%20to%20do%20ysto%20for%20this%20astsytask%20task%20and%20just%20start%20it%2C%20the%20task%20object%20async%20async%20sys%20sholylyeth%2C%20we%20sy'n%20call%20an%20async%20the%20function%20the%20first%20sire.%20Although%20this%20will%20be%20in%20the%20visual%20studio%20with%20a%20green%20wavy%20line%20warning%2C%20but%20this%20is%20actually%20a%20legitimate%20use.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CI%3E%20%3C%2FI%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20883px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66946i740A812918D8B53D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EIn%20order%20to%20avoid%20the%20thoita%20of%20the%20noise%2C%20returned%20be%20by%20a%20temporary%20variable%20without%20any%20synd.%20Like%20what%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evoid%20Start()%0A%7B%0A%20%20%20%20stopwatch%20%3D%20new%20System.Diagnostics.Stopwatch()%3B%0A%20%20%20%20stopwatch.Start()%3B%0A%20%20%20%20var%20_%20%3D%20TaskAsyncCountDown(3%2C%20%22BasicAsyncCall%22)%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3EResult%20Cecte%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66948i97C84ED199A5E1C3%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20as%20you%20sy'n%20see%2C%20await%20in%20the%20async%20function%20ses%20stask%2C%20sydd%20task%20will%20be%20thin%20each%20time%20the%20sy'n%20sy'n%20sydd%20yn%20rheims.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CI%3E%20If%20we%20want%20change%20this%20behavior%2C%20we%20can%20the%20modify%20taskAsyncCountDown%20Function%2C%20modify%20the%20sleuth%20code.%20%3C%2FI%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20async%20Task%20TaskAsyncCountDown(int%20count%2C%20string%20flag%20%3D%20%22%22)%0A%7B%0A%20%20%20%20for%20(int%20i%20%3D%20count%3B%20i%20%26gt%3B%3D%200%3B%20i--)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LogToTUnityConsole(i%2C%20flag)%3B%0A%20%20%20%20%20%20%20%20await%20Task.Delay(1000).ConfigureAwait(false)%3B%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66949iB3B9FAE2E988C170%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CI%3E%20Await%20Scheduler%20uses%20the%20thread%20pool%2C%20and%20each%20step%20of%20await%20is%20no%20longer%20be%20forced%20to%20return%20to%20game's%20main%20thread%2C%20what%20o%20seoedt.%20In%20this%20case%20operations%20to%20some%20objects%20that%20need%20be%20thin%20main%20thread%20(such%20as%20texture%20Read-write)%20will%20fail.%20CPU%20siewer%20scomputed%20to%20be%20scheduled%20in%20the%20thread%20pool%2C%20in%20order%20to%20unblock%20swyr%2C%20what%20can%20make%20same%20effect%20as%20sworkloadin%20in%20update%20()%20method.%20%3C%2FI%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%3CSTRONG%3E%20%3CI%3EExecute%20in%20parallel%20from%20main%20thread%3C%2FI%3E%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20.%20Using%20the%20Task%2C%20multiple%20operations%20turn%20and%20return%20to%20the%20main%20thread.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evoid%20Start()%0A%7B%0A%20%20%20%20stopwatch%20%3D%20new%20System.Diagnostics.Stopwatch()%3B%0A%20%20%20%20stopwatch.Start()%3B%0A%20%20%20%20var%20a%20%3D%20TaskAsyncCountDown(3%2C%20%22BasicAsyncCallA%22)%3B%0A%20%20%20%20var%20b%20%3D%20TaskAsyncCountDown(3%2C%20%22BasicAsyncCallB%22)%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66951iFE564B31E30A4500%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%3CSTRONG%3E%20%3CI%3EExecute%20in%20linear%20from%20main%20thread%3C%2FI%3E%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20.%20Like%20the%20Coroutine%2C%20async%20methods%20can%20be%20linked%20one%20after%20another%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evoid%20Start()%0A%7B%0A%20%20%20%20stopwatch%20%3D%20new%20System.Diagnostics.Stopwatch()%3B%0A%20%20%20%20stopwatch.Start()%3B%0A%20%20%20%20var%20t%20%3D%20TaskAsyncCountDownSeq(3%2C%20%22BasicAsyncCallA%22%2C%20%22BasicAsyncCallB%22)%3B%0A%7D%0A%0Apublic%20async%20Task%20TaskAsyncCountDownSeq(int%20countDown%2C%20params%20string%5B%5D%20flags)%0A%7B%0A%20%20%20%20foreach%20(var%20flag%20in%20flags)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20await%20TaskAsyncCountDown(3%2C%20flag)%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3ESyllain%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66953i0D65A25642E27452%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CSTRONG%3E%20%3CI%3Eexecutes%20in%20thread%20ed%20if%3C%2FI%3E%20%3C%2FSTRONG%3E%20you%20are%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20a'r%20careful%20enough%2C%20you%20will%20find%20that%20out%20in%20ConfigureAwait%20(false)%20code%20id%20id%20id%20value%20in%20first%20line%20was%201%20.%20%3CBR%20%2F%3E%3CBR%20%2F%3EMae'r%20first%20await%20keyword%2C%20operations%20will%20execute%20on%20the%20thread's%20thread%20in%20async%20alyss.%20In%20extreme%20cases%2C%20if%20an%20asydyn%20sydd%20sylls%20CPU-intensive%20or%20long-run-run-swisio%20operations%20at%20the%20entrance%2C%20thread%20of%20the%20main%20of%20the%20unity%20will%20will%20still%20be%20blocked.%20This%20can%20be%20optimized%20in%20the%20following%20way%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evoid%20Start()%0A%7B%0A%20%20%20%20stopwatch%20%3D%20new%20System.Diagnostics.Stopwatch()%3B%0A%20%20%20%20stopwatch.Start()%3B%0A%20%20%20%20var%20_%20%3D%20Task.Run(()%20%3D%26gt%3B%20TaskAsyncCountDown(3%2C%20%22BasicAsyncCall%22))%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66954i87DF1E11C1DA7C3A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3EAt%20this%20point%20the%20function%20entry%20is%20by%20the%20thread%20pool%20pool.%20Since%20the%20thread%20in%20the%20thread%20pool%20does%20not%20synchronize%20the%20context%2C%20all%20subsequent%20calls%20will%20randomly%20use%20the%20available%20threads%20in%20the%20thread%20pool.%20Even%20if%20you%20forced%20it%20by%20setting%20parameter%20of%20.%20ConfigureAwait%20()%20to%20true%2C%20it%20will%20not%20help.%20Please%20pay%20attention%20to%20the%20impact%20of%20such%20calls%3A%20you%20will%20will%20lose%20the%20viewy%20to%20go%20return%20to%20the%20main%20thread%20contexts.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CI%3EIs%20there%20ysly%20way%20to%20have%20swerrun%20context%20and%20run%20steps%20in%20thread%20pool%3F%3C%2FI%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CSTRONG%3E%20%3CI%3EWith%20Execute%20Mixed%20Scheduler.%3C%2FI%3E%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20In%20order%20to%20address%20the%20questions%20raised%20by%20the%20end%20of%20the%20previous%20section%2C%20I%20have%20designed%20this%20case%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20Assume%20this%20de%20sare%20is%20demanded%20in%20a%20business%20logic%3A%3C%2FP%3E%0A%3CP%3EA.%20computing%20n!%20(n-150)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EB.%201%20seconds%20waiting.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EC.%20output.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESo%20we%20get%20the%20slecode%20(in%20order%20to%20the%20execution%20of%20the%20load%2C%20I%20added%20sleep%20time%20time%20in%20loops)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20void%20CPUBigHead(int%20n)%0A%7B%0A%20%20%20%20double%20result%20%3D%201%3B%0A%20%20%20%20LogToTUnityConsole(result%2C%20%24%22n!(n%3D%7Bn%7D)%20start%22)%3B%0A%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%26lt%3B%3D%20n%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20result%20%3D%20result%20*%20i%3B%0A%20%20%20%20%20%20%20%20Thread.Sleep(10)%3B%0A%20%20%20%20%7D%0A%20%20%20%20LogToTUnityConsole(result%2C%20%24%22n!(n%3D%7Bn%7D)%20stop%22)%3B%0A%20%20%20%20Thread.Sleep(1000)%3B%0A%0A%20%20%20%20%2F%2FTexture%20change%20logic%20puts%20here%20%0A%0A%20%20%20%20LogToTUnityConsole(result%2C%20%24%22n!(n%3D%7Bn%7D)%20output%22)%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66956i5F6909DBEF711B37%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3EN!%20calculation%20is%20a%20CPU-intensive%20operation.%20We's%20want%20it%20executing%20in%20thread%20other%2C%20and%20y%20output%20the%20results%20in%20the%20UI%20layer%20texture%20changes%2C%20you%20need%20to%20manipulate%20the%20thread%20id%20is%201.%20%3CBR%20%2F%3EOn%20this%20basis%2C%20I%20modified%20sync%20code%20into%20async%20to%20achieve.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20async%20Task%20CPUBigHeadAsync(int%20n)%0A%7B%0A%20%20%20%20double%20result%20%3D%201%3B%0A%20%20%20%20await%20Task.Run(()%20%3D%26gt%3B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LogToTUnityConsole(result%2C%20%24%22n!(n%3D%7Bn%7D)%20start%22)%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%26lt%3B%3D%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20result%20%3D%20result%20*%20i%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Thread.Sleep(10)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20LogToTUnityConsole(result%2C%20%24%22n!(n%3D%7Bn%7D)%20stop%22)%3B%0A%20%20%20%20%7D)%3B%20%2F%2FScheduler%20will%20contiue%20execution%20in%20main%20thread%20here%20%0A%0A%20%20%20%20await%20Task.Delay(1000)%3B%0A%20%20%20%20%2F%2FTexture%20change%20logic%20puts%20here%20%0A%20%20%20%20LogToTUnityConsole(result%2C%20%24%22n!(n%3D%7Bn%7D)%20output%22)%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3EGet%20the%20sleat%20s%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66958i13261F43E4F2C1A9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20Can%20see%20this%20we%20se%20we%20sit%20cpu-siel%20operations%20are%20not%20running%20in%20the%20main%20thread.%20This%20change%20di%20ensures%20aly%20smooth%20sydd%20syned.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%3CA%3E%20%3CSTRONG%3EComparison%20summary%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%20.%20From%20the%20above%20examples%2C%20we%20can%20see%20this%20new%20Task%20API%20%2F%20async%20syntax%20brings%20new%20ar%20diedifor%20unity.%20Old%20Coroutine%20sydd%20sy'n%20sy'r%20small%20synd%2C%20but%20it%20hasles%20power%20to%20control%20all-part%20thread%20in%20thread%20freely.%20With%20new%20Task%20API%20you%20can%20easily%20do%20operations%20in%20a%20thread%20pool%2C%20and%20make%20calculations%20easier.%20%3CBR%20%2F%3E%3CBR%20%2F%3EIn%20addition%2C%20Task%2Fawait%20also%20has%20some%20others.%3C%2FP%3E%0A%3CP%3E%C2%B7%20Highly%20integrated%20with%20the%20UWP%20API%2C%20you%20can%20use%20the%20simple%20await%2FAsTask%20()%20extension%20method%20to%20convert.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%C2%B7%20To%20maintain%20async%20job's%20lifetime%20at%20the%20application%20level.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%C2%B7%20Controltimer%2Fmanual-www%20op%20of%20the%20execution%20tree%20by%20OkToke.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%C2%B7%20Fast%20package%20of%20event%2Fthread%2FIAsyncResult%2C%20provides%20a%20simple%20unified%20room%20call.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EIn%20this%20article%20we%20are%20not%20don%20go%20to%20explain%20all%20of%20these%20use.%20If%20you%20have%20havens%20to%20learn%20more%2C%20see%20-ERR%3AREF-NOT-FOUND-Asyd%20programming%20Patterns.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CI%3E%20As%20i'r%20tha%20ei%20fod%20at%20the%20a'%20sane%2C%20new%20aw%2C%20task%2Fasync%20will%20exist%20with%20co%20sides%20by%2C%20and%20thee%20tha%20ei%20tha%20iad%20willse%20iad%20oedd.%20So%20how%20to%20interactive%20one%20to%20another%20%3F%20%3C%2FI%3ETask%20Coroutine%20interactive%20sinteractive%20with%20each%20other%2C%20means%20that%20we%20are%20able%20to%20Coroutine%20call%20the%20Async%20method%2C%20as%20well%20as%20the%20call%20Coroutine%20in%20the%20async%20method.%20%3CI%3E%20%3C%2FI%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CA%3E%20%3CSTRONG%3E%20Task%20Coroutine%20Interactive%20%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3EExamples%20of%20the%20concurrent%20execution%20and%20no%20vis%20from%20basic%20calls%2C%20here%20we%20show%20you%20how%20to%20call%20one%20and%20the%20next%20next%20one%20the%20life%20of%20the%20work.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%3CA%3E%3CSTRONG%3EAsync%20Method%20Calls%20a%20Coroutine%20And%20Wait%20for%20The%20Dion.%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CA%3E%20%3CSTRONG%3ECode%3A%20TaskAwaitACoroutine.%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20If%20you%20want%20to%20call%20a%20Coroutine%20y%20in%20async%20methods%2C%20we%20need%20to%20wrap%20the%20coroutine%20to%20a%20simple%20task.%20%3CBR%20%2F%3E%3CBR%20%2F%3ETha%20are%20no%20callbacks%20or%20wait%20handle%20yn%20yild%20instruction%20class%20of%20unity.%20So%20we%20need%20a%20parent's%20Coroutine%20method%20to%20monitor%20work%20of%20the%20target%20Coroutine.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EIEnumerator%20tempCoroutine(IEnumerator%20coro%2C%20System.Action%20afterExecutionCallback)%0A%7B%0A%20%20%20%20yield%20return%20coro%3B%0A%20%20%20%20afterExecutionCallback()%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3EAt%20this%20point%20we%20sy'n%20use%20the%20taskcompeletionSource%20to%20the%20sub-e%20Coroutine%20into%20a%20Task.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20async%20Task%20TaskAwaitACoroutine()%0A%7B%0A%20%20%20%20await%20TaskAsyncCountDown(2%2C%20%22precoro%22)%3B%0A%20%20%20%20var%20tcs%20%3D%20new%20System.Threading.Tasks.TaskCompletionSource%3C%2FCODE%3E%3COBJECT%3E%3CCODE%3E()%3B%0A%0A%20%20%20%20StartCoroutine(%0A%20%20%20%20%20%20%20%20tempCoroutine(%0A%20%20%20%20%20%20%20%20%20%20%20%20CoroutineCountDown(3%2C%20%22coro%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20()%20%3D%26gt%3B%20tcs.TrySetResult(null)))%3B%0A%0A%20%20%20%20await%20tcs.Task%3B%0A%20%20%20%20await%20TaskAsyncCountDown(2%2C%20%22postcoro%22)%3B%0A%7D%3C%2FCODE%3E%3C%2FOBJECT%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3EExecution%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evoid%20Start()%0A%7B%0A%20%20%20%20stopwatch%20%3D%20new%20System.Diagnostics.Stopwatch()%3B%0A%20%20%20%20stopwatch.Start()%3B%0A%20%20%20%20var%20t%20%3D%20TaskAwaitACoroutine()%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20467px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66959i787A6B3B2E7F9702%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3EAs%20you%20you%20can%20see%2C%20after%20the%20execution%20of%20the%20execution%20of%20the%20execution%20of%20the%20planecountDown%20%22coro%22%2C%20the%20scheduler%20started%20to%20the%20task%20task%20athAsyncCountDown%20%22postcoro%22%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%3E%20%3CSTRONG%3ECoroutine%20Call%20Task%20Task%20and%20wait%20for%20the%3C%2FSTRONG%3E%20for%20the%20%3CBR%20%2F%3E%20sho%20i'r%20atharoutine%20call%20co%2C%20co%20calls%20a%20check%20This%20checking%20is%20managed%20by%20WaitUntil%20class%2C%20what%20is%20also%20a%20yield%20instruction.%20Dispatcher%20will%20will%20sy'r%20execution%20after%20waituntil%20is%20done%2C%20and%20execute%20next%20object%20in%20sequence%20in%20main%20thread.%20%3CBR%20%2F%3E%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20IEnumerator%20CoroutineAwaitATask()%0A%7B%0A%20%20%20%20yield%20return%20CoroutineCountDown(2%2C%20%22pretask%22)%3B%0A%0A%20%20%20%20var%20task%20%3D%20TaskAsyncCountDown(3%2C%20%22task%22)%3B%0A%20%20%20%20yield%20return%20new%20WaitUntil(()%20%3D%26gt%3B%20task.IsCompleted%20%7C%7C%20task.IsFaulted%20%7C%7C%20task.IsCanceled)%3B%0A%20%20%20%20%2F%2FCheck%20task's%20return%3B%20%0A%20%20%20%20if%20(task.IsCompleted)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LogToTUnityConsole(%22TDone%22%2C%20%22task%22)%3B%0A%20%20%20%20%7D%0A%20%20%20%20yield%20return%20CoroutineCountDown(2%2C%20%22posttask%22)%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EExecution%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evoid%20Start()%0A%7B%0A%20%20%20%20stopwatch%20%3D%20new%20System.Diagnostics.Stopwatch()%3B%0A%20%20%20%20stopwatch.Start()%3B%0A%20%20%20%20StartCoroutine(CoroutineAwaitATask())%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66960iA330973183F5C52E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3EYou%20can%20sy'r%20see%2C%20the%20executions%20of%20the%20taskAsyncCountDown%20%22task%22%20Async%20method%2C%20unity%20dispatcher%20synre%20to%20the%20execution%20the%20cocountdown%20%22post%20task%22%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CA%3E%20%3CSTRONG%3Eadvanced%20packaging%3A%20cotask%20helper%3C%2FSTRONG%3E%20%3C%2FA%3E%20code%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20snippets%20snippets%20long%20show%20us%20to%20make%20task%20and%20coroutine.%20So%20do%20we%20have%20a%20way%20of%20simplifying%20this%20process%2C%20makes%20more%20sling%3F%20%3CBR%20%2F%3E%3CBR%20%2F%3EBebewe%20waswere%20using%20the.%20NET%204.6%20subset%20already%20%2C%20wrapping%20operations%20into%20the%20es%20methods%20is%20easy%20very.%20So%20I%20created%20CoTaskHelper%20class.%20To%20simplify%20calling.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20System.Collections%3B%0Ausing%20System.Collections.Generic%3B%0Ausing%20System.Threading.Tasks%3B%0Ausing%20UnityEngine%3B%0A%0Apublic%20static%20class%20CoTaskHelper%0A%7B%0A%0A%0A%20%20%20%20public%20static%20async%20Task%20ExecuteCoroutineAsync(this%20MonoBehaviour%20monoBehavior%2C%20IEnumerator%20coroutine)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20tcs%20%3D%20new%20System.Threading.Tasks.TaskCompletionSource%3C%2FCODE%3E%3COBJECT%3E%3CCODE%3E()%3B%0A%0A%20%20%20%20%20%20%20%20monoBehavior.StartCoroutine(%0A%20%20%20%20%20%20%20%20tempCoroutine(%0A%20%20%20%20%20%20%20%20coroutine%2C%0A%20%20%20%20%20%20%20%20()%20%3D%26gt%3B%20tcs.TrySetResult(null)))%3B%0A%0A%20%20%20%20%20%20%20%20await%20tcs.Task%3B%0A%20%20%20%20%7D%0A%0A%0A%0A%20%20%20%20static%20IEnumerator%20tempCoroutine(IEnumerator%20coro%2C%20System.Action%20afterExecutionCallback)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20yield%20return%20coro%3B%0A%20%20%20%20%20%20%20%20afterExecutionCallback()%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20static%20IEnumerator%20WaitAsCoroutine(this%20Task%20task)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20yield%20return%20new%20WaitUntil(()%20%3D%26gt%3B%20task.IsCompleted%20%7C%7C%20task.IsFaulted%20%7C%7C%20task.IsCanceled)%3B%0A%20%20%20%20%7D%0A%0A%7D%0A%3C%2FCODE%3E%3C%2FOBJECT%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EThe%20code%20in%20last%20section%20can%20be%20simplified%20as.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20async%20Task%20TaskAwaitACoroutineWithEx()%0A%7B%0A%20%20%20%20await%20TaskAsyncCountDown(2%2C%20%22precoro%22)%3B%0A%20%20%20%20await%20this.ExecuteCoroutineAsync(CoroutineCountDown(3%2C%20%22coro%22))%3B%0A%20%20%20%20await%20TaskAsyncCountDown(2%2C%20%22postcoro%22)%3B%0A%7D%0A%0Apublic%20IEnumerator%20CoroutineAwaitATaskWithEx()%0A%7B%0A%20%20%20%20yield%20return%20CoroutineCountDown(2%2C%20%22pretask%22)%3B%0A%20%20%20%20var%20task%20%3D%20TaskAsyncCountDown(3%2C%20%22task%22)%3B%0A%20%20%20%20yield%20return%20task.WaitAsCoroutine()%3B%0A%20%20%20%20LogToTUnityConsole(%22TDone%22%2C%20%22task%22)%3B%0A%20%20%20%20yield%20return%20CoroutineCountDown(2%2C%20%22posttask%22)%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EFor%20clarity%2C%20more%20native%20flavour.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20%20%3CA%3E%20%3CSTRONG%3ESummary.%3C%2FSTRONG%3E%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20In%20this%20article%2C%20we%20briefly%20introduc%20and%20compare%20the%20unity%20native%20coroutine%20programming%20and%20the%20.NET%204.6%20subset%20smh.%20%3CBR%20%2F%3EThai%20theses%20sgours%20sgours%20a%20tha%20iad%20and%20a%20sydd%20a'%20sgofa%20uabym.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EP.S.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20After%20practice%20with%20projects%2C%20I%20have%20written%20a%20better%20a'%20and%20ystyin%20yeild%20instruction.%20Please%20feel%20free%20give%20me%20feedback%20in%20%3A)%3C%2Fimg%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%2F%2F%2F%20%3CSUMMARY%3E%20%0A%2F%2F%2F%20Coroutine%20and%20Task%20Async%20Interactive%20Helper%20%0A%2F%2F%2F%20Based%20on%20artical%20https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2017%2F09%2F01%2Funity-coroutine-tap-en-us%2F%20%0A%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0Apublic%20static%20class%20CoroutineTaskHelper%0A%7B%0A%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%2F%2F%2F%20Translate%20a%20coroutine%20to%20Task%20and%20run.%20It%20needs%20a%20Mono%20Behavior%20to%20locate%20Unity%20thread%20context.%20%0A%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20%2F%2F%2F%20%3C%2FCODE%3E%3COBJECT%3E%3CCODE%3EThe%20Mono%20Behavior%20that%20managing%20coroutines%20%0A%20%20%20%20%2F%2F%2F%20%3CPARAM%20name%3D%22coroutine%22%20%2F%3Ethe%20coroutine%20that%20needs%20to%20run%20%20%0A%20%20%20%20%2F%2F%2F%20%3CRETURNS%3ETask%20that%20can%20be%20await%3C%2FRETURNS%3E%20%0A%20%20%20%20public%20static%20async%20Task%20StartCoroutineAsync(this%20MonoBehaviour%20monoBehavior%2C%20IEnumerator%20coroutine)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20tcs%20%3D%20new%20TaskCompletionSource%3C%2FCODE%3E%3COBJECT%3E%3CCODE%3E()%3B%0A%20%20%20%20%20%20%20%20monoBehavior%0A%20%20%20%20%20%20%20%20.StartCoroutine(%0A%20%20%20%20%20%20%20%20emptyCoroutine(%0A%20%20%20%20%20%20%20%20coroutine%2C%0A%20%20%20%20%20%20%20%20tcs))%3B%0A%20%20%20%20%20%20%20%20await%20tcs.Task%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%2F%2F%2F%20Translate%20a%20YieldInstruction%20to%20Task%20and%20run.%20It%20needs%20a%20Mono%20Behavior%20to%20locate%20Unity%20thread%20context.%20%0A%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20%2F%2F%2F%20%3CPARAM%20name%3D%22monoBehavior%22%20%2F%3EThe%20Mono%20Behavior%20that%20managing%20coroutines%20%0A%20%20%20%20%2F%2F%2F%20%3CPARAM%20name%3D%22yieldInstruction%22%20%2F%3E%20%0A%20%20%20%20%2F%2F%2F%20%3CRETURNS%3ETask%20that%20can%20be%20await%3C%2FRETURNS%3E%20%0A%20%20%20%20public%20static%20async%20Task%20StartCoroutineAsync(this%20MonoBehaviour%20monoBehavior%2C%20YieldInstruction%20yieldInstruction)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20tcs%20%3D%20new%20TaskCompletionSource%3C%2FCODE%3E%3COBJECT%3E%3CCODE%3E()%3B%0A%20%20%20%20%20%20%20%20monoBehavior%0A%20%20%20%20%20%20%20%20.StartCoroutine(%0A%20%20%20%20%20%20%20%20emptyCoroutine(%0A%20%20%20%20%20%20%20%20yieldInstruction%2C%0A%20%20%20%20%20%20%20%20tcs))%3B%0A%20%20%20%20%20%20%20%20await%20tcs.Task%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%2F%2F%2F%20Wrap%20a%20Task%20as%20a%20Coroutine.%20%0A%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20%2F%2F%2F%20%3CPARAM%20name%3D%22task%22%20%2F%3EThe%20target%20task.%20%0A%20%20%20%20%2F%2F%2F%20%3CRETURNS%3EWrapped%20Coroutine%3C%2FRETURNS%3E%20%0A%20%20%20%20public%20static%20CoroutineWithTask%3CSYSTEM.OBJECT%3E%20AsCoroutine(this%20Task%20task)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20coroutine%20%3D%20new%20WaitUntil(()%20%3D%26gt%3B%20task.IsCompleted%20%7C%7C%20task.IsFaulted%20%7C%7C%20task.IsCanceled)%3B%0A%20%20%20%20%20%20%20%20return%20new%20CoroutineWithTask%3COBJECT%3E(task)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%2F%2F%2F%20Wrap%20a%20Task%20as%20a%20Coroutine.%20%0A%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20%2F%2F%2F%20%3CPARAM%20name%3D%22task%22%20%2F%3EThe%20target%20task.%20%0A%20%20%20%20%2F%2F%2F%20%3CRETURNS%3EWrapped%20Coroutine%3C%2FRETURNS%3E%20%0A%20%20%20%20public%20static%20CoroutineWithTask%3CT%3E%20AsCoroutine%3CT%3E(this%20Task%3CT%3E%20task)%0A%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20return%20new%20CoroutineWithTask%3CT%3E(task)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20private%20static%20IEnumerator%20emptyCoroutine(YieldInstruction%20coro%2C%20TaskCompletionSource%3COBJECT%3E%20completion)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20yield%20return%20coro%3B%0A%20%20%20%20%20%20%20%20completion.TrySetResult(null)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20private%20static%20IEnumerator%20emptyCoroutine(IEnumerator%20coro%2C%20TaskCompletionSource%3COBJECT%3E%20completion)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20yield%20return%20coro%3B%0A%20%20%20%20%20%20%20%20completion.TrySetResult(null)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%2F%2F%2F%20Wrapped%20Task%2C%20behaves%20like%20a%20coroutine%20%0A%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20public%20struct%20CoroutineWithTask%3CT%3E%20%3A%20IEnumerator%0A%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20private%20IEnumerator%20_coreCoroutine%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20Constructor%20for%20Task%20with%20a%20return%20value%3B%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3CPARAM%20name%3D%22coreTask%22%20%2F%3ETask%20that%20need%20wrap%20%0A%20%20%20%20%20%20%20%20public%20CoroutineWithTask(Task%3CT%3E%20coreTask)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20WrappedTask%20%3D%20coreTask%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_coreCoroutine%20%3D%20new%20WaitUntil(()%20%3D%26gt%3B%20coreTask.IsCompleted%20%7C%7C%20coreTask.IsFaulted%20%7C%7C%20coreTask.IsCanceled)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20Constructor%20for%20Task%20without%20a%20return%20value%3B%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3CPARAM%20name%3D%22coreTask%22%20%2F%3ETask%20that%20need%20wrap%20%0A%20%20%20%20%20%20%20%20public%20CoroutineWithTask(Task%20coreTask)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20WrappedTask%20%3D%20Task.Run(async%20()%20%3D%26gt%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20await%20coreTask%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20default(T)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_coreCoroutine%20%3D%20new%20WaitUntil(()%20%3D%26gt%3B%20coreTask.IsCompleted%20%7C%7C%20coreTask.IsFaulted%20%7C%7C%20coreTask.IsCanceled)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20The%20task%20have%20wrapped%20in%20this%20coroutine.%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20public%20Task%3CT%3E%20WrappedTask%20%7B%20get%3B%20private%20set%3B%20%7D%0A%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20Task%20result%2C%20if%20it%20have.%20Calling%20this%20property%20will%20wait%20execution%20synchronously.%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20public%20T%20Result%20%7B%20get%20%7B%20return%20WrappedTask.Result%3B%20%7D%20%7D%0A%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20Gets%20the%20current%20element%20in%20the%20collection.%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20public%20object%20Current%20%3D%26gt%3B%20_coreCoroutine.Current%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20Advances%20the%20enumerator%20to%20the%20next%20element%20of%20the%20collection.%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3CRETURNS%3E%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20true%20if%20the%20enumerator%20was%20successfully%20advanced%20to%20the%20next%20element%3B%20false%20if%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20the%20enumerator%20has%20passed%20the%20end%20of%20the%20collection.%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3C%2FRETURNS%3E%20%0A%20%20%20%20%20%20%20%20public%20bool%20MoveNext()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20_coreCoroutine.MoveNext()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3CSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20Sets%20the%20enumerator%20to%20its%20initial%20position%2C%20which%20is%20before%20the%20first%20element%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20in%20the%20collection.%20%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%3C%2FSUMMARY%3E%20%0A%20%20%20%20%20%20%20%20public%20void%20Reset()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20_coreCoroutine.Reset()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FT%3E%3C%2FT%3E%3C%2FT%3E%3C%2FOBJECT%3E%3C%2FOBJECT%3E%3C%2FT%3E%3C%2FT%3E%3C%2FT%3E%3C%2FT%3E%3C%2FOBJECT%3E%3C%2FSYSTEM.OBJECT%3E%3C%2FCODE%3E%3C%2FOBJECT%3E%3C%2FOBJECT%3E%3C%2FOBJECT%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-316851%22%20slang%3D%22zh-CN%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3Cpre%20class%3D%22lia-code-sample%20language-csharp%22%3E%3Ccode%3Epublic%20async%20Task%20TaskAwaitACoroutine()%20%0A%7B%20%0Aawait%20TaskAsyncCountDown(2%2C%20%22precoro%22)%3B%20%0Avar%20tcs%20%3D%20new%20System.Threading.Tasks.TaskCompletionSource%3COBJECT%3E()%3B%20%0A%0AStartCoroutine(%20%0AtempCoroutine(%20%0ACoroutineCountDown(3%2C%20%22coro%22)%2C%20%0A()%20%3D%26gt%3B%20tcs.TrySetResult(null)))%3B%20%0A%0Aawait%20tcs.Task%3B%20%0Aawait%20TaskAsyncCountDown(2%2C%20%22postcoro%22)%3B%20%0A%7D%20%3C%2FOBJECT%3E%3C%2Fcode%3E%3C%2Fpre%3E%3Cpre%20class%3D%22lia-code-sample%20language-csharp%22%3E%3Ccode%3Evoid%20Start()%20%0A%7B%20%0Astopwatch%20%3D%20new%20System.Diagnostics.Stopwatch()%3B%20%0Astopwatch.Start()%3B%20%0AStartCoroutine(CoroutineCountDown(3%2C%20%22BasicCoCall%22))%3B%20%0A%7D%20%3C%2Fcode%3E%3C%2Fpre%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20published%20on%20MSDN%20on%20Sep%2001%2C%202017%20(Chinese%20version)%20Background%20A%20couple%20of%20weeks%20ago%2C%20Unity%20has%20released%20a%20new%20version%2C%202017.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3Cpre%20class%3D%22lia-code-sample%20language-csharp%22%3E%3Ccode%3Epublic%20async%20Task%20TaskAwaitACoroutine()%20%0A%7B%20%0Aawait%20TaskAsyncCountDown(2%2C%20%22precoro%22)%3B%20%0Avar%20tcs%20%3D%20new%20System.Threading.Tasks.TaskCompletionSource%3COBJECT%3E()%3B%20%0A%0AStartCoroutine(%20%0AtempCoroutine(%20%0ACoroutineCountDown(3%2C%20%22coro%22)%2C%20%0A()%20%3D%26gt%3B%20tcs.TrySetResult(null)))%3B%20%0A%0Aawait%20tcs.Task%3B%20%0Aawait%20TaskAsyncCountDown(2%2C%20%22postcoro%22)%3B%20%0A%7D%20%3C%2FOBJECT%3E%3C%2Fcode%3E%3C%2Fpre%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-316851%22%20slang%3D%22zh-CN%22%3E%3CLINGO-LABEL%3Euwp%20async%20await%20tap%20coroutine.%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

First published on MSDN on Sep 01, 2017


Background



A couple of weeks ago, Unity has released a new version, 2017.1. In this version, the most anticipated feature, TAP and Async-await support, finally came out as a beta feature, with .NET 4.6 Experimental Equivalent.
This is not a coincidence. With the Microsoft HoloLens and Windows MR platform strategic layout gradually being mature, many of Unity developers began to study how to interact with the UWP API to obtain the necessary support from windows platform.

As a 3D scripting engine, Unity 5.X and earlier provide only limited. NET 2.0-4. x, c # 4 support. To access a comprehensive asynchronous API still requires a lot of adaptive work. If you are unfamiliar with UWP and TAP the underlying implementations, the developer is hard to port TAP into Unity existing Update Check/Coroutine model development process. In the other hand, experienced UWP developers are also eager to XR world,, but are suffering from lacking async-await/TAP , misunderstanding Coroutine asynchronous model. (I was one of the struggled member.)

Now, with the constant maturity of .net standard libs, which is trying to smooth all platform differences. With efforts from Unity, Microsoft and geeks in community , Unity developers and UWP developers are finally able to develop using the Coroutine and TAP at the same time. That is the kind of news makes me laugh out loud in my dream.

While celebrating, we still should realize that this brings new challenges. We update the function is not to all overturn everything to start again, whetheryou are a Unity master developer, you may have projects that need porting to the UWP platform, or you are a novice of unity but a UWP developer, you may want to upgrade your 2D XAML application to Windows MR platform. Or, if you are completely new to both,just want a lot of Unity and UWP code examples find your App technical feasibility. No matter who you are, , you will eventually encounter this problem: how to interact with Unity Coroutine and. Net TAP correctly.

This article aims to provide you with a clear reference, or to be inspired by the tools provided in this article, to provide better productivity for later work and study.



Environment Preparation



Software installation

When I was writing this article, I was using:

o Windows 10 Creator Update 15063

 

o Unity 2017.1

 

o Visual Studio 2017.2




As most of the code snippets only dependents on Unity Editor Console printout, you can have use Unity 2017.1 to proceed.



Project Setting Up
The sample project needs to configure your Unity project to start .NET 4.6 and async support. If you are not familiar with this, it is recommended that you follow the steps below to set up your new project.


1. Start Unity

 

2. Click on the "New" icon, enter the project name "AsyncExample"


3. Click the "Create Project" button

 

4. Click on the menu "File-> Build Settings"


5. Click "Player Settings" on the bottom.


Look at your  "Inspector" window, there will be some property tabs. Unfold "Other Settings" tab.

6. Find the "Configuration" property group.  Change the value of "Scripting Runtime Version *"  "Stable (.NET 3.5 Equivalent)" to "Experinmental (.NET 4.6 Equivalent) "


7. Unity Editor needs a complete reboot after your modification, you will see following message.




Select "Restart" to restart the Unity.
After restart, at the same location, properties should be





At this point, Unity Editor obtained the ability to execute c# 6.0, some previously unrecognized class library and syntax will be able to be tested and debugged at the Editor Player, without the need for conditional compilation delay to other platforms to run.

8. In the "Project" window, create a new child folder "Scripts" as child folder of "Assets". Create a new C# script "AsyncExample.cs" in "Scripts"


9. In the "Hierarchy" window, create a new, empty object named "ConsoleScripts"


10. Drag the script you have created into "ConsoleScripts" Game Object







 


Basic codes
Next, we need to prepare for some infrastructure, allows us to create some simple behaviors for our tests.



Logging

Log output code is very simple, it will help us to Unity Console output format information. First of all, we add the following code in the script:

 

 

 

 

using System.Collections;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;

public class AsyncExample : MonoBehaviour
{

    Stopwatch stopwatch;
    void Start()
    {
        stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();
    }

    void LogToTUnityConsole(object content, string flag, [CallerMemberName] string callerName = null)
    {
        UnityEngine.Debug.Log($"{callerName}:\t{flag} {content}\t at { stopwatch.ElapsedMilliseconds } \t in thread {Thread.CurrentThread.ManagedThreadId}");
    }

}

 

 

 

 



Stopwatch object can help us trace calling time. When the Unity scene enters the running state, the stopwatch will start, we can clearly see a thread at a certain time, in which function to do what.




Next, In order to study how multiple asynchronous tasks directly link and call, we often need to pre-design some tasks , we often need to design a typical template. which can be easy to control and the output sequence. My choice was Count Down Task.


Countdown: Coroutine Version

Code: CoroutineCountDown

 

 

 

 

public IEnumerator CoroutineCountDown(int count, string flag = "")
{
    for (int i = count; i >= 0; i--)
    {
        LogToTUnityConsole(i, flag);
        yield return new WaitForSeconds(1);
    }
}

 

 

 

 


If you are a beginner of Unity, you can see this is a yield function returns a enumerator, by your c# experience. Unity can use IEnumerator returned this by method, which can populate sequence of YieldInstruction objects (WaitForSeconds). This sequence is consumed by dispatcher in the base class, MonoBehavior. In each of the YieldInstruction consumption, when the asynchronous operation is complete, the dispatcher will join execution back to the main thread of Unity and calls the IEnumerator MoveNext() method to get the next YieldInstruction.
Execution: CoroutineCountDown

Execute separately

Put the following code in the Start method as below, we can run game in Editor and get console output






You can see, in the logs, execution thread is always Thread1, count down by 1, every 1 second.
Execute in parallel

As this code snippet runs asynchronously, in Unity in this method execution will not affect another operation executed in main thread. It also does not prevent multiple execution..

Like:

 

 

 

 

void Start()
{
    stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();
    StartCoroutine(CoroutineCountDown(3, "BasicCoCallA"));
    StartCoroutine(CoroutineCountDown(3, "BasicCoCallB"));
}

 

 

 

 




When we called twice in a row, we'll see the two tasks transformed themselves into slices, and weaved in the execution of the main thread.





Execution in linear

If two or more Coroutines require convergence (including other process controls, such as if / switch / loop), an additional parent Coroutine is required to be scheduled in Unity. We can create the following code:

 

 

 

 

void Start()
{
    stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();
    StartCoroutine(CoroutineCountDownSeq(3, "BasicCoCallA", "BasicCoCallB"));
}

public IEnumerator CoroutineCountDownSeq(int countDown, params string[] flags)
{
    foreach (var flag in flags)
    {
        yield return StartCoroutine(CoroutineCountDown(3, flag));
    }
}

 

 

 

 


You can see that parent method enumerates a IEnumerator waiting YieldInstructions (this time UnityEngine.Coroutine class) , so dispatcher can schedule YieldInstructions sequences as one, back to top Scheduler.

Results:





Combining code snippet above, we can also implement complex patterns, such as Timer, Event Recognizer, Fork-Join etc.. We are not unfold all of them here.



Countdown: TaskAsync Version

Code :TaskAsyncCountDown

 

 

 

 

public async Task TaskAsyncCountDown(int count, string flag = "")
{
    for (int i = count; i >= 0; i--)
    {
        LogToTUnityConsole(i, flag);
        await Task.Delay(1000);
    }
}

 

 

 

 



This version of the code is very similar to the Coroutine version: same CPS transformation, only using await instead of yield return, and using Task instead if Coroutine.
Another way to achieve same is "async void function".

 

 

 

 

public async void VoidAsyncCountDown(int count, string flag = "")
{
    for (int i = count; i >= 0; i--)
    {
        LogToTUnityConsole(i, flag);
        await Task.Delay(1000);
    }
}

 

 

 

 


In community articles and multiple versions of the documents we mentioned, In addition to using the void function as an event handler, "async void functions" are strongly not recommended . Because the functions dose not return anything Task scheduler can access. This code snippet is presented as a negative example here, for your reference.

Execution: TaskAsyncCountDown

Execution in main thread scheduling

When we do not need to do anything for this asynchronous task and just start it, the Task object async method returns is relatively meaningless, we can call an async function the in the Start function directly. Although this will be in the Visual Studio with a green wavy line warning, but this is actually a legitimate usage.







In order to avoid the occurrence of such noise, returned could be received by a temporary variable without any operation on it. Like what:

 

 

 

 

void Start()
{
    stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();
    var _ = TaskAsyncCountDown(3, "BasicAsyncCall");
}

 

 

 

 



Result just like Coroutine



As you can see, await in the async functions use the default Task choreographer, completed Task will be executed each time the County asked after conversion back to the Unity of the main thread.



If we want to change this behavior, we can modify the TaskAsyncCountDown Function, modify the following code

 

 

 

 

public async Task TaskAsyncCountDown(int count, string flag = "")
{
    for (int i = count; i >= 0; i--)
    {
        LogToTUnityConsole(i, flag);
        await Task.Delay(1000).ConfigureAwait(false);
    }
}

 

 

 

 

 





Await Scheduler uses the thread pool, and each step of await is no longer be forced to return to game's main thread, which might improve execution speed. In this case operations to some objects that need be operate in main thread (such as Texture Read-write) will fail.CPU intensive computation are recommended to be scheduled in the thread pool, in order to unblock render updates, which can make same effect as reduce workload in Update() method.



Execute in parallel from main thread

Using the Task, multiple operations can start and return to the main thread.

 

 

 

 

void Start()
{
    stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();
    var a = TaskAsyncCountDown(3, "BasicAsyncCallA");
    var b = TaskAsyncCountDown(3, "BasicAsyncCallB");
}

 

 

 

 







Execute in linear from main thread

Like the Coroutine, async methods can be linked one after another:

 

 

 

 

void Start()
{
    stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();
    var t = TaskAsyncCountDownSeq(3, "BasicAsyncCallA", "BasicAsyncCallB");
}

public async Task TaskAsyncCountDownSeq(int countDown, params string[] flags)
{
    foreach (var flag in flags)
    {
        await TaskAsyncCountDown(3, flag);
    }
}

 

 

 

 


>Execution results





Execute in thread pooling



If you are careful enough, you will find out that in ConfigureAwait(false) code execution result, thread id value in first line was 1 .

Before the first await keyword, operations will execute on the caller's thread in typical async executions. In extreme cases, if an asynchronous function executes CPU-intensive or long-running synchronous IO operations at the entrance, main thread of Unity will still be blocked. This can be optimized in the following way:

 

 

 

 

void Start()
{
    stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();
    var _ = Task.Run(() => TaskAsyncCountDown(3, "BasicAsyncCall"));
}

 

 

 

 




At this point the function entry is called by the thread pool scheduler. Since the thread in the thread pool does not synchronize the context, all subsequent calls will randomly use the available threads in the thread pool. Even if you forced it by setting parameter of .ConfigureAwait () to true, it would not help. Please pay attention to the potential impact of such calls: you will lose the ability to go return to the main thread contexts.





Is there another way to have synchronization context and run steps in thread pool?

Execute with Mixed Scheduler

In order to address the questions raised by the end of the previous section, I have designed this use case:

Assume that following steps are demanded in a business logic:

a. computing n! (n=150)

 

b. 1 seconds waiting

 

c. output




So we get the following code (in order to expand the execution load, I added sleep time in loops):

 

 

 

 

public void CPUBigHead(int n)
{
    double result = 1;
    LogToTUnityConsole(result, $"n!(n={n}) start");
    for (int i = 1; i <= n; i++)
    {
        result = result * i;
        Thread.Sleep(10);
    }
    LogToTUnityConsole(result, $"n!(n={n}) stop");
    Thread.Sleep(1000);

    //Texture change logic puts here 

    LogToTUnityConsole(result, $"n!(n={n}) output");
}

 

 

 

 





n! calculation is a CPU-intensive operation. We want it executing in other threads, and output the results in the UI layer called texture changes, you need to manipulate the thread Id is 1.
On this basis, I modified synchronized code into async to achieve.

 

 

 

 

 

public async Task CPUBigHeadAsync(int n)
{
    double result = 1;
    await Task.Run(() =>
    {
        LogToTUnityConsole(result, $"n!(n={n}) start");
        for (int i = 1; i <= n; i++)
        {
            result = result * i;
            Thread.Sleep(10);
        }
        LogToTUnityConsole(result, $"n!(n={n}) stop");
    }); //Scheduler will contiue execution in main thread here 

    await Task.Delay(1000);
    //Texture change logic puts here 
    LogToTUnityConsole(result, $"n!(n={n}) output");
}

 

 

 

 



> Get the following results:



Can see that we make sure that CPU-intensive operations are not running in the main thread. This modification ensures a relatively smooth rendering update.



Comparison summary
From the above examples, we can see that the new Task API / async syntax brings new optimization possibilities for Unity. Old Coroutine mechanisms can be logically split into small sections, but it have less power to control each part running in thread freely. With new Task API you can easily do operations in a thread pool, and make calculations easier.

In addition, Task/await also has some other advantages

· Highly integrated with the UWP API, you can use the simple await/AsTask () extension method to convert.

 

· To maintain async job's lifetime at the Application level.。

 

· Control timer/manual interruption of the execution tree by CancellationToke

 

· Fast package of Event/thread/IAsyncResult, provides a simple unified calling pattern.




In this article we are not going to explain all of these usage. If you have needs to learn more, see Asynchronous Programming Patterns



As I mentioned at the beginning of this article, new member, Task/async will exist with Coroutine side by side, and the ecosystem will need them interactive with each other. So how to interactive one to another ?

Task Coroutine Interactive

>interactive with each other,means that we are able to Coroutine call the Async method, as well as the calling Coroutine in the Async method.
Examples of concurrent execution and no different from basic calls, here we show you how to call one another and calling the next one after the implementation of process implementation.



Async Method Calls a Coroutine And Wait for Completion

Code: TaskAwaitACoroutine

If you want to call a Coroutine and in the async methods, we need to wrap the Coroutine implementation to a simple Task.

There are no callbacks or wait handle in YeildInstruction class of Unity. So we need a parent's Coroutine method to monitor completion of the target Coroutine.

 

 

 

 

IEnumerator tempCoroutine(IEnumerator coro, System.Action afterExecutionCallback)
{
    yield return coro;
    afterExecutionCallback();
}

 

 

 

 



At this point we can use the TaskCompeletionSource to encapsulate Coroutine into a Task

 

 

 

 

public async Task TaskAwaitACoroutine()
{
    await TaskAsyncCountDown(2, "precoro");
    var tcs = new System.Threading.Tasks.TaskCompletionSource<object>();

    StartCoroutine(
        tempCoroutine(
            CoroutineCountDown(3, "coro"),
            () => tcs.TrySetResult(null)));

    await tcs.Task;
    await TaskAsyncCountDown(2, "postcoro");
}

 

 

 

 



Execution::

 

 

 

 

void Start()
{
    stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();
    var t = TaskAwaitACoroutine();
}

 

 

 

 







As you can see, after the execution of CoroutineCountDown "Coro", the scheduler started to execute the TaskAsyncCountDown "postcoro"



Coroutine Call Task And wait for completion
Unlike how the Async method call Coroutine, Coroutine...

 

 

 

public IEnumerator CoroutineAwaitATask()
{
    yield return CoroutineCountDown(2, "pretask");

    var task = TaskAsyncCountDown(3, "task");
    yield return new WaitUntil(() => task.IsCompleted || task.IsFaulted || task.IsCanceled);
    //Check task's return; 
    if (task.IsCompleted)
    {
        LogToTUnityConsole("TDone", "task");
    }
    yield return CoroutineCountDown(2, "posttask");
}

 

 

 





Execution:

 

 

 

void Start()
{
    stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();
    StartCoroutine(CoroutineAwaitATask());
}

 

 

 





You can see, after the executions of the TaskAsyncCountDown "task" Async method, Unity dispatcher continued to execute the CoroutineCountDown "posttask"



Advanced packaging: CoTaskHelper

Code snippets above shows us how to make Task and Coroutine call each other. So do we have a way of simplifying this process, makes operations more productive?

Because we were using the. NET 4.6 subset already , wrapping these operations into extension methods is very easy. So I created CoTaskHelper class. to simplify calling.

 

 

using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

public static class CoTaskHelper
{


    public static async Task ExecuteCoroutineAsync(this MonoBehaviour monoBehavior, IEnumerator coroutine)
    {
        var tcs = new System.Threading.Tasks.TaskCompletionSource<object>();

        monoBehavior.StartCoroutine(
        tempCoroutine(
        coroutine,
        () => tcs.TrySetResult(null)));

        await tcs.Task;
    }



    static IEnumerator tempCoroutine(IEnumerator coro, System.Action afterExecutionCallback)
    {
        yield return coro;
        afterExecutionCallback();
    }

    public static IEnumerator WaitAsCoroutine(this Task task)
    {
        yield return new WaitUntil(() => task.IsCompleted || task.IsFaulted || task.IsCanceled);
    }

}

 

 


The code in last section can be simplified as

 

 

public async Task TaskAwaitACoroutineWithEx()
{
    await TaskAsyncCountDown(2, "precoro");
    await this.ExecuteCoroutineAsync(CoroutineCountDown(3, "coro"));
    await TaskAsyncCountDown(2, "postcoro");
}

public IEnumerator CoroutineAwaitATaskWithEx()
{
    yield return CoroutineCountDown(2, "pretask");
    var task = TaskAsyncCountDown(3, "task");
    yield return task.WaitAsCoroutine();
    LogToTUnityConsole("TDone", "task");
    yield return CoroutineCountDown(2, "posttask");
}

 

 


For clarity, more native flavour.



Summary

In this article, we briefly introduce and compare the Unity native Coroutine programming approach and the .NET 4.6 subset + TAP programming approach introduced in the Unity 2017.1, also release and provide examples of code that two methods interact with each other.
Hopefully these examples can inspire readers and become the help of using Unity to increase productivity.

 





P.S.

After practice with projects, I have written a better version, and added YeildInstruction support. Please feel free give me feedback in comments :)

 

 

/// <summary> 
/// Coroutine and Task Async Interactive Helper 
/// Based on artical https://blogs.msdn.microsoft.com/appconsult/2017/09/01/unity-coroutine-tap-en-us/ 
/// </summary> 
public static class CoroutineTaskHelper
{
    /// <summary> 
    /// Translate a coroutine to Task and run. It needs a Mono Behavior to locate Unity thread context. 
    /// </summary> 
    /// <param name="monoBehavior">The Mono Behavior that managing coroutines</param> 
    /// <param name="coroutine">the coroutine that needs to run </param> 
    /// <returns>Task that can be await</returns> 
    public static async Task StartCoroutineAsync(this MonoBehaviour monoBehavior, IEnumerator coroutine)
    {
        var tcs = new TaskCompletionSource<object>();
        monoBehavior
        .StartCoroutine(
        emptyCoroutine(
        coroutine,
        tcs));
        await tcs.Task;
    }

    /// <summary> 
    /// Translate a YieldInstruction to Task and run. It needs a Mono Behavior to locate Unity thread context. 
    /// </summary> 
    /// <param name="monoBehavior">The Mono Behavior that managing coroutines</param> 
    /// <param name="yieldInstruction"></param> 
    /// <returns>Task that can be await</returns> 
    public static async Task StartCoroutineAsync(this MonoBehaviour monoBehavior, YieldInstruction yieldInstruction)
    {
        var tcs = new TaskCompletionSource<object>();
        monoBehavior
        .StartCoroutine(
        emptyCoroutine(
        yieldInstruction,
        tcs));
        await tcs.Task;
    }

    /// <summary> 
    /// Wrap a Task as a Coroutine. 
    /// </summary> 
    /// <param name="task">The target task.</param> 
    /// <returns>Wrapped Coroutine</returns> 
    public static CoroutineWithTask<System.Object> AsCoroutine(this Task task)
    {
        var coroutine = new WaitUntil(() => task.IsCompleted || task.IsFaulted || task.IsCanceled);
        return new CoroutineWithTask<object>(task);
    }

    /// <summary> 
    /// Wrap a Task as a Coroutine. 
    /// </summary> 
    /// <param name="task">The target task.</param> 
    /// <returns>Wrapped Coroutine</returns> 
    public static CoroutineWithTask<T> AsCoroutine<T>(this Task<T> task)
    {

        return new CoroutineWithTask<T>(task);
    }

    private static IEnumerator emptyCoroutine(YieldInstruction coro, TaskCompletionSource<object> completion)
    {
        yield return coro;
        completion.TrySetResult(null);
    }

    private static IEnumerator emptyCoroutine(IEnumerator coro, TaskCompletionSource<object> completion)
    {
        yield return coro;
        completion.TrySetResult(null);
    }

    /// <summary> 
    /// Wrapped Task, behaves like a coroutine 
    /// </summary> 
    public struct CoroutineWithTask<T> : IEnumerator
    {

        private IEnumerator _coreCoroutine;

        /// <summary> 
        /// Constructor for Task with a return value; 
        /// </summary> 
        /// <param name="coreTask">Task that need wrap</param> 
        public CoroutineWithTask(Task<T> coreTask)
        {
            WrappedTask = coreTask;
            _coreCoroutine = new WaitUntil(() => coreTask.IsCompleted || coreTask.IsFaulted || coreTask.IsCanceled);
        }

        /// <summary> 
        /// Constructor for Task without a return value; 
        /// </summary> 
        /// <param name="coreTask">Task that need wrap</param> 
        public CoroutineWithTask(Task coreTask)
        {
            WrappedTask = Task.Run(async () =>
            {
                await coreTask;
                return default(T);
            });
            _coreCoroutine = new WaitUntil(() => coreTask.IsCompleted || coreTask.IsFaulted || coreTask.IsCanceled);
        }

        /// <summary> 
        /// The task have wrapped in this coroutine. 
        /// </summary> 
        public Task<T> WrappedTask { get; private set; }


        /// <summary> 
        /// Task result, if it have. Calling this property will wait execution synchronously. 
        /// </summary> 
        public T Result { get { return WrappedTask.Result; } }


        /// <summary> 
        /// Gets the current element in the collection. 
        /// </summary> 
        public object Current => _coreCoroutine.Current;

        /// <summary> 
        /// Advances the enumerator to the next element of the collection. 
        /// </summary> 
        /// <returns> 
        /// true if the enumerator was successfully advanced to the next element; false if 
        /// the enumerator has passed the end of the collection. 
        /// </returns> 
        public bool MoveNext()
        {
            return _coreCoroutine.MoveNext();
        }

        /// <summary> 
        /// Sets the enumerator to its initial position, which is before the first element 
        /// in the collection. 
        /// </summary> 
        public void Reset()
        {
            _coreCoroutine.Reset();
        }
    }
}