{"id":1324,"date":"2023-10-01T13:14:27","date_gmt":"2023-10-01T05:14:27","guid":{"rendered":"https:\/\/summid.icu\/?p=1324"},"modified":"2023-10-01T13:14:47","modified_gmt":"2023-10-01T05:14:47","slug":"stasktutorials3","status":"publish","type":"post","link":"https:\/\/summid.icu\/index.php\/2023\/10\/01\/stasktutorials3\/","title":{"rendered":"Unity\u5f02\u6b65\u6269\u5c55\u5b9e\u8df5\uff08\u4e09\uff09\u2014\u2014\u4ee5UniTask\u5e93\u4e3a\u53c2\u8003"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p>\u8fd9\u4e00\u7ae0\u6211\u4eec\u6765\u8ba8\u8bba\u5982\u4f55\u8ba9 STask \u53d8\u5f97\u66f4\u9177\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5b9e\u73b0\u4e00\u4e9b\u529f\u80fd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>await STask.Delay(1000); \/\/ \u7b49\u5f851\u79d2\r\n\r\nawait STask.NextFrame(); \/\/ \u7b49\u5f85\u4e0b\u4e00\u5e27\r\n\r\nawait STask.WhenAny(task1, task2); \/\/ \u7b49\u5f85\u5176\u4e2d\u4e00\u4e2a\u4efb\u52a1\u5b8c\u6210\r\n\r\nawait STask.WhenAll(task1, task2); \/\/ \u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\r\n\r\nawait SceneManager.LoadSceneAsync(\"Scene\"); \/\/ \u7b49\u5f85\u573a\u666f\u52a0\u8f7d\u5b8c\u6bd5\r\n\r\nawait AssetBundle.LoadFromFileAsync(\"Asset\"); \/\/ \u7b49\u5f85AB\u5305\u52a0\u8f7d\u5b8c\u6bd5<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u5217\u4e3e\u7684\u63a5\u53e3\u80fd\u88ab\u5927\u81f4\u5206\u4e3a\u4e24\u7c7b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u9700\u8981 STask \u81ea\u5df1\u5b9e\u73b0\u7684\u529f\u80fd\u3002<\/li>\n\n\n\n<li>\u5728 Unity \u63a5\u53e3\u57fa\u7840\u4e0a\u6539\u9020\uff0c\u8ba9\u5b83\u80fd\u88ab await \u5173\u952e\u5b57\u7b49\u5f85\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u540e\u8005\u76f8\u5bf9\u6765\u8bf4\u6bd4\u8f83\u7b80\u5355\uff0c\u6211\u4eec\u53ea\u9700\u5b9e\u73b0\u5bf9\u5e94\u7684 awaiter \u5373\u53ef\u3002\u6bd4\u5982 <code>SceneManager.LoadSceneAsync()<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684\u5bf9\u8c61\u662f <code>AsyncOperation<\/code> \uff0c\u6211\u4eec\u5c31\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static AsyncOperationAwaiter GetAwaiter(this AsyncOperation asyncOperation)\r\n{\r\n\treturn new AsyncOperationAwaiter(asyncOperation);\r\n}\r\n\r\npublic struct AsyncOperationAwaiter : ICriticalNotifyCompletion\r\n{\r\n    public AsyncOperationAwaiter(AsyncOperation asyncOperation);\r\n    \r\n    \/\/ awaiter logic\r\n    public bool IsCompleted;\r\n    public void GetResult();\r\n    public void OnCompleted(Action continuation);\r\n    public void UnsafeOnCompleted(Action continuation);\r\n}<\/code><\/pre>\n\n\n\n<p>\u5185\u90e8\u5b9e\u73b0\u4f9d\u9760\u7684\u662f <code>AsyncOperation<\/code> \u5bf9\u8c61\uff0c\u5b8c\u5168\u6ca1\u6709\u8ddf STask \u6709\u5173\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u8fd9\u4e00\u5757\u4ee3\u7801\u53ef\u4ee5\u968f\u4fbf\u590d\u5236\u7c98\u8d34\u5230\u4efb\u4f55 Unity \u9879\u76ee\u3002<\/p>\n\n\n\n<p>\u800c\u7b2c\u4e00\u7c7b\u529f\u80fd\u5c31\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u5177\u4f53\u4f53\u73b0\u5728\u6211\u4eec\u7684\u5b9e\u73b0\u65b9\u5f0f\u4e0a\u3002\u5f53\u6211\u4eec\u60f3\u5728 Unity \u4e2d\u5b9e\u73b0\u201c\u8ba9\u7a0b\u5e8f\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\uff0c\u518d\u6267\u884c\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u5e76\u4e14\u7b49\u5f85\u65f6\u4e0d\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u201d\u7684\u529f\u80fd\u65f6\uff0c\u4f1a\u8003\u8651\u54ea\u4e9b\u65b9\u6cd5\uff1f<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8ba1\u65f6\u5668\u52a0\u56de\u8c03\u3002<\/li>\n\n\n\n<li>\u5728\u534f\u7a0b\u4e2d\u7b49\u5f85\uff0c\u7136\u540e\u6267\u884c\u4ee3\u7801\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>Task.Delay()<\/code> \u63a5\u53e3\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5c0f\u7f16\u6700\u5f00\u59cb\u60f3\u5230\u7684\u662f\u4e0a\u9762\u4e09\u79cd\u65b9\u6cd5\uff0c\u867d\u7136\u5b83\u4eec\u90fd\u80fd\u8fbe\u5230\u76ee\u7684\uff0c\u4f46\u5404\u81ea\u90fd\u6709\u7f3a\u70b9\uff0c\u6bd4\u5982\u4f7f\u7528\u9ebb\u70e6\uff0c\u6709\u4e0d\u5fc5\u8981\u7684\u7ebf\u7a0b\u5207\u6362\u5f00\u9500\u3002\u90a3\u5982\u679c\u6211\u4eec\u60f3\u505a\u5230\u65e2\u4f7f\u7528\u7b80\u5355\uff0c\u53c8\u6027\u80fd\u4f18\u79c0\uff0c\u5c31\u4e0d\u5f97\u4e0d\u4ecb\u7ecd Unity \u7684 PlayerLoop \u7cfb\u7edf\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PlayerLoopSystem<\/h2>\n\n\n\n<p>Unity \u7684\u751f\u547d\u5468\u671f\u76f8\u4fe1\u5927\u5bb6\u90fd\u5f88\u719f\u6089\u4e86\uff0c\u57282018\u7248\u672c\u540e\uff0cUnity \u5f00\u653e\u4e86\u751f\u547d\u5468\u671f\u63a5\u53e3\uff08<a href=\"https:\/\/docs.unity.cn\/2021.1\/Documentation\/ScriptReference\/LowLevel.PlayerLoop.html\" target=\"_blank\" rel=\"noreferrer noopener\">\u5148\u770b\u6587\u6863\u55f7\uff0c\u8fd9\u662f\u94fe\u63a5<\/a>\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 PlayerLoopSystem \u6765\u4fee\u6539\u751f\u547d\u5468\u671f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class PlayerLoopTest\r\n{\r\n    public struct MyUpdate { }\r\n\r\n    &#91;RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]\r\n    private static void Init()\r\n    {\r\n        var mySystem = new PlayerLoopSystem\r\n        {\r\n            subSystemList = new PlayerLoopSystem&#91;]\r\n            {\r\n                new PlayerLoopSystem\r\n                {\r\n                    updateDelegate = CustomUpdate,\r\n                    type = typeof(MyUpdate),\r\n                }\r\n            }\r\n        };\r\n\r\n        PlayerLoop.SetPlayerLoop(mySystem);\r\n    }\r\n\r\n    private static void CustomUpdate()\r\n    {\r\n        Debug.Log(\"my update.\");\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u9762\u4ee3\u7801\uff0c\u6211\u4eec Unity \u7684\u751f\u547d\u5468\u671f\u4e8b\u4ef6\u5c31\u53ea\u5269\u4e0b\u4e86 <code>CustomUpdate()<\/code> \u3002\u5982\u679c\u6211\u4eec\u60f3\u5728\u539f\u6709\u751f\u547d\u5468\u671f\u4e8b\u4ef6\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u6dfb\u52a0\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff08\u4f2a\u4ee3\u7801\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class PlayerLoopTest\r\n{\r\n    public struct MyUpdate { }\r\n    \r\n    private static void Init()\r\n    {\r\n        PlayerLoopSystem playerLoop = PlayerLoop.GetCurrentPlayerLoop();\r\n        PlayerLoopSystem&#91;] copyList = playerLoop.subSystemList.ToArrar();\r\n        int index = FindLoopSystemIndex(copyList, typeof(PlayerLoopType.Update));\r\n        PlayerLoopSystem&#91;] source = copyList&#91;index];\r\n        PlayerLoopSystem&#91;] dest = new PlayerLoopSystem&#91;source.Length + 1];\r\n        Array.Copy(source, 0, dest, 0, source.Length);\r\n        \r\n        PlayerLoopSystem MyUpdate = new PlayerLoopSystem\r\n        {\r\n            type = typeof(MyUpdate),\r\n            updateDelegate = CustomUpdate,\r\n        }\r\n        \r\n        dest&#91;dest.Length - 1] = MyUpdate;\r\n        \r\n        playerLoop.subSystemList = copyList;\r\n        PlayerLoop.SetPlayerLoop(playerLoop);\r\n    }\r\n    \r\n    private static int FindLoopSystemIndex(PlayerLoopSystem&#91;] playerLoopList, Type systemType)\r\n    {\r\n        for (int i = 0; i &lt; playerLoopList.Length; ++i)\r\n        {\r\n            if (playerLoopList&#91;i].type == systemType)\r\n            {\r\n                return i;\r\n            }\r\n        }\r\n\r\n        throw new Exception(\"PlayerLoopSystem \u672a\u627e\u5230\uff0ctype:\" + systemType.FullName);\r\n    }\r\n\r\n    private static void CustomUpdate()\r\n    {\r\n        Debug.Log(\"my update.\");\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u64cd\u4f5c\uff0c\u6211\u4eec\u5c31\u6210\u529f\u5728 Update \u751f\u547d\u5468\u671f\u540e\u6dfb\u52a0\u4e86\u81ea\u5b9a\u4e49\u4e8b\u4ef6 <code>CustomUpdate<\/code> \uff0c\u8be5\u65b9\u6cd5\u6bcf\u4e00\u5e27\u90fd\u4f1a\u88ab\u8c03\u7528\uff0c\u5e76\u6253\u5370\u201cmy update.\u201d\u3002<\/p>\n\n\n\n<p>\u56de\u5230 STask \uff0c\u6211\u4eec\u5c06\u5728\u539f\u6709\u751f\u547d\u5468\u671f\u4e0a\u6dfb\u52a014\u5904\u81ea\u5b9a\u4e49\u4e8b\u4ef6\uff082020.2\u7248\u672c\u53ca\u4ee5\u4e0a\u4e3a16\u5904\uff09\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Initialization\r\n\/\/---\r\n\/\/**STaskLoopRunnerYieldInitialization**\r\n\/\/**STaskLoopRunnerInitialization**\r\n\/\/PlayerUpdateTime\r\n\/\/DirectorSampleTime\r\n\/\/AsyncUploadTimeSlicedUpdate  \r\n\/\/SynchronizeInputs  \r\n\/\/SynchronizeState  \r\n\/\/XREarlyUpdate  \r\n\/\/**STaskLoopRunnerLastYieldInitialization**  \r\n\/\/**STaskLoopRunnerLastInitialization**  \r\n\r\n\/\/EarlyUpdate  \r\n\/\/---  \r\n\/\/**STaskLoopRunnerYieldEarlyUpdate**  \r\n\/\/**STaskLoopRunnerEarlyUpdate**  \r\n\/\/PollPlayerConnection  \r\n\/\/ProfilerStartFrame  \r\n\/\/GpuTimestamp  \r\n\/\/AnalyticsCoreStatsUpdate  \r\n\/\/UnityWebRequestUpdate  \r\n\/\/ExecuteMainThreadJobs  \r\n\/\/ProcessMouseInWindow  \r\n\/\/ClearIntermediateRenderers  \r\n\/\/ClearLines  \r\n\/\/PresentBeforeUpdate  \r\n\/\/ResetFrameStatsAfterPresent  \r\n\/\/UpdateAsyncReadbackManager  \r\n\/\/UpdateStreamingManager  \r\n\/\/UpdateTextureStreamingManager  \r\n\/\/UpdatePreloading  \r\n\/\/RendererNotifyInvisible  \r\n\/\/PlayerCleanupCachedData  \r\n\/\/UpdateMainGameViewRect  \r\n\/\/UpdateCanvasRectTransform  \r\n\/\/XRUpdate  \r\n\/\/UpdateInputManager  \r\n\/\/ProcessRemoteInput  \r\n\/\/*ScriptRunDelayedStartupFrame*  \r\n\/\/UpdateKinect  \r\n\/\/DeliverIosPlatformEvents  \r\n\/\/TangoUpdate  \r\n\/\/DispatchEventQueueEvents  \r\n\/\/PhysicsResetInterpolatedTransformPosition  \r\n\/\/SpriteAtlasManagerUpdate  \r\n\/\/PerformanceAnalyticsUpdate  \r\n\/\/**STaskLoopRunnerLastYieldEarlyUpdate**  \r\n\/\/**STaskLoopRunnerLastEarlyUpdate**  \r\n\r\n\/\/FixedUpdate  \r\n\/\/---  \r\n\/\/**STaskLoopRunnerYieldFixedUpdate**  \r\n\/\/**STaskLoopRunnerFixedUpdate**  \r\n\/\/ClearLines  \r\n\/\/NewInputFixedUpdate  \r\n\/\/DirectorFixedSampleTime  \r\n\/\/AudioFixedUpdate  \r\n\/\/*ScriptRunBehaviourFixedUpdate*  \r\n\/\/DirectorFixedUpdate  \r\n\/\/LegacyFixedAnimationUpdate  \r\n\/\/XRFixedUpdate  \r\n\/\/PhysicsFixedUpdate  \r\n\/\/Physics2DFixedUpdate  \r\n\/\/DirectorFixedUpdatePostPhysics  \r\n\/\/*ScriptRunDelayedFixedFrameRate*  \r\n\/\/**STaskLoopRunnerLastYieldFixedUpdate**  \r\n\/\/**STaskLoopRunnerLastFixedUpdate**  \r\n\r\n\/\/PreUpdate  \r\n\/\/---  \r\n\/\/**STaskLoopRunnerYieldPreUpdate**  \r\n\/\/**STaskLoopRunnerPreUpdate**  \r\n\/\/PhysicsUpdate  \r\n\/\/Physics2DUpdate  \r\n\/\/CheckTexFieldInput  \r\n\/\/IMGUISendQueuedEvents  \r\n\/\/NewInputUpdate  \r\n\/\/SendMouseEvents  \r\n\/\/AIUpdate  \r\n\/\/WindUpdate  \r\n\/\/UpdateVideo  \r\n\/\/**STaskLoopRunnerLastYieldPreUpdate**  \r\n\/\/**STaskLoopRunnerLastPreUpdate**  \r\n\r\n\/\/Update  \r\n\/\/---  \r\n\/\/**STaskLoopRunnerYieldUpdate**  \r\n\/\/**STaskLoopRunnerUpdate**  \r\n\/\/*ScriptRunBehaviourUpdate*  \r\n\/\/*ScriptRunDelayedDynamicFrameRate*  \r\n\/\/*ScriptRunDelayedTasks*  \r\n\/\/DirectorUpdate  \r\n\/\/**STaskLoopRunnerLastYieldUpdate**  \r\n\/\/**STaskLoopRunnerLastUpdate**  \r\n\r\n\/\/PreLateUpdate  \r\n\/\/---  \r\n\/\/**STaskLoopRunnerYieldPreLateUpdate**  \r\n\/\/**STaskLoopRunnerPreLateUpdate**  \r\n\/\/AIUpdatePostScript  \r\n\/\/DirectorUpdateAnimationBegin  \r\n\/\/LegacyAnimationUpdate  \r\n\/\/DirectorUpdateAnimationEnd  \r\n\/\/DirectorDeferredEvaluate  \r\n\/\/EndGraphicsJobsAfterScriptUpdate  \r\n\/\/ParticleSystemBeginUpdateAll  \r\n\/\/ConstraintManagerUpdate  \r\n\/\/*ScriptRunBehaviourLateUpdate*  \r\n\/\/**STaskLoopRunnerLastYieldPreLateUpdate**  \r\n\/\/**STaskLoopRunnerLastPreLateUpdate**  \r\n\r\n\/\/PostLateUpdate  \r\n\/\/---  \r\n\/\/**STaskLoopRunnerYieldPostLateUpdate**  \r\n\/\/**STaskLoopRunnerPostLateUpdate**  \r\n\/\/PlayerSendFrameStarted  \r\n\/\/DirectorLateUpdate  \r\n\/\/*ScriptRunDelayedDynamicFrameRate*  \r\n\/\/PhysicsSkinnedClothBeginUpdate  \r\n\/\/UpdateRectTransform  \r\n\/\/UpdateCanvasRectTransform  \r\n\/\/PlayerUpdateCanvases  \r\n\/\/UpdateAudio  \r\n\/\/VFXUpdate  \r\n\/\/ParticleSystemEndUpdateAll  \r\n\/\/EndGraphicsJobsAfterScriptLateUpdate  \r\n\/\/UpdateCustomRenderTextures  \r\n\/\/UpdateAllRenderers  \r\n\/\/EnlightenRuntimeUpdate  \r\n\/\/UpdateAllSkinnedMeshes  \r\n\/\/ProcessWebSendMessages  \r\n\/\/SortingGroupsUpdate  \r\n\/\/UpdateVideoTextures  \r\n\/\/UpdateVideo  \r\n\/\/DirectorRenderImage  \r\n\/\/PlayerEmitCanvasGeometry  \r\n\/\/PhysicsSkinnedClothFinishUpdate  \r\n\/\/FinishFrameRendering  \r\n\/\/BatchModeUpdate  \r\n\/\/PlayerSendFrameComplete  \r\n\/\/UpdateCaptureScreenshot  \r\n\/\/PresentAfterDraw  \r\n\/\/ClearImmediateRenderers  \r\n\/\/PlayerSendFramePostPresent  \r\n\/\/UpdateResolution  \r\n\/\/InputEndFrame  \r\n\/\/TriggerEndOfFrameCallbacks  \r\n\/\/GUIClearEvents  \r\n\/\/ShaderHandleErrors  \r\n\/\/ResetInputAxis  \r\n\/\/ThreadedLoadingDebug  \r\n\/\/ProfilerSynchronizeStats  \r\n\/\/MemoryFrameMaintenance  \r\n\/\/ExecuteGameCenterCallbacks  \r\n\/\/ProfilerEndFrame  \r\n\/\/**STaskLoopRunnerLastYieldPostLateUpdate**  \r\n\/\/**STaskLoopRunnerLastPostLateUpdate**<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\u524d\u540e\u52a0\u4e86<strong>\u4e24\u9897<\/strong>\u661f\u661f\u7684\u4e3a\u81ea\u5b9a\u4e49\u4e8b\u4ef6\uff0c<strong>\u4e00\u9897<\/strong>\u661f\u661f\u7684\u662f\u6bd4\u8f83\u91cd\u8981\u7684\u81ea\u5e26\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<p>\u806a\u660e\u7684\u5c0f\u4f19\u4f34\u53ef\u80fd\u5df2\u7ecf\u53d1\u73b0\uff0c\u6211\u4eec\u6bcf\u4e00\u5904\u63d2\u5165\u7684\u4e8b\u4ef6\u90fd\u5206\u4e3a\u4e86\u4e24\u79cd\u7c7b\u578b\u2014\u2014\u666e\u901a\u7c7b\u578b\u548c Yield \u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u8981\u5b9a\u4e49\u4e24\u79cd\u7c7b\u578b\uff1f<\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u63d2\u5165\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u540e\uff0c\u8fd9\u4e9b\u4e8b\u4ef6\u65b9\u6cd5\u6bcf\u4e00\u5e27\u90fd\u4f1a\u88ab\u8c03\u7528\u3002\u800c STask \u7684 PlayerLoopSystem \u8003\u8651\u5230\u901a\u7528\u6027\uff0c\u4e0d\u80fd\u4ec5\u4ec5\u53ea\u4e3a\u4e86 <code>STask.Delay()<\/code> \u8fd9\u79cd\u63a5\u53e3\u670d\u52a1\uff0c\u5b83\u8981\u652f\u6301\u7528\u6237\u901a\u8fc7 PlayerLoopSystem \u7684\u63a5\u53e3\u5411\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u4e2d\u6dfb\u52a0\u9700\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002\u90a3\u4e48\u7528\u6237\u63d0\u4f9b\u7684\u4ee3\u7801\u6709\u4e24\u79cd\u6267\u884c\u9700\u6c42\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6bcf\u4e00\u5e27\u6267\u884c\u4e00\u6b21\uff0c\u6bcf\u4e00\u6b21\u6267\u884c\u540e\u90fd\u5224\u65ad\u4e0b\u4e00\u6b21\u662f\u5426\u9700\u8981\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n\n\n\n<li>\u53ea\u6267\u884c\u4e00\u6b21\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4e0d\u540c\u7684\u6267\u884c\u9700\u6c42\uff0c\u5b9e\u73b0\u7684\u65b9\u5f0f\u4e5f\u662f\u4e0d\u4e00\u6837\u7684\u3002STask \u7684 PlayerLoopSystem \u6709\u4e24\u4e2a\u8fed\u4ee3\u529f\u80fd\u7684\u5b9e\u73b0\u5c0f\u5e2e\u624b\uff0c\u5206\u522b\u662f PlayerLoopRunner \u548c ContinuationQueue .<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PlayerLoopRunner<\/h3>\n\n\n\n<p>PlayerLoopRunner \u7528\u4e8e\u5904\u7406\u9700\u8981\u591a\u6b21\u8fed\u4ee3\u7684\u4ee3\u7801\u3002\u4e3a\u4e86\u5224\u65ad\u6bcf\u6b21\u8fed\u4ee3\u540e\uff0c\u4e0b\u4e00\u6b21\u662f\u5426\u9700\u8981\u7ee7\u7eed\u8fed\u4ee3\uff0c\u5b83\u9700\u8981\u914d\u5408\u63a5\u53e3 IPlayerLoopItem \u63a5\u53e3\u5de5\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\/ &lt;summary>\r\n\/\/\/ PlayerLoopSystem\u7684\u8fed\u4ee3\u5bf9\u8c61\r\n\/\/\/ \u63d0\u4f9b&lt;see cref=\"MoveNext\"\/>\u65b9\u6cd5\u4f9bPlayerLoopSystem\u8fed\u4ee3\uff0c\u8fd4\u56de false \u65f6\u8fed\u4ee3\u7ed3\u675f\r\n\/\/\/ \uff08\u7c7b\u4f3cIEnumerator\uff09\r\n\/\/\/ &lt;\/summary>\r\n\/\/\/ &lt;seealso href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.collections.ienumerator\"\/>IEnumerator\u53c2\u8003\r\npublic interface IPlayerLoopItem\r\n{\r\n    bool MoveNext();\r\n}<\/code><\/pre>\n\n\n\n<p>\u88ab\u8fed\u4ee3\u7684\u5bf9\u8c61\u9700\u8981\u5b9e\u73b0\u8be5\u63a5\u53e3\uff0c\u5e76\u5c06\u5f85\u6267\u884c\u4ee3\u7801\u653e\u8fdb <code>MoveNext()<\/code> \u65b9\u6cd5\u4e2d\uff0c\u5f53\u8be5\u65b9\u6cd5\u8fd4\u56de true \uff0c\u8868\u793a\u4e0b\u6b21\u7ee7\u7eed\u8fed\u4ee3\uff0c\u5426\u5219\u7ed3\u675f\u8fed\u4ee3\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ContinuationQueue<\/h3>\n\n\n\n<p>ContinuationQueue \u7528\u4e8e\u5904\u7406\u53ea\u9700\u8981\u8fed\u4ee3\u4e00\u6b21\u7684\u4ee3\u7801\u3002\u5b83\u7684\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\uff0c\u6bcf\u6b21\u8fed\u4ee3\u540e\u5c31\u628a\u56de\u8c03\u65b9\u6cd5\u4ece\u961f\u5217\u4e2d\u6392\u9664\u51fa\u53bb\u3002<\/p>\n\n\n\n<p>\u5b83\u4e0e PlayerLoopRunner \u7684\u533a\u522b\u8fd8\u5728\u4e8e\u5404\u81ea\u4f7f\u7528\u7684\u7ebf\u7a0b\u9501\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>PlayerLoopRunner\uff1a\u7531\u4e8e\u4e0d\u786e\u5b9aIPlayerLoopItem.MoveNext()\u7684\u6267\u884c\u7ed3\u679c\uff0c\u53ef\u80fd\u4f1a\u6709\u6bd4\u8f83\u591aItem\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\uff0c\u8fed\u4ee3\u4e00\u6b21\u9700\u8981\u82b1\u8d39\u8f83\u591a\u65f6\u95f4\uff0c\u56e0\u6b64\u8fd9\u91cc\u7528\u6df7\u5408\u591a\u7ebf\u7a0b\u9501\uff08Monitor\uff09\u3002<\/li>\n\n\n\n<li>ContinuationQueue\uff1a\u8fed\u4ee3\u5bf9\u8c61\u4e3a\u59d4\u6258\uff0c\u7531\u4e8e\u53ea\u6267\u884c\u4e00\u6b21\uff0c\u8fed\u4ee3\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e00\u822c\u6765\u8bf4\u6bd4\u8f83\u5c11\uff0c\u8fd9\u91cc\u7528\u81ea\u65cb\u7ebf\u7a0b\u9501\uff08SpinLock\uff09\uff0c\u4e14\u591a\u7528\u5728Yield\u3001\u7ebf\u7a0b\u5207\u6362\u4e0a\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">PlayerLoopHelper<\/h3>\n\n\n\n<p>STask \u7684 PlayerLoopSystem \u7684\u5927\u90e8\u5206\u64cd\u4f5c\u90fd\u901a\u8fc7 PlayerLoopHelper \u7c7b\u6765\u5b9e\u73b0\u3002\u5f53\u6211\u4eec\u9700\u8981\u5411 PlaerLoopRunner \u6216 ContinuationQueue \u6ce8\u518c\u8fed\u4ee3\u65b9\u6cd5\u65f6\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u4e24\u4e2a\u63a5\u53e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void AddAction(PlayerLoopTiming timing, IPlayerLoopItem action)\r\n{\r\n    PlayerLoopRunner runner = runners&#91;(int)timing];\r\n    if (runner == null)\r\n    {\r\n        ThrowInvalidLoopTiming(timing);\r\n    }\r\n    runner.AddAction(action);\r\n}\r\n\r\npublic static void AddContinuation(PlayerLoopTiming timing, Action continuation)\r\n{\r\n    ContinuationQueue queue = yielders&#91;(int)timing];\r\n    if (queue == null)\r\n    {\r\n        ThrowInvalidLoopTiming(timing);\r\n    }\r\n    queue.Enqueue(continuation);\r\n}<\/code><\/pre>\n\n\n\n<p>\u4e0e PlayerLoopSystem \u76f8\u5173\u7684\u4ee3\u7801\u6bd4\u8f83\u591a\uff0c\u5c0f\u7f16\u5c31\u4e0d\u5728\u8fd9\u91cc\u8d34\u6e90\u7801\u4e86\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u524d\u5f80\u4ed3\u5e93\u5730\u5740\u81ea\u884c\u7ffb\u9605\uff0c\u4ee3\u7801\u90fd\u505a\u4e86\u6bd4\u8f83\u8be6\u7ec6\u7684\u6ce8\u91ca\u5594\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">STask.Delay<\/h2>\n\n\n\n<p>\u8981\u5b9e\u73b0\u201c\u8ba9\u7a0b\u5e8f\u5728\u8fd9\u884c\u4ee3\u7801\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u201d\u7684\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u7d2f\u52a0\u6bcf\u4e00\u5e27\u7684\u95f4\u9694\u65f6\u95f4\uff0c\u5e76\u5224\u65ad\u4ece\u5f00\u59cb\u5230\u8fd9\u4e00\u5e27\u7684\u65f6\u95f4\u4e4b\u548c\u662f\u5426\u8fbe\u5230\u4e86\u9884\u8bbe\u7684\u7b49\u5f85\u65f6\u95f4\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u501f\u52a9 <code>PlayerLoopHelper.AddAction<\/code> \u63a5\u53e3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static STask Delay(int millisecondsDelay, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))\r\n{\r\n\tTimeSpan delayTimeSpan = TimeSpan.FromMilliseconds(millisecondsDelay);\r\n\treturn new STask(DelayPromise.Create(delayTimeSpan, delayTiming, cancellationToken, out short token), token);\r\n}\r\n\r\nprivate sealed class DelayPromise : ISTaskSource, IPlayerLoopItem, ITaskPoolNode&lt;DelayPromise>\r\n{\r\n    private static TaskPool&lt;DelayPromise> pool;\r\n    private DelayPromise nextNode;\r\n    public ref DelayPromise NextNode => ref this.nextNode;\r\n\r\n    static DelayPromise()\r\n    {\r\n        TaskPool.RegisterSizeGetter(typeof(DelayPromise), () => pool.Size);\r\n    }\r\n\r\n    private int initialFrame;\r\n    private float delayTimeSpan;\r\n    private float elapsed;\r\n    private CancellationToken cancellationToken;\r\n\r\n    STaskCompletionSourceCore&lt;object> core;\r\n\r\n    private DelayPromise() { }\r\n\r\n    public static ISTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)\r\n    {\r\n        if (cancellationToken.IsCancellationRequested)\r\n        {\r\n            return AutoResetSTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);\r\n        }\r\n\r\n        if(!pool.TryPop(out DelayPromise result))\r\n        {\r\n            result = new DelayPromise();\r\n        }\r\n\r\n        result.elapsed = 0.0f;\r\n        result.delayTimeSpan = (float)delayTimeSpan.TotalSeconds;\r\n        result.cancellationToken = cancellationToken;\r\n        result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;\r\n\r\n        PlayerLoopHelper.AddAction(timing, result);\r\n\r\n        token = result.core.Version;\r\n        return result;\r\n    }\r\n\r\n    public void GetResult(short token)\r\n    {\r\n        try\r\n        {\r\n            this.core.GetResult(token);\r\n        }\r\n        finally\r\n        {\r\n            this.TryReturn();\r\n        }\r\n    }\r\n\r\n    public STaskStatus GetStatus(short token)\r\n    {\r\n        return this.core.GetStatus(token);\r\n    }\r\n\r\n    public STaskStatus UnsafeGetStatus()\r\n    {\r\n        return this.core.UnsafeGetStatus();\r\n    }\r\n\r\n    public void OnCompleted(Action&lt;object> continuation, object state, short token)\r\n    {\r\n        this.core.OnCompleted(continuation, state, token);\r\n    }\r\n\r\n    public bool MoveNext()\r\n    {\r\n        if(this.cancellationToken.IsCancellationRequested)\r\n        {\r\n            this.core.TrySetCanceled(this.cancellationToken);\r\n            return false;\r\n        }\r\n\r\n        if (this.elapsed == 0.0f)\/\/\u521a\u5f00\u59cb\r\n        {\r\n            if (this.initialFrame == Time.frameCount)\r\n            {\r\n                return true;\r\n            }\r\n        }\r\n\r\n        this.elapsed += Time.deltaTime;\r\n        if (this.elapsed >= this.delayTimeSpan)\r\n        {\r\n            this.core.TrySetResult(null);\r\n            return false;\r\n        }\r\n\r\n        return true;\r\n    }\r\n\r\n    private bool TryReturn()\r\n    {\r\n        this.core.Reset();\r\n        this.delayTimeSpan = default;\r\n        this.elapsed = default;\r\n        this.cancellationToken = default;\r\n        return pool.TryPush(this);\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>\u91cd\u70b9\u662f DelayPromise \u8fd9\u4e2a\u5bb6\u4f19\uff0c\u5728\u5b83\u8eab\u4e0a\u6211\u4eec\u770b\u5230\u4e86\u4e09\u4e2a\u63a5\u53e3\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ITaskPoolNode\uff1a\u4f7f DelayPromise \u6c60\u5316\u3002<\/li>\n\n\n\n<li>ISTaskSource\uff1a\u5b9e\u73b0 STask \u7684\u76f8\u5173\u903b\u8f91\u3002<\/li>\n\n\n\n<li>IPlayerLoopItem\uff1a\u5b9e\u73b0\u8fed\u4ee3\u903b\u8f91\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u6709\u4e2a\u8001\u670b\u53cb STaskCompletionSourceCore \uff0c\u5b83\u6765\u914d\u5408 ISTaskSource \u63a5\u53e3\u7684\u76f8\u5173\u65b9\u6cd5\uff0c\u5904\u7406\u5f02\u6b65\u76f8\u5173\u7684\u4e8b\u60c5\uff0c\u8fd9\u4e00\u70b9\u8ddf AsyncSTask \u7c7b\u5f88\u76f8\u4f3c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<p>\u4e0e\u4e0a\u4e00\u7ae0\u7684\u5185\u5bb9\u76f8\u6bd4\uff0cSTask \u7684\u6269\u5c55\u6bd4\u8f83\u7b80\u5355\uff0c\u552f\u4e00\u9ebb\u70e6\u7684\u662f\u524d\u671f\u7684\u51c6\u5907\u5de5\u4f5c\u6bd4\u8f83\u591a\u3002\u5728\u7406\u89e3\u4e86\u6269\u5c55\u7684\u539f\u7406\u540e\uff0c\u540e\u9762\u7684\u5de5\u4f5c\u5c31\u4e00\u6cfb\u5343\u91cc\uff08\uff1f\u4e86\uff0c\u5176\u4ed6\u529f\u80fd\u7684\u6269\u5c55\uff0c\u5c0f\u4f19\u4f34\u4eec\u53ef\u4ee5\u67e5\u9605\u6e90\u7801\u6765\u5b66\u4e60\u3002<\/p>\n\n\n\n<p>\u5230\u6b64\uff0cUnity \u5f02\u6b65\u6269\u5c55\u5b9e\u8df5\u7684\u5185\u5bb9\u5c06\u544a\u4e00\u6bb5\u843d\u3002\u5176\u5b9e\u5c0f\u7f16\u4e5f\u6ca1\u6709\u6df1\u5165\u4e86\u89e3\u5168\u90e8\u7684 UniTask \u4ee3\u7801\uff0c\u5b83\u7684\u5176\u4ed6\u529f\u80fd\uff0c\u6bd4\u5982 linq \u5f02\u6b65\uff0c\u4e0e\u7b2c\u4e09\u65b9\u63d2\u4ef6\uff08DoTween\uff09\u7684\u9002\u914d\u6211\u90fd\u8fd8\u6ca1\u6709\u4e86\u89e3\u8fc7\uff0c\u7b49\u5230\u4ee5\u540e\u9700\u8981\u7528\u5230\u7684\u65f6\u5019\u518d\u7ee7\u7eed\u5427\uff0c\u54c8\u54c8O(\u2229_\u2229)O\u3002<\/p>\n\n\n\n<p>\u6700\u540e\u795d\u5c0f\u4f19\u4f34\u4eec\u5b66\u4e60\u6109\u5feb\uff0cDo what you do best.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u8fd9\u4e00\u7ae0\u6211\u4eec\u6765\u8ba8\u8bba\u5982\u4f55\u8ba9 STask \u53d8\u5f97\u66f4\u9177\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5b9e\u73b0\u4e00\u4e9b\u529f\u80fd\uff1a \u4e0a\u9762\u5217\u4e3e\u7684\u63a5\u53e3\u80fd\u88ab\u5927\u81f4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[23,20],"class_list":["post-1324","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-c","tag-unity"],"_links":{"self":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts\/1324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/comments?post=1324"}],"version-history":[{"count":2,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts\/1324\/revisions"}],"predecessor-version":[{"id":1326,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts\/1324\/revisions\/1326"}],"wp:attachment":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/media?parent=1324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/categories?post=1324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/tags?post=1324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}