Skip to content

Commit e69af81

Browse files
committed
added ability to start initial animation on the different initial time
1 parent 7d26e45 commit e69af81

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Editor/SpriteAnimator3000InspectorHelper.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using UnityEditor;
33
using UnityEditor.SceneManagement;
44
using UnityEngine.SceneManagement;
5-
using Spritesheet3000;
65

76
namespace Spritesheet3000.Editor
87
{
@@ -12,6 +11,7 @@ public static class SpriteAnimator3000InspectorHelper
1211
private static readonly GUIContent _timeScaleLabel = new GUIContent("Time Scale");
1312
private static readonly GUIContent _flipXLabel = new GUIContent("Flip X");
1413
private static readonly GUIContent _flipYLabel = new GUIContent("Flip Y");
14+
private static readonly GUIContent _randomStartLabel = new GUIContent("Random Start");
1515

1616
public static void OnInspectorDraw<T>(SerializedObject serializedObject, BaseSpriteAnimator3000<T> anim, ref float currentTime, ref string[] currentClipOptions)
1717
{
@@ -30,6 +30,10 @@ public static void OnInspectorDraw<T>(SerializedObject serializedObject, BaseSpr
3030
if (timeScaleProp != null)
3131
EditorGUILayout.PropertyField(timeScaleProp, _timeScaleLabel);
3232

33+
var randomStartProp = serializedObject.FindProperty("m_randomStart");
34+
if (randomStartProp != null)
35+
EditorGUILayout.PropertyField(randomStartProp, _randomStartLabel);
36+
3337
var flipXProp = serializedObject.FindProperty("m_flip_x");
3438
if (flipXProp != null)
3539
EditorGUILayout.PropertyField(flipXProp, _flipXLabel);

Runtime/BaseSpriteAnimator3000.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public abstract class BaseSpriteAnimator3000<T> : MonoBehaviour
2222
public float timeScale { get { return m_timeScale; } set { m_timeScale = value; } }
2323
public float totalTimeScale { get { return m_timeScale * SpriteAnimatorTimer3000.timeScale; } }
2424

25+
[SerializeField]
26+
[HideInInspector]
27+
private bool m_randomStart;
28+
2529
[SerializeField]
2630
[HideInInspector]
2731
private bool m_flip_x = false;
@@ -47,7 +51,7 @@ public bool flipY
4751
ChangeFlip(m_flip_x, m_flip_y);
4852
}
4953
}
50-
54+
5155
private bool _playInEditor;
5256
public bool playInEditor { get => _playInEditor; set => _playInEditor = value; }
5357

@@ -91,6 +95,16 @@ protected virtual void Awake()
9195
ChangeFlip(flipX, flipY);
9296
}
9397

98+
protected virtual void Start()
99+
{
100+
if (m_randomStart)
101+
{
102+
var l = clipLength;
103+
if (l > 0)
104+
_clipTime = UnityEngine.Random.Range(0f, l);
105+
}
106+
}
107+
94108
protected virtual void OnDestroy()
95109
{
96110
//do nothing
@@ -106,17 +120,18 @@ private void Update()
106120

107121
private void Animation(SpriteAnimationClip3000 clip, float deltaTime)
108122
{
109-
float dt = deltaTime;
110-
if (dt < 0)
123+
if (deltaTime < 0)
111124
return;
112125

113126
Sprite sprite = SampleByNormalizedTime(clip, normalizedTime);
114127
ChangeSprite(sprite);
115128

116-
_clipTime += dt;
117-
if (_clipTime >= clipLength)
129+
_clipTime += deltaTime;
130+
131+
var l = clipLength;
132+
if (_clipTime >= l)
118133
{
119-
_clipTime = 0;
134+
_clipTime = Mathf.Repeat(_clipTime, l);
120135
if (_callback != null)
121136
{
122137
_callback();

0 commit comments

Comments
 (0)