Skip to content

Commit d760808

Browse files
https://github.com/annulusgames/LitMotion/pull/240
Fixing LitMotion to work with Enter the Play Mode annulusgames#240
1 parent e64d858 commit d760808

6 files changed

Lines changed: 71 additions & 0 deletions

File tree

src/LitMotion/Assets/LitMotion.Animation/Editor/LitMotionAnimationEditor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEditor.UIElements;
44
using System.Collections.Generic;
55
using UnityEngine;
6+
using UnityEditor.SceneManagement;
67

78
namespace LitMotion.Animation.Editor
89
{

src/LitMotion/Assets/LitMotion.Animation/Runtime/Internal.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
5+
#if UNITY_EDITOR
6+
using UnityEditor;
7+
8+
namespace LitMotion.Runtime.Internal
9+
{
10+
internal class EnterThePlayModeHelper
11+
{
12+
private static readonly List<Action> _scheduledActions = new();
13+
14+
[InitializeOnEnterPlayMode]
15+
private static void Reset()
16+
{
17+
foreach (var action in _scheduledActions)
18+
action();
19+
MotionDispatcher.Clear();
20+
}
21+
22+
[Conditional("UNITY_EDITOR")]
23+
public static void Register<TValue, TOptions, TAdapter>(MotionStorage<TValue, TOptions, TAdapter> storage)
24+
where TValue : unmanaged
25+
where TOptions : unmanaged, IMotionOptions
26+
where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
27+
{
28+
if (EditorSettings.enterPlayModeOptionsEnabled)
29+
_scheduledActions.Add(storage.Reset);
30+
}
31+
32+
[Conditional("UNITY_EDITOR")]
33+
public static void Register<TValue, TOptions, TAdapter>(UpdateRunner<TValue, TOptions, TAdapter> runner)
34+
where TValue : unmanaged
35+
where TOptions : unmanaged, IMotionOptions
36+
where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
37+
{
38+
if (EditorSettings.enterPlayModeOptionsEnabled)
39+
_scheduledActions.Add(runner.Reset);
40+
}
41+
}
42+
}
43+
#endif

src/LitMotion/Assets/LitMotion/Runtime/Internal/EnterThePlayModeHelper.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/LitMotion/Assets/LitMotion/Runtime/Internal/MotionManager.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,14 @@ static void CheckTypeId(in MotionHandle handle)
115115
throw new ArgumentException("Invalid type id.");
116116
}
117117
}
118+
119+
#if UNITY_EDITOR
120+
[UnityEditor.InitializeOnEnterPlayMode]
121+
private static void Clear()
122+
{
123+
MotionTypeCount = 0;
124+
list.Clear();
125+
}
126+
#endif
118127
}
119128
}

src/LitMotion/Assets/LitMotion/Runtime/MotionDispatcher.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using LitMotion.Collections;
55

66
#if UNITY_EDITOR
7+
using LitMotion.Runtime.Internal;
78
using UnityEditor;
89
#endif
910

@@ -51,6 +52,9 @@ static MotionStorage<TValue, TOptions, TAdapter> CreateIfNull(ref MotionStorage<
5152
{
5253
storage = new MotionStorage<TValue, TOptions, TAdapter>(MotionManager.MotionTypeCount);
5354
MotionManager.Register(storage);
55+
#if UNITY_EDITOR
56+
EnterThePlayModeHelper.Register(storage);
57+
#endif
5458
}
5559
return storage;
5660
}
@@ -100,6 +104,9 @@ public static (UpdateRunner<TValue, TOptions, TAdapter> runner, bool isCreated)
100104
runner = new UpdateRunner<TValue, TOptions, TAdapter>(storage, Time.timeAsDouble, Time.unscaledTimeAsDouble, Time.realtimeSinceStartupAsDouble);
101105
}
102106
GetRunnerList(playerLoopTiming).Add(runner);
107+
#if UNITY_EDITOR
108+
EnterThePlayModeHelper.Register(runner);
109+
#endif
103110
return (runner, true);
104111
}
105112
return (runner, false);
@@ -251,6 +258,7 @@ public static MotionStorage<TValue, TOptions, TAdapter> GetOrCreateStorage()
251258
{
252259
storage = new MotionStorage<TValue, TOptions, TAdapter>(MotionManager.MotionTypeCount);
253260
MotionManager.Register(storage);
261+
EnterThePlayModeHelper.Register(storage);
254262
}
255263
return storage;
256264
}

0 commit comments

Comments
 (0)