From 908e3619852f2f449183362fda4b9e15ac0f1334 Mon Sep 17 00:00:00 2001 From: neuecc Date: Thu, 7 Jan 2021 13:02:18 +0900 Subject: [PATCH] Add PlayerLoopTiming.TimeUpdate/LastTimeUpdate in Unity 2020.2 --- README.md | 5 ++++ .../Runtime/Internal/ContinuationQueue.cs | 12 ++++++++ .../Runtime/Internal/PlayerLoopRunner.cs | 12 ++++++++ .../UniTask/Runtime/PlayerLoopHelper.cs | 28 ++++++++++++++++++- src/UniTask/Assets/Scenes/SandboxMain.cs | 20 +++++++------ 5 files changed, 67 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bd49950..729f890 100644 --- a/README.md +++ b/README.md @@ -346,6 +346,11 @@ public enum PlayerLoopTiming PostLateUpdate = 12, LastPostLateUpdate = 13 + +#if UNITY_2020_2_OR_NEWER + TimeUpdate = 14, + LastTimeUpdate = 15, +#endif } ``` diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs index f65ae59..a311126 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs @@ -132,6 +132,14 @@ namespace Cysharp.Threading.Tasks.Internal case PlayerLoopTiming.LastPostLateUpdate: LastPostLateUpdate(); break; +#if UNITY_2020_2_OR_NEWER + case PlayerLoopTiming.TimeUpdate: + TimeUpdate(); + break; + case PlayerLoopTiming.LastTimeUpdate: + LastTimeUpdate(); + break; +#endif default: break; } @@ -154,6 +162,10 @@ namespace Cysharp.Threading.Tasks.Internal void LastPreLateUpdate() => RunCore(); void PostLateUpdate() => RunCore(); void LastPostLateUpdate() => RunCore(); +#if UNITY_2020_2_OR_NEWER + void TimeUpdate() => RunCore(); + void LastTimeUpdate() => RunCore(); +#endif [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 a1dd3cd..43625ab 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopRunner.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopRunner.cs @@ -118,6 +118,14 @@ namespace Cysharp.Threading.Tasks.Internal case PlayerLoopTiming.LastPostLateUpdate: LastPostLateUpdate(); break; +#if UNITY_2020_2_OR_NEWER + case PlayerLoopTiming.TimeUpdate: + TimeUpdate(); + break; + case PlayerLoopTiming.LastTimeUpdate: + LastTimeUpdate(); + break; +#endif default: break; } @@ -140,6 +148,10 @@ namespace Cysharp.Threading.Tasks.Internal void LastPreLateUpdate() => RunCore(); void PostLateUpdate() => RunCore(); void LastPostLateUpdate() => RunCore(); +#if UNITY_2020_2_OR_NEWER + void TimeUpdate() => RunCore(); + void LastTimeUpdate() => RunCore(); +#endif [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 1aa6a61..ae1bcb3 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs @@ -57,6 +57,13 @@ namespace Cysharp.Threading.Tasks public struct UniTaskLoopRunnerLastYieldUpdate { }; public struct UniTaskLoopRunnerLastYieldPreLateUpdate { }; public struct UniTaskLoopRunnerLastYieldPostLateUpdate { }; + +#if UNITY_2020_2_OR_NEWER + public struct UniTaskLoopRunnerTimeUpdate { }; + public struct UniTaskLoopRunnerLastTimeUpdate { }; + public struct UniTaskLoopRunnerYieldTimeUpdate { }; + public struct UniTaskLoopRunnerLastYieldTimeUpdate { }; +#endif } public enum PlayerLoopTiming @@ -80,7 +87,13 @@ namespace Cysharp.Threading.Tasks LastPreLateUpdate = 11, PostLateUpdate = 12, - LastPostLateUpdate = 13 + LastPostLateUpdate = 13, + +#if UNITY_2020_2_OR_NEWER + // Unity 2020.2 added TimeUpdate https://docs.unity3d.com/2020.2/Documentation/ScriptReference/PlayerLoop.TimeUpdate.html + TimeUpdate = 14, + LastTimeUpdate = 15, +#endif } public interface IPlayerLoopItem @@ -298,8 +311,13 @@ namespace Cysharp.Threading.Tasks public static void Initialize(ref PlayerLoopSystem playerLoop) { +#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 copyList = playerLoop.subSystemList.ToArray(); @@ -345,6 +363,14 @@ namespace Cysharp.Threading.Tasks typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldPostLateUpdate), yielders[13] = new ContinuationQueue(PlayerLoopTiming.LastPostLateUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerPostLateUpdate), runners[12] = new PlayerLoopRunner(PlayerLoopTiming.PostLateUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastPostLateUpdate), runners[13] = new PlayerLoopRunner(PlayerLoopTiming.LastPostLateUpdate)); +#if UNITY_2020_2_OR_NEWER + // TimeUpdate + i = FindLoopSystemIndex(copyList, typeof(UnityEngine.PlayerLoop.TimeUpdate)); + copyList[i].subSystemList = InsertRunner(copyList[i], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldTimeUpdate), yielders[14] = new ContinuationQueue(PlayerLoopTiming.TimeUpdate), + typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldTimeUpdate), yielders[15] = new ContinuationQueue(PlayerLoopTiming.LastTimeUpdate), + typeof(UniTaskLoopRunners.UniTaskLoopRunnerTimeUpdate), runners[14] = new PlayerLoopRunner(PlayerLoopTiming.TimeUpdate), + typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastTimeUpdate), runners[15] = new PlayerLoopRunner(PlayerLoopTiming.LastTimeUpdate)); +#endif // Insert UniTaskSynchronizationContext to Update loop i = FindLoopSystemIndex(copyList, typeof(UnityEngine.PlayerLoop.Update)); diff --git a/src/UniTask/Assets/Scenes/SandboxMain.cs b/src/UniTask/Assets/Scenes/SandboxMain.cs index ed6007c..0db724d 100644 --- a/src/UniTask/Assets/Scenes/SandboxMain.cs +++ b/src/UniTask/Assets/Scenes/SandboxMain.cs @@ -516,6 +516,13 @@ public class SandboxMain : MonoBehaviour Debug.Log("end cor"); } + IEnumerator LastYieldCore() + { + Debug.Log("YieldBegin:" + Time.frameCount); + yield return new WaitForEndOfFrame(); + Debug.Log("YieldEnd:" + Time.frameCount); + } + async UniTaskVoid Start() { await TestCor().ToUniTask(this); @@ -567,18 +574,13 @@ public class SandboxMain : MonoBehaviour okButton.onClick.AddListener(UniTask.UnityAction(async () => { + StartCoroutine(LastYieldCore()); - var client = new NetworkClient("http://localhost:5000", TimeSpan.FromSeconds(2), - new QueueRequestDecorator() - /*, new LoggingDecorator()*/ - ); - //new AppendTokenDecorator(), - //new SetupHeaderDecorator()); - - - await client.PostAsync("", new { Id = 100 }); + Debug.Log("BEFORE:" + Time.frameCount); + await UniTask.Yield(PlayerLoopTiming.LastTimeUpdate); + Debug.Log("AFTER:" + Time.frameCount); })); // _ = ExecuteAsync();