forked from Gkxd/Rhythmify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeColors.cs
More file actions
68 lines (58 loc) · 1.95 KB
/
Copy pathChangeColors.cs
File metadata and controls
68 lines (58 loc) · 1.95 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
using UnityEngine;
using System.Collections;
namespace Rhythmify {
[System.Serializable]
public class ColorTransition {
public Color startColor;
public Color endColor;
public bool smooth;
}
public class ChangeColors : _AbstractRhythmObject {
public ColorTransition[] colorTransitions;
public int[] indices;
public int offset;
public bool shared;
private new Renderer renderer;
override protected void init() {
renderer = GetComponent<Renderer>();
}
override protected void rhythmUpdate(int beat) {
int size = colorTransitions.Length;
if (size < 0) {
return;
}
int idx = beat + offset;
if (indices.Length > 0) {
idx = indices [idx % indices.Length];
}
if (idx < 0) {
return;
}
ColorTransition t = colorTransitions[idx % size];
if (t.smooth) {
StartCoroutine(changeSmooth(t.startColor, t.endColor, secondsPerBeat));
}
else {
if (shared) {
renderer.sharedMaterial.color = t.endColor;
}
else {
renderer.material.color = t.endColor;
}
}
}
private IEnumerator changeSmooth(Color startColor, Color endColor, float duration) {
float startTime = Time.time;
while (Time.time <= startTime + duration) {
float lerpPercent = Mathf.Clamp01((Time.time - startTime) / duration);
if (shared) {
renderer.sharedMaterial.color = Color.Lerp(startColor, endColor, lerpPercent);
}
else {
renderer.material.color = Color.Lerp(startColor, endColor, lerpPercent);
}
yield return null;
}
}
}
}