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..50e01dd9 --- /dev/null +++ b/src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs @@ -0,0 +1,49 @@ +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(); + 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/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 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; }