From 90631c54b186d4c6828647f6133df1d6dfbd2003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Lipert?= Date: Wed, 6 Jan 2021 19:39:31 +0100 Subject: [PATCH] More robust finding of right player loop system --- .../Runtime/Internal/PlayerLoopSystemEnum.cs | 22 ------- .../Internal/PlayerLoopSystemEnum.cs.meta | 11 ---- .../UniTask/Runtime/PlayerLoopHelper.cs | 60 ++++++++++++++++--- 3 files changed, 51 insertions(+), 42 deletions(-) delete mode 100644 src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopSystemEnum.cs delete mode 100644 src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopSystemEnum.cs.meta diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopSystemEnum.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopSystemEnum.cs deleted file mode 100644 index 0d03e03..0000000 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopSystemEnum.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Cysharp.Threading.Tasks.Internal -{ - public enum PlayerLoopSystemEnum : int - { -#if UNITY_2020_2_OR_NEWER - TimeUpdate, -#endif - Initialization, - EarlyUpdate, - FixedUpdate, - PreUpdate, - Update, - PreLateUpdate, - PostLateUpdate, - } -} diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopSystemEnum.cs.meta b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopSystemEnum.cs.meta deleted file mode 100644 index 5013a18..0000000 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopSystemEnum.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 82c7d0801384e6843a0cc6811570ca91 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs index 16c1005..f8776a2 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs @@ -283,6 +283,16 @@ namespace Cysharp.Threading.Tasks #endif + private static int? FindLoopSystemIndex(PlayerLoopSystem[] playerLoopList, Type systemType) + { + for (int i = 0; i < playerLoopList.Length; i++) + { + if (playerLoopList[i].type == systemType) + return i; + } + return null; + } + public static void Initialize(ref PlayerLoopSystem playerLoop) { yielders = new ContinuationQueue[14]; @@ -291,43 +301,75 @@ namespace Cysharp.Threading.Tasks var copyList = playerLoop.subSystemList.ToArray(); // Initialization - copyList[(int)PlayerLoopSystemEnum.Initialization].subSystemList = InsertRunner(copyList[(int)PlayerLoopSystemEnum.Initialization], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldInitialization), yielders[0] = new ContinuationQueue(PlayerLoopTiming.Initialization), + int? systemIndex = FindLoopSystemIndex(copyList, typeof(UnityEngine.PlayerLoop.Initialization)); + if (systemIndex.HasValue) + { + copyList[systemIndex.Value].subSystemList = InsertRunner(copyList[systemIndex.Value], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldInitialization), yielders[0] = new ContinuationQueue(PlayerLoopTiming.Initialization), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldInitialization), yielders[1] = new ContinuationQueue(PlayerLoopTiming.LastInitialization), typeof(UniTaskLoopRunners.UniTaskLoopRunnerInitialization), runners[0] = new PlayerLoopRunner(PlayerLoopTiming.Initialization), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastInitialization), runners[1] = new PlayerLoopRunner(PlayerLoopTiming.LastInitialization)); + } // EarlyUpdate - copyList[(int)PlayerLoopSystemEnum.EarlyUpdate].subSystemList = InsertRunner(copyList[(int)PlayerLoopSystemEnum.EarlyUpdate], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldEarlyUpdate), yielders[2] = new ContinuationQueue(PlayerLoopTiming.EarlyUpdate), + systemIndex = FindLoopSystemIndex(copyList, typeof(UnityEngine.PlayerLoop.EarlyUpdate)); + if (systemIndex.HasValue) + { + copyList[systemIndex.Value].subSystemList = InsertRunner(copyList[systemIndex.Value], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldEarlyUpdate), yielders[2] = new ContinuationQueue(PlayerLoopTiming.EarlyUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldEarlyUpdate), yielders[3] = new ContinuationQueue(PlayerLoopTiming.LastEarlyUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerEarlyUpdate), runners[2] = new PlayerLoopRunner(PlayerLoopTiming.EarlyUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastEarlyUpdate), runners[3] = new PlayerLoopRunner(PlayerLoopTiming.LastEarlyUpdate)); + } // FixedUpdate - copyList[(int)PlayerLoopSystemEnum.FixedUpdate].subSystemList = InsertRunner(copyList[(int)PlayerLoopSystemEnum.FixedUpdate], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldFixedUpdate), yielders[4] = new ContinuationQueue(PlayerLoopTiming.FixedUpdate), + systemIndex = FindLoopSystemIndex(copyList, typeof(UnityEngine.PlayerLoop.FixedUpdate)); + if (systemIndex.HasValue) + { + copyList[systemIndex.Value].subSystemList = InsertRunner(copyList[systemIndex.Value], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldFixedUpdate), yielders[4] = new ContinuationQueue(PlayerLoopTiming.FixedUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldFixedUpdate), yielders[5] = new ContinuationQueue(PlayerLoopTiming.LastFixedUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerFixedUpdate), runners[4] = new PlayerLoopRunner(PlayerLoopTiming.FixedUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastFixedUpdate), runners[5] = new PlayerLoopRunner(PlayerLoopTiming.LastFixedUpdate)); + } // PreUpdate - copyList[(int)PlayerLoopSystemEnum.PreUpdate].subSystemList = InsertRunner(copyList[(int)PlayerLoopSystemEnum.PreUpdate], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPreUpdate), yielders[6] = new ContinuationQueue(PlayerLoopTiming.PreUpdate), + systemIndex = FindLoopSystemIndex(copyList, typeof(UnityEngine.PlayerLoop.PreUpdate)); + if (systemIndex.HasValue) + { + copyList[systemIndex.Value].subSystemList = InsertRunner(copyList[systemIndex.Value], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPreUpdate), yielders[6] = new ContinuationQueue(PlayerLoopTiming.PreUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldPreUpdate), yielders[7] = new ContinuationQueue(PlayerLoopTiming.LastPreUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerPreUpdate), runners[6] = new PlayerLoopRunner(PlayerLoopTiming.PreUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastPreUpdate), runners[7] = new PlayerLoopRunner(PlayerLoopTiming.LastPreUpdate)); + } // Update - copyList[(int)PlayerLoopSystemEnum.Update].subSystemList = InsertRunner(copyList[(int)PlayerLoopSystemEnum.Update], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldUpdate), yielders[8] = new ContinuationQueue(PlayerLoopTiming.Update), + systemIndex = FindLoopSystemIndex(copyList, typeof(UnityEngine.PlayerLoop.Update)); + if (systemIndex.HasValue) + { + copyList[systemIndex.Value].subSystemList = InsertRunner(copyList[systemIndex.Value], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldUpdate), yielders[8] = new ContinuationQueue(PlayerLoopTiming.Update), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldUpdate), yielders[9] = new ContinuationQueue(PlayerLoopTiming.LastUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerUpdate), runners[8] = new PlayerLoopRunner(PlayerLoopTiming.Update), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastUpdate), runners[9] = new PlayerLoopRunner(PlayerLoopTiming.LastUpdate)); + } // PreLateUpdate - copyList[(int)PlayerLoopSystemEnum.PreLateUpdate].subSystemList = InsertRunner(copyList[(int)PlayerLoopSystemEnum.PreLateUpdate], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPreLateUpdate), yielders[10] = new ContinuationQueue(PlayerLoopTiming.PreLateUpdate), + systemIndex = FindLoopSystemIndex(copyList, typeof(UnityEngine.PlayerLoop.PreLateUpdate)); + if (systemIndex.HasValue) + { + copyList[systemIndex.Value].subSystemList = InsertRunner(copyList[systemIndex.Value], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPreLateUpdate), yielders[10] = new ContinuationQueue(PlayerLoopTiming.PreLateUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastYieldPreLateUpdate), yielders[11] = new ContinuationQueue(PlayerLoopTiming.LastPreLateUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerPreLateUpdate), runners[10] = new PlayerLoopRunner(PlayerLoopTiming.PreLateUpdate), typeof(UniTaskLoopRunners.UniTaskLoopRunnerLastPreLateUpdate), runners[11] = new PlayerLoopRunner(PlayerLoopTiming.LastPreLateUpdate)); + } // PostLateUpdate - copyList[(int)PlayerLoopSystemEnum.PostLateUpdate].subSystemList = InsertRunner(copyList[(int)PlayerLoopSystemEnum.PostLateUpdate], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPostLateUpdate), yielders[12] = new ContinuationQueue(PlayerLoopTiming.PostLateUpdate), + systemIndex = FindLoopSystemIndex(copyList, typeof(UnityEngine.PlayerLoop.PostLateUpdate)); + if (systemIndex.HasValue) + { + copyList[systemIndex.Value].subSystemList = InsertRunner(copyList[systemIndex.Value], typeof(UniTaskLoopRunners.UniTaskLoopRunnerYieldPostLateUpdate), yielders[12] = new ContinuationQueue(PlayerLoopTiming.PostLateUpdate), 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)); + } - // Insert UniTaskSynchronizationContext to Update loop - copyList[(int)PlayerLoopSystemEnum.Update].subSystemList = InsertUniTaskSynchronizationContext(copyList[(int)PlayerLoopSystemEnum.Update]); + systemIndex = FindLoopSystemIndex(copyList, typeof(UnityEngine.PlayerLoop.Update)); + if (systemIndex.HasValue) + { + // Insert UniTaskSynchronizationContext to Update loop + copyList[systemIndex.Value].subSystemList = InsertUniTaskSynchronizationContext(copyList[systemIndex.Value]); + } playerLoop.subSystemList = copyList; PlayerLoop.SetPlayerLoop(playerLoop);