blob: ae132b3a10da2c011fa3a9b71b34d26a7339ee36 (
plain)
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
|
using System;
using System.Collections;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Events;
public class CodeAnimation : MonoBehaviour
{
[HideInInspector]
public bool isPlaying;
public bool loonIn;
public bool playInOnAwake;
public bool playInOnEnable;
public float enablePlayDelay;
public bool interuptAnimations = true;
public bool setFirstFrame;
public bool useTimeScale = true;
private Vector3 defaultScale;
private Vector3 defaultLocalPosition;
private Vector3 defaultRectPosition;
private Vector3 defaultLocalRotation;
public CodeAnimationInstance[] animations;
private RectTransform rectTransform;
[HideInInspector]
public CodeAnimationInstance.AnimationUse currentState;
[HideInInspector]
public float animationValue;
private Action AnimationChangeAction;
public void Start()
{
currentState = CodeAnimationInstance.AnimationUse.None;
rectTransform = GetComponent<RectTransform>();
for (int i = 0; i < animations.Length; i++)
{
animations[i].animationSpeed += UnityEngine.Random.Range((0f - animations[i].animationSpeed) * animations[i].randomSpeedAmount, animations[i].animationSpeed * animations[i].randomSpeedAmount);
if (animations[i].animationUse == CodeAnimationInstance.AnimationUse.In)
{
float num = animations[i].curve[animations[i].curve.length - 1].value - animations[i].curve[0].value;
if ((bool)rectTransform && animations[i].animationType == CodeAnimationInstance.AnimationType.rectPosition)
{
rectTransform.anchoredPosition += num * (Vector2)animations[i].direction * (0f - animations[i].multiplier);
}
}
}
SetDefaults();
if (playInOnAwake)
{
PlayIn();
}
if (setFirstFrame)
{
ApplyValues(animations[0], 0f);
}
}
private void OnEnable()
{
if (playInOnEnable)
{
StartCoroutine(DelayEnablePlay());
}
}
private IEnumerator DelayEnablePlay()
{
yield return new WaitForSecondsRealtime(enablePlayDelay);
PlayIn();
}
private void SetDefaults()
{
defaultScale = base.transform.localScale;
defaultLocalPosition = base.transform.localPosition;
defaultLocalRotation = base.transform.localRotation.eulerAngles;
if ((bool)rectTransform)
{
defaultRectPosition = rectTransform.anchoredPosition;
}
}
public void Animate(CodeAnimationInstance.AnimationUse use)
{
currentState = use;
if (interuptAnimations)
{
StopAllCoroutines();
}
for (int i = 0; i < animations.Length; i++)
{
if (animations[i].animationUse == use)
{
StartCoroutine(PlayAnimations(animations[i]));
}
}
}
[Button]
public void PlayIn()
{
Animate(CodeAnimationInstance.AnimationUse.In);
}
[Button]
public void PlayOut()
{
Animate(CodeAnimationInstance.AnimationUse.Out);
}
[Button]
public void PlayBoop()
{
Animate(CodeAnimationInstance.AnimationUse.Boop);
}
private IEnumerator PlayAnimations(CodeAnimationInstance animation)
{
isPlaying = true;
animation.startEvent.Invoke();
StartCoroutine(DelayTimedEvent(animation.eventTiming / animation.animationSpeed, animation.timedEvent));
float t = animation.curve.keys[animation.curve.keys.Length - 1].time;
float c = 0f;
while (c < t)
{
ApplyValues(animation, c);
c += (useTimeScale ? TimeHandler.deltaTime : Time.unscaledDeltaTime) * animation.animationSpeed;
yield return null;
}
ApplyValues(animation, t);
isPlaying = false;
animation.endEvent.Invoke();
if (loonIn)
{
PlayIn();
}
}
private void ApplyValues(CodeAnimationInstance animation, float time)
{
if (animation.animationType == CodeAnimationInstance.AnimationType.rectPosition && (bool)rectTransform)
{
rectTransform.anchoredPosition = defaultRectPosition + animation.direction * animation.curve.Evaluate(time) * animation.multiplier;
}
if (animation.animationType == CodeAnimationInstance.AnimationType.position)
{
base.transform.localPosition = defaultLocalPosition + animation.direction * animation.curve.Evaluate(time) * animation.multiplier;
}
if (animation.animationType == CodeAnimationInstance.AnimationType.scale)
{
Vector3 localScale = defaultScale * animation.curve.Evaluate(time) * animation.multiplier;
if (!animation.X)
{
localScale.x = base.transform.localScale.x;
}
if (!animation.Y)
{
localScale.y = base.transform.localScale.y;
}
if (!animation.Z)
{
localScale.z = base.transform.localScale.z;
}
base.transform.localScale = localScale;
}
if (animation.animationType == CodeAnimationInstance.AnimationType.floatNumber)
{
animationValue = animation.curve.Evaluate(time) * animation.multiplier;
}
if (animation.animationType == CodeAnimationInstance.AnimationType.rotation)
{
base.transform.localRotation = Quaternion.Euler(animation.curve.Evaluate(time) * animation.direction + defaultLocalRotation);
}
if (AnimationChangeAction != null)
{
AnimationChangeAction();
}
}
public void AddAnimationChangeAction(Action action)
{
AnimationChangeAction = (Action)Delegate.Combine(AnimationChangeAction, action);
}
private IEnumerator DelayTimedEvent(float time, UnityEvent eventToCall)
{
yield return new WaitForSeconds(time);
eventToCall.Invoke();
}
}
|