-
Notifications
You must be signed in to change notification settings - Fork 4
/
AnimationUtilities.cs
163 lines (134 loc) · 4.71 KB
/
AnimationUtilities.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
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
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class AnimationUtilities : MonoBehaviour {
public AudioClip[] Sounds;
public float[] Delays;
float delayRemaining;
float preDelaySpeed;
public float[] Speeds;
public GameObject[] Objects;
public Sprite[] Sprites;
Animator animator;
SpriteRenderer spriteRenderer;
void Awake() {
animator = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
void FixedUpdate() {
// handle delays
{
if (delayRemaining > 0) {
animator.speed = 0;
delayRemaining -= Time.deltaTime;
if (delayRemaining < 0) {
delayRemaining = 0;
animator.speed = preDelaySpeed;
}
}
}
}
public void LoadScene(string scene) {
SceneManager.LoadScene(scene);
}
public void LoadSceneById(int scene) {
SceneManager.LoadScene(scene);
}
public void DelayByIndexOncePerPlay(int delayIndex) {
if (delayIndex >= 0 && delayIndex < Delays.Length) {
DelayByValueOncePerPlay(Delays[delayIndex]);
} else {
Debug.LogError("No delay with index " + delayIndex + " found on this AnimationUtilities component!");
}
}
public void DelayByIndexEveryLoop(int delayIndex) {
if (delayIndex >= 0 && delayIndex < Delays.Length) {
DelayByValueEveryLoop(Delays[delayIndex]);
} else {
Debug.LogError("No delay with index " + delayIndex + " found on this AnimationUtilities component!");
}
}
public void DelayByValueOncePerPlay(float delay) {
if (animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1) // this means it's after the first loop
return;
DelayByValueEveryLoop(delay);
}
public void DelayByValueEveryLoop(float delay) {
if (animator.speed != 0) {
preDelaySpeed = animator.speed;
}
delayRemaining = delay;
}
public void SetSpeedByIndex(int speedIndex) {
if (speedIndex >= 0 && speedIndex < Speeds.Length) {
SetSpeedByValue(Speeds[speedIndex]);
} else {
Debug.LogError("No speed with index " + speedIndex + " found on this AnimationUtilities component!");
}
}
public void SetSpeedByValue(float speed) {
animator.speed = speed;
}
public void PlaySoundByIndex(int soundIndex) {
if (soundIndex >= 0 && soundIndex < Sounds.Length) {
Sounds[soundIndex].Play();
} else {
Debug.LogError("No sound with index " + soundIndex + " found on this AnimationUtilities component!");
}
}
public void PlaySoundByName(string soundName) {
for (int i = 0; i < Sounds.Length; i++) {
if (Sounds[i].name == soundName) {
PlaySoundByIndex(i);
}
}
}
public void SpawnPrefab(GameObject prefab) {
Instantiate(prefab);
}
public void EnableGameObjectByIndex(int objIndex) {
if (objIndex >= 0 && objIndex < Objects.Length) {
Objects[objIndex].SetActive(true);
} else {
Debug.LogError("No object with index " + objIndex + " found on this AnimationUtilities component!");
}
}
public void DisableGameObjectByIndex(int objIndex) {
if (objIndex >= 0 && objIndex < Objects.Length) {
Objects[objIndex].SetActive(false);
} else {
Debug.LogError("No object with index " + objIndex + " found on this AnimationUtilities component!");
}
}
public void DestroySelf() {
Destroy(gameObject);
}
public void DestroyParent() {
if (gameObject.transform.parent != null)
Destroy(gameObject.transform.parent.gameObject);
}
public void ReleaseSelf() {
gameObject.Release();
}
public void ReleaseParent() {
if (gameObject.transform.parent != null)
gameObject.transform.parent.gameObject.Release();
}
public void PlayAnimationState(string state) {
animator.Play(state);
}
public void SetSpriteByIndex(int spriteIndex) {
if (spriteIndex >= 0 && spriteIndex < Sprites.Length) {
spriteRenderer.sprite = Sprites[spriteIndex];
} else {
Debug.LogError("No sprite with index " + spriteIndex + " found on this AnimationUtilities component!");
}
}
public void SetSpriteByName(string spritename) {
for (int i = 0; i < Sprites.Length; i++) {
if (Sprites[i].name == spritename) {
SetSpriteByIndex(i);
}
}
}
}