Skip to content

Commit 816c8c1

Browse files
committed
prevents playing mode if Unity SpritePacker isn't enabled
minor refactoring in BaseSpriteAnimator3000.cs
1 parent 2c0cfc1 commit 816c8c1

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

Runtime/BaseSpriteAnimator3000.cs

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,35 @@ public bool flipY
4848
}
4949
}
5050

51-
public bool playInEditor { get; set; }
51+
private bool _playInEditor;
52+
public bool playInEditor { get => _playInEditor; set => _playInEditor = value; }
53+
54+
private float _clipTime;
55+
public float clipTime => _clipTime;
56+
57+
private int _clipIndex = 0;
58+
public int clipIndex => _clipIndex;
5259

53-
public float clipTime { get; private set; }
54-
public int clipIndex { get { return clipIdx; } }
5560
public SpriteAnimationClip3000 clip { get { return GetClipInternal(clipIndex); } }
5661
public string clipName { get { return clip?.name; } }
5762
public float clipLength { get { return clip?.GetLength(totalTimeScale) ?? 0; } }
58-
public float normalizedTime { get { return clipTime / clipLength; } }
63+
public float normalizedTime
64+
{
65+
get
66+
{
67+
var l = clipLength;
68+
if (l > 0)
69+
return _clipTime / clipLength;
70+
else
71+
return 0f;
72+
}
73+
}
5974
#if UNITY_EDITOR
60-
public int editorIndex { get { return clipIdx; } set { clipIdx = value; } }
75+
public int editorIndex { get { return _clipIndex; } set { _clipIndex = value; } }
6176
#endif
62-
private int clipIdx = 0;
63-
private Action callback = null;
64-
private bool isAnimated = false;
77+
78+
private Action _callback = null;
79+
private bool _isAnimated = false;
6580

6681
private static readonly SpriteAnimatorTimer3000 timer = new SpriteAnimatorTimer3000();
6782

@@ -83,7 +98,7 @@ protected virtual void OnDestroy()
8398

8499
private void Update()
85100
{
86-
isAnimated = true;
101+
_isAnimated = true;
87102

88103
var dt = timer.GetDeltaTime(m_timeThread);
89104
Animation(clip, dt);
@@ -98,14 +113,14 @@ private void Animation(SpriteAnimationClip3000 clip, float deltaTime)
98113
Sprite sprite = SampleByNormalizedTime(clip, normalizedTime);
99114
ChangeSprite(sprite);
100115

101-
clipTime += dt;
102-
if (clipTime >= clipLength)
116+
_clipTime += dt;
117+
if (_clipTime >= clipLength)
103118
{
104-
clipTime = 0;
105-
if (callback != null)
119+
_clipTime = 0;
120+
if (_callback != null)
106121
{
107-
callback();
108-
callback = null;
122+
_callback();
123+
_callback = null;
109124
}
110125
}
111126
}
@@ -166,18 +181,12 @@ private void ChangeFlip(bool flipX, bool flipY)
166181

167182
private bool ChangeClipIndex(string clipName)
168183
{
169-
if (callback != null)
170-
{
171-
//do nothing
172-
callback = null;
173-
}
174-
175184
int idx = GetClipIndex(clipName);
176185
if (idx == -1)
177186
return false;
178187

179-
clipIdx = idx;
180-
clipTime = 0;
188+
_clipIndex = idx;
189+
_clipTime = 0;
181190
return true;
182191
}
183192

@@ -219,10 +228,10 @@ private bool PlayInternal(string clipName, bool immediately, Action callback = n
219228
if (!res)
220229
return false;
221230

222-
if (!isAnimated)
231+
if (!_isAnimated)
223232
Animation(clip, 0);
224233

225-
this.callback = callback;
234+
this._callback = callback;
226235
return true;
227236
}
228237

@@ -293,9 +302,22 @@ public void EditorUpdate()
293302

294303
if (playInEditor)
295304
{
296-
float deltaTime = Time.realtimeSinceStartup - lastRealtimeSinceStartup;
297-
Animation(clip, deltaTime);
298-
lastRealtimeSinceStartup = Time.realtimeSinceStartup;
305+
if (UnityEditor.EditorSettings.spritePackerMode != UnityEditor.SpritePackerMode.Disabled)
306+
{
307+
float deltaTime = Time.realtimeSinceStartup - lastRealtimeSinceStartup;
308+
Animation(clip, deltaTime);
309+
lastRealtimeSinceStartup = Time.realtimeSinceStartup;
310+
}
311+
else
312+
{
313+
playInEditor = false;
314+
315+
const string url = "https://docs.unity3d.com/es/Manual/SpritePackerModes.html";
316+
if (UnityEditor.EditorUtility.DisplayDialog("Error", $"SpritePacker is disabled. Please go to the official documentation: {url}", "Go to Documentation", "I've got it!"))
317+
{
318+
Application.OpenURL(url);
319+
}
320+
}
299321
}
300322
}
301323

Runtime/SpriteAnimationClip3000.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public Sprite GetFrameSprite(string spriteName)
104104
if (m_sprites == null)
105105
{
106106
#if UNITY_EDITOR
107+
var available = true;
107108
switch (EditorSettings.spritePackerMode)
108109
{
109110
case SpritePackerMode.BuildTimeOnlyAtlas:
@@ -114,9 +115,14 @@ public Sprite GetFrameSprite(string spriteName)
114115
case SpritePackerMode.AlwaysOnAtlas:
115116
//do nothing, this SpriteAtlas packed already
116117
break;
118+
119+
case SpritePackerMode.Disabled:
120+
//shouble be return null, because no one sprite available
121+
available = false;
122+
break;
117123
}
118124
#endif
119-
if (m_spriteAtlas != null)
125+
if (available && m_spriteAtlas != null)
120126
{
121127
m_spritesLength = m_spriteAtlas.spriteCount;
122128
m_sprites = new Sprite[m_spritesLength];

0 commit comments

Comments
 (0)