-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimationExtension.cs
88 lines (78 loc) · 2.62 KB
/
AnimationExtension.cs
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
using System;
using UnityEngine;
namespace Aya.Extension
{
public static class AnimationExtension
{
#region Event
public static Animation AddEvent(this Animation animation, string clipName, string methodName, float time)
{
var clip = animation[clipName].clip;
var animationEvent = new AnimationEvent
{
functionName = methodName,
time = time
};
clip.AddEvent(animationEvent);
return animation;
}
public static Animation RemoveEvent(this Animation animation, string clipName, string methodName)
{
var clip = animation[clipName].clip;
var list = clip.events.ToList();
for (var i = 0; i < list.Count; i++)
{
var e = list[i];
if (e.functionName != methodName) continue;
list.Remove(e);
break;
}
clip.events = list.ToArray();
return animation;
}
#endregion
public static Animation SetSpeed(this Animation animation, float speed)
{
animation[animation.clip.name].speed = speed;
return animation;
}
public static Animation SetTime(this Animation animation, float time)
{
var clip = animation[animation.clip.name].clip;
clip.SampleAnimation(animation.gameObject, time);
return animation;
}
public static Animation SetProgress(this Animation animation, float progress)
{
var clip = animation[animation.clip.name].clip;
var time = clip.length * progress;
clip.SampleAnimation(animation.gameObject, time);
return animation;
}
public static AnimationClip GetPlayingClip(this Animation animation)
{
AnimationClip clip = null;
var weight = 0f;
var enumerator = animation.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
var current = (AnimationState) enumerator.Current;
if (current == null) continue;
if (!current.enabled || (!(weight < current.weight))) continue;
weight = current.weight;
clip = current.clip;
}
}
finally
{
if (enumerator is IDisposable disposable)
{
disposable.Dispose();
}
}
return clip;
}
}
}