This repository was archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathSimpleAnimation_impl.cs
More file actions
401 lines (340 loc) · 10.5 KB
/
SimpleAnimation_impl.cs
File metadata and controls
401 lines (340 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
using System;
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.Playables;
[RequireComponent(typeof(Animator))]
public partial class SimpleAnimation: MonoBehaviour
{
private class StateEnumerable : IEnumerable<State>
{
private SimpleAnimation m_Owner;
public StateEnumerable(SimpleAnimation owner)
{
m_Owner = owner;
}
public IEnumerator<State> GetEnumerator()
{
return new StateEnumerator(m_Owner);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new StateEnumerator(m_Owner);
}
class StateEnumerator : IEnumerator<State>
{
private SimpleAnimation m_Owner;
private IEnumerator<SimpleAnimationPlayable.IState> m_Impl;
public StateEnumerator(SimpleAnimation owner)
{
m_Owner = owner;
m_Impl = m_Owner.m_Playable.GetStates().GetEnumerator();
Reset();
}
State GetCurrent()
{
return new StateImpl(m_Impl.Current, m_Owner);
}
object IEnumerator.Current { get { return GetCurrent(); } }
State IEnumerator<State>.Current { get { return GetCurrent(); } }
public void Dispose() { }
public bool MoveNext()
{
return m_Impl.MoveNext();
}
public void Reset()
{
m_Impl.Reset();
}
}
}
private class StateImpl : State
{
public StateImpl(SimpleAnimationPlayable.IState handle, SimpleAnimation component)
{
m_StateHandle = handle;
m_Component = component;
}
private SimpleAnimationPlayable.IState m_StateHandle;
private SimpleAnimation m_Component;
bool State.enabled
{
get { return m_StateHandle.enabled; }
set
{
m_StateHandle.enabled = value;
if (value)
{
m_Component.Kick();
}
}
}
bool State.isValid
{
get { return m_StateHandle.IsValid(); }
}
float State.time
{
get { return m_StateHandle.time; }
set { m_StateHandle.time = value;
m_Component.Kick(); }
}
float State.normalizedTime
{
get { return m_StateHandle.normalizedTime; }
set { m_StateHandle.normalizedTime = value;
m_Component.Kick();}
}
float State.speed
{
get { return m_StateHandle.speed; }
set { m_StateHandle.speed = value;
m_Component.Kick();}
}
string State.name
{
get { return m_StateHandle.name; }
set { m_StateHandle.name = value; }
}
float State.weight
{
get { return m_StateHandle.weight; }
set { m_StateHandle.weight = value;
m_Component.Kick();}
}
float State.length
{
get { return m_StateHandle.length; }
}
AnimationClip State.clip
{
get { return m_StateHandle.clip; }
}
WrapMode State.wrapMode
{
get { return m_StateHandle.wrapMode; }
set { Debug.LogError("Not Implemented"); }
}
}
[System.Serializable]
public class EditorState
{
public AnimationClip clip;
public string name;
public bool defaultState;
}
protected void Kick()
{
if (!m_IsPlaying)
{
m_Graph.Play();
m_IsPlaying = true;
}
}
protected PlayableGraph m_Graph;
protected PlayableHandle m_LayerMixer;
protected PlayableHandle m_TransitionMixer;
protected Animator m_Animator;
protected bool m_Initialized;
protected bool m_IsPlaying;
protected SimpleAnimationPlayable m_Playable;
[SerializeField]
protected bool m_PlayAutomatically = true;
[SerializeField]
protected bool m_AnimatePhysics = false;
[SerializeField]
protected AnimatorCullingMode m_CullingMode = AnimatorCullingMode.CullUpdateTransforms;
[SerializeField]
protected WrapMode m_WrapMode;
[SerializeField]
protected AnimationClip m_Clip;
[SerializeField]
private EditorState[] m_States;
public SimpleAnimationPlayable Playable
{
get { return m_Playable; }
}
protected virtual void OnEnable()
{
Initialize();
m_Graph.Play();
if (m_PlayAutomatically)
{
Stop();
Play();
}
}
protected virtual void OnDisable()
{
if (m_Initialized)
{
Stop();
m_Graph.Stop();
}
}
private void Reset()
{
if (m_Graph.IsValid())
m_Graph.Destroy();
m_Initialized = false;
}
private void Initialize()
{
if (m_Initialized)
return;
m_Animator = GetComponent<Animator>();
m_Animator.updateMode = m_AnimatePhysics ? AnimatorUpdateMode.AnimatePhysics : AnimatorUpdateMode.Normal;
m_Animator.cullingMode = m_CullingMode;
m_Graph = PlayableGraph.Create();
m_Graph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
SimpleAnimationPlayable template = new SimpleAnimationPlayable();
var playable = ScriptPlayable<SimpleAnimationPlayable>.Create(m_Graph, template, 1);
m_Playable = playable.GetBehaviour();
m_Playable.onDone += OnPlayableDone;
if (m_States == null)
{
m_States = new EditorState[1];
m_States[0] = new EditorState();
m_States[0].defaultState = true;
m_States[0].name = "Default";
}
if (m_States != null)
{
foreach (var state in m_States)
{
if (state.clip)
{
m_Playable.AddClip(state.clip, state.name);
}
}
}
EnsureDefaultStateExists();
AnimationPlayableUtilities.Play(m_Animator, m_Playable.playable, m_Graph);
Play();
Kick();
m_Initialized = true;
}
private void EnsureDefaultStateExists()
{
if ( m_Playable != null && m_Clip != null && m_Playable.GetState(m_Clip.name) == null )
{
m_Playable.AddClip(m_Clip, m_Clip.name);
Kick();
}
}
protected virtual void Awake()
{
Initialize();
}
protected void OnDestroy()
{
if (m_Graph.IsValid())
{
m_Graph.Destroy();
}
}
private void OnPlayableDone()
{
m_Graph.Stop();
m_IsPlaying = false;
}
private void RebuildStates()
{
var playableStates = GetStates();
var list = new List<EditorState>();
foreach (var state in playableStates)
{
var newState = new EditorState();
newState.clip = state.clip;
newState.name = state.name;
list.Add(newState);
}
m_States = list.ToArray();
}
EditorState CreateDefaultEditorState()
{
var defaultState = new EditorState();
defaultState.name = "Default";
defaultState.clip = m_Clip;
defaultState.defaultState = true;
return defaultState;
}
static void LegacyClipCheck(AnimationClip clip)
{
if (clip && clip.legacy)
{
throw new ArgumentException(string.Format("Legacy clip {0} cannot be used in this component. Set .legacy property to false before using this clip", clip));
}
}
void InvalidLegacyClipError(string clipName, string stateName)
{
Debug.LogErrorFormat(this.gameObject,"Animation clip {0} in state {1} is Legacy. Set clip.legacy to false, or reimport as Generic to use it with SimpleAnimationComponent", clipName, stateName);
}
private void OnValidate()
{
//Don't mess with runtime data
if (Application.isPlaying)
return;
if (m_Clip && m_Clip.legacy)
{
Debug.LogErrorFormat(this.gameObject,"Animation clip {0} is Legacy. Set clip.legacy to false, or reimport as Generic to use it with SimpleAnimationComponent", m_Clip.name);
m_Clip = null;
}
//Ensure at least one state exists
if (m_States == null || m_States.Length == 0)
{
m_States = new EditorState[1];
}
//Create default state if it's null
if (m_States[0] == null)
{
m_States[0] = CreateDefaultEditorState();
}
//If first state is not the default state, create a new default state at index 0 and push back the rest
if (m_States[0].defaultState == false || m_States[0].name != "Default")
{
var oldArray = m_States;
m_States = new EditorState[oldArray.Length + 1];
m_States[0] = CreateDefaultEditorState();
oldArray.CopyTo(m_States, 1);
}
//If default clip changed, update the default state
if (m_States[0].clip != m_Clip)
m_States[0].clip = m_Clip;
//Make sure only one state is default
for (int i = 1; i < m_States.Length; i++)
{
if (m_States[i] == null)
{
m_States[i] = new EditorState();
}
m_States[i].defaultState = false;
}
//Ensure state names are unique
int stateCount = m_States.Length;
string[] names = new string[stateCount];
for (int i = 0; i < stateCount; i++)
{
EditorState state = m_States[i];
if (state.name == "" && state.clip)
{
state.name = state.clip.name;
}
#if UNITY_EDITOR
state.name = ObjectNames.GetUniqueName(names, state.name);
#endif
names[i] = state.name;
if (state.clip && state.clip.legacy)
{
InvalidLegacyClipError(state.clip.name, state.name);
state.clip = null;
}
}
m_Animator = GetComponent<Animator>();
m_Animator.updateMode = m_AnimatePhysics ? AnimatorUpdateMode.AnimatePhysics : AnimatorUpdateMode.Normal;
m_Animator.cullingMode = m_CullingMode;
}
}