From ec8a614af361bbe3de2e51addc348579090c2ded Mon Sep 17 00:00:00 2001 From: pc Date: Fri, 1 Aug 2025 00:16:34 +0200 Subject: [PATCH 1/3] Reset static cache on enter the play mode --- .../Internal/EnterThePlayModeHelper.cs | 50 +++++++++++++++++++ .../Internal/EnterThePlayModeHelper.cs.meta | 3 ++ .../LitMotion/Runtime/MotionDispatcher.cs | 4 ++ 3 files changed, 57 insertions(+) create mode 100644 src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs create mode 100644 src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs.meta diff --git a/src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs b/src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs new file mode 100644 index 00000000..a87fef6a --- /dev/null +++ b/src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; + +#if UNITY_EDITOR +using UnityEditor; +#endif + +namespace LitMotion.LitMotion.Runtime.Internal +{ + internal class EnterThePlayModeHelper + { +#if UNITY_EDITOR + private static readonly List _scheduledActions = new(); + + [InitializeOnEnterPlayMode] + private static void Reset() + { + foreach (var action in _scheduledActions) + action(); + _scheduledActions.Clear(); + MotionDispatcher.Clear(); + } +#endif + + [Conditional("UNITY_EDITOR")] + public static void Register(MotionStorage storage) + where TValue : unmanaged + where TOptions : unmanaged, IMotionOptions + where TAdapter : unmanaged, IMotionAdapter + { +#if UNITY_EDITOR + if (EditorSettings.enterPlayModeOptionsEnabled) + _scheduledActions.Add(storage.Reset); +#endif + } + + [Conditional("UNITY_EDITOR")] + public static void Register(UpdateRunner runner) + where TValue : unmanaged + where TOptions : unmanaged, IMotionOptions + where TAdapter : unmanaged, IMotionAdapter + { +#if UNITY_EDITOR + if (EditorSettings.enterPlayModeOptionsEnabled) + _scheduledActions.Add(runner.Reset); +#endif + } + } +} \ No newline at end of file diff --git a/src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs.meta b/src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs.meta new file mode 100644 index 00000000..600d5a52 --- /dev/null +++ b/src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a81228c84a2b49cda1bea548b9bf0867 +timeCreated: 1753999485 \ No newline at end of file diff --git a/src/LitMotion/Assets/LitMotion/Runtime/MotionDispatcher.cs b/src/LitMotion/Assets/LitMotion/Runtime/MotionDispatcher.cs index 0fb2723d..d3fda541 100644 --- a/src/LitMotion/Assets/LitMotion/Runtime/MotionDispatcher.cs +++ b/src/LitMotion/Assets/LitMotion/Runtime/MotionDispatcher.cs @@ -2,6 +2,7 @@ using System.Runtime.CompilerServices; using UnityEngine; using LitMotion.Collections; +using LitMotion.LitMotion.Runtime.Internal; #if UNITY_EDITOR using UnityEditor; @@ -51,6 +52,7 @@ static MotionStorage CreateIfNull(ref MotionStorage< { storage = new MotionStorage(MotionManager.MotionTypeCount); MotionManager.Register(storage); + EnterThePlayModeHelper.Register(storage); } return storage; } @@ -100,6 +102,7 @@ public static (UpdateRunner runner, bool isCreated) runner = new UpdateRunner(storage, Time.timeAsDouble, Time.unscaledTimeAsDouble, Time.realtimeSinceStartupAsDouble); } GetRunnerList(playerLoopTiming).Add(runner); + EnterThePlayModeHelper.Register(runner); return (runner, true); } return (runner, false); @@ -251,6 +254,7 @@ public static MotionStorage GetOrCreateStorage() { storage = new MotionStorage(MotionManager.MotionTypeCount); MotionManager.Register(storage); + EnterThePlayModeHelper.Register(storage); } return storage; } From 04dbdf401bf225b61549ba478d348b63941893bf Mon Sep 17 00:00:00 2001 From: pc Date: Fri, 1 Aug 2025 01:13:59 +0200 Subject: [PATCH 2/3] Removed clearing of list because caches don't get created every time --- .../Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs b/src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs index a87fef6a..50e01dd9 100644 --- a/src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs +++ b/src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs @@ -18,7 +18,6 @@ private static void Reset() { foreach (var action in _scheduledActions) action(); - _scheduledActions.Clear(); MotionDispatcher.Clear(); } #endif From 0a9c3f08e0e883afc5da3c1b25aa1215f9cb83b3 Mon Sep 17 00:00:00 2001 From: pc Date: Fri, 1 Aug 2025 01:26:42 +0200 Subject: [PATCH 3/3] Clearing MotionManager on entering play mode --- .../Assets/LitMotion/Runtime/Internal/MotionManager.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/LitMotion/Assets/LitMotion/Runtime/Internal/MotionManager.cs b/src/LitMotion/Assets/LitMotion/Runtime/Internal/MotionManager.cs index 940b5441..fb83d5f8 100644 --- a/src/LitMotion/Assets/LitMotion/Runtime/Internal/MotionManager.cs +++ b/src/LitMotion/Assets/LitMotion/Runtime/Internal/MotionManager.cs @@ -115,5 +115,14 @@ static void CheckTypeId(in MotionHandle handle) throw new ArgumentException("Invalid type id."); } } + +#if UNITY_EDITOR + [UnityEditor.InitializeOnEnterPlayMode] + private static void Clear() + { + MotionTypeCount = 0; + list.Clear(); + } +#endif } } \ No newline at end of file