From f60d2c51fbc99f0119c6bcca026c3ac4aeee94e6 Mon Sep 17 00:00:00 2001 From: neuecc Date: Sun, 24 May 2020 03:27:05 +0900 Subject: [PATCH] fix in UnityEditor performance issue --- .../Assets/Editor/EditorRunnerChecker.cs | 28 +++++++++++++++++ .../Assets/Editor/EditorRunnerChecker.cs.meta | 11 +++++++ .../UniTask/Runtime/PlayerLoopHelper.cs | 30 +++++++++++++++---- 3 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 src/UniTask/Assets/Editor/EditorRunnerChecker.cs create mode 100644 src/UniTask/Assets/Editor/EditorRunnerChecker.cs.meta diff --git a/src/UniTask/Assets/Editor/EditorRunnerChecker.cs b/src/UniTask/Assets/Editor/EditorRunnerChecker.cs new file mode 100644 index 0000000..ebff07c --- /dev/null +++ b/src/UniTask/Assets/Editor/EditorRunnerChecker.cs @@ -0,0 +1,28 @@ +#if UNITY_EDITOR + +using Cysharp.Threading.Tasks; +using System; +using System.IO; +using System.Linq; +using UnityEditor; +using UnityEngine; + +public static class EditorRunnerChecker +{ + [MenuItem("Tools/UniTaskEditorRunnerChecker")] + public static void RunUniTaskAsync() + { + RunCore().Forget(); + } + + static async UniTaskVoid RunCore() + { + Debug.Log("Start, Wait 5 seconds. deltaTime?" + Time.deltaTime); + + await UniTask.Delay(TimeSpan.FromSeconds(5)); + + Debug.Log("End, Wait 5 seconds. deltaTime?" + Time.deltaTime); + } +} + +#endif \ No newline at end of file diff --git a/src/UniTask/Assets/Editor/EditorRunnerChecker.cs.meta b/src/UniTask/Assets/Editor/EditorRunnerChecker.cs.meta new file mode 100644 index 0000000..f6a8be7 --- /dev/null +++ b/src/UniTask/Assets/Editor/EditorRunnerChecker.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e51b78c06cb410f42b36e0af9de3b065 +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 66f44d1..6389d3c 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs @@ -200,27 +200,45 @@ namespace Cysharp.Threading.Tasks #if UNITY_EDITOR + [InitializeOnLoadMethod] static void InitOnEditor() { - //Execute the play mode init method + // Execute the play mode init method Init(); - //register an Editor update delegate, used to forcing playerLoop update + // register an Editor update delegate, used to forcing playerLoop update EditorApplication.update += ForceEditorPlayerLoopUpdate; } + static double beforeCalledTime; + private static void ForceEditorPlayerLoopUpdate() { - if (EditorApplication.isPlayingOrWillChangePlaymode || EditorApplication.isCompiling || - EditorApplication.isUpdating) + if (EditorApplication.isPlayingOrWillChangePlaymode || EditorApplication.isCompiling || EditorApplication.isUpdating) { // Not in Edit mode, don't interfere return; } - //force unity to update PlayerLoop callbacks - EditorApplication.QueuePlayerLoopUpdate(); + // EditorApplication.QueuePlayerLoopUpdate causes performance issue, don't call directly. + // EditorApplication.QueuePlayerLoopUpdate(); + + if (yielders != null) + { + foreach (var item in yielders) + { + if (item != null) item.Run(); + } + } + + if (runners != null) + { + foreach (var item in runners) + { + if (item != null) item.Run(); + } + } } #endif