-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorChanger.cs
72 lines (52 loc) · 2.27 KB
/
ColorChanger.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
using UnityEngine;
using System.Collections;
using TMPro;
public class ColorChanger : MonoBehaviour
{
[SerializeField] Gradient gradientText;
[SerializeField] float gradientSpeed = .1f;
private TMP_Text m_TextComponent;
private float totalTime;
void Awake()
{
m_TextComponent = GetComponent<TMP_Text>();
}
void Start()
{
StartCoroutine(AnimateVertexColors());
}
IEnumerator AnimateVertexColors()
{
m_TextComponent.ForceMeshUpdate();
TMP_TextInfo textInfo = m_TextComponent.textInfo;
int currentCharacter = 0;
Color32[] newVertexColors;
Color32 c0 = m_TextComponent.color;
while (true)
{
int characterCount = textInfo.characterCount;
if (characterCount == 0)
{
yield return new WaitForSeconds(0.25f);
continue;
}
int materialIndex = textInfo.characterInfo[currentCharacter].materialReferenceIndex;
newVertexColors = textInfo.meshInfo[materialIndex].colors32;
int vertexIndex = textInfo.characterInfo[currentCharacter].vertexIndex;
if (textInfo.characterInfo[currentCharacter].isVisible)
{
//c0 = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
float offset = (currentCharacter / characterCount);
c0 = gradientText.Evaluate((totalTime + offset) % 1);
totalTime += Time.deltaTime;
newVertexColors[vertexIndex + 0] = c0;
newVertexColors[vertexIndex + 1] = c0;
newVertexColors[vertexIndex + 2] = c0;
newVertexColors[vertexIndex + 3] = c0;
m_TextComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
}
currentCharacter = (currentCharacter + 1) % characterCount;
yield return new WaitForSeconds(gradientSpeed);
}
}
}