From 4f292b57e7ba921803c1ff1b8abb8f3e2d84cbd1 Mon Sep 17 00:00:00 2001 From: cuibeibei Date: Fri, 2 Apr 2021 17:19:10 +0800 Subject: [PATCH] [PlayerLoop]:Add Manual Loop Timing --- .../Runtime/Internal/ContinuationQueue.cs | 4 +++ .../Runtime/Internal/PlayerLoopRunner.cs | 4 +++ .../UniTask/Runtime/PlayerLoopHelper.cs | 36 +++++++++++++++---- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs index a311126..e478aac 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs @@ -140,6 +140,9 @@ namespace Cysharp.Threading.Tasks.Internal LastTimeUpdate(); break; #endif + case PlayerLoopTiming.Manual: + ManualUpdate(); + break; default: break; } @@ -166,6 +169,7 @@ namespace Cysharp.Threading.Tasks.Internal void TimeUpdate() => RunCore(); void LastTimeUpdate() => RunCore(); #endif + void ManualUpdate() => RunCore(); [System.Diagnostics.DebuggerHidden] void RunCore() diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopRunner.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopRunner.cs index 43625ab..c6c8a73 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopRunner.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopRunner.cs @@ -126,6 +126,9 @@ namespace Cysharp.Threading.Tasks.Internal LastTimeUpdate(); break; #endif + case PlayerLoopTiming.Manual: + ManualUpdate(); + break; default: break; } @@ -152,6 +155,7 @@ namespace Cysharp.Threading.Tasks.Internal void TimeUpdate() => RunCore(); void LastTimeUpdate() => RunCore(); #endif + void ManualUpdate() => RunCore(); [System.Diagnostics.DebuggerHidden] void RunCore() diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs index a194c36..d6fecf9 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs @@ -96,6 +96,8 @@ namespace Cysharp.Threading.Tasks TimeUpdate = 14, LastTimeUpdate = 15, #endif + + Manual } [Flags] @@ -393,13 +395,12 @@ namespace Cysharp.Threading.Tasks public static void Initialize(ref PlayerLoopSystem playerLoop, InjectPlayerLoopTimings injectTimings = InjectPlayerLoopTimings.All) { -#if UNITY_2020_2_OR_NEWER - yielders = new ContinuationQueue[16]; - runners = new PlayerLoopRunner[16]; -#else - yielders = new ContinuationQueue[14]; - runners = new PlayerLoopRunner[14]; -#endif + var manualIndex = (int)PlayerLoopTiming.Manual; + yielders = new ContinuationQueue[manualIndex+1]; + runners = new PlayerLoopRunner[manualIndex+1]; + + yielders[manualIndex] = new ContinuationQueue(PlayerLoopTiming.Manual); + runners[manualIndex] = new PlayerLoopRunner(PlayerLoopTiming.Manual); var copyList = playerLoop.subSystemList.ToArray(); @@ -485,6 +486,27 @@ namespace Cysharp.Threading.Tasks PlayerLoop.SetPlayerLoop(playerLoop); } + public static void ManualUpdate() + { + if (yielders != null) + { + var item = yielders[(int)PlayerLoopTiming.Manual]; + if (item != null) + { + item.Run(); + } + } + + if (runners != null) + { + var item = runners[(int)PlayerLoopTiming.Manual]; + if (item != null) + { + item.Run(); + } + } + } + public static void AddAction(PlayerLoopTiming timing, IPlayerLoopItem action) { var runner = runners[(int)timing];