Elin Decompiled Documentation EA 23.264 Nightly
Loading...
Searching...
No Matches
RainbowTextColor.cs
Go to the documentation of this file.
1using System.Collections;
2using UnityEngine;
3using UnityEngine.UI;
4
5[DisallowMultipleComponent]
6[RequireComponent(typeof(Text))]
7public sealed class RainbowTextColor : MonoBehaviour
8{
9 [Header("Duration")]
10 [Tooltip("虹色アニメーションを行う秒数")]
11 [Min(0f)]
12 public float duration = 3f;
13
14 [Header("Speed")]
15 [Tooltip("1秒あたり何周Hueを回すか(1=1周/秒)")]
16 [Min(0f)]
17 public float cyclesPerSecond = 1f;
18
19 [Header("Look")]
20 [Tooltip("彩度")]
21 [Range(0f, 1f)]
22 public float saturation = 1f;
23
24 [Tooltip("明度")]
25 [Range(0f, 1f)]
26 public float value = 1f;
27
28 [Tooltip("元のアルファに対して掛ける係数(1=そのまま)")]
29 [Range(0f, 1f)]
30 public float alphaMultiplier = 1f;
31
32 [Header("Playback")]
33 [Tooltip("開始時に自動再生")]
34 public bool playOnEnable = true;
35
36 [Tooltip("終了時に元の色へ戻す")]
37 public bool restoreOriginalColor = true;
38
39 private Text _text;
40
41 private Coroutine _routine;
42
44
45 private void Awake()
46 {
47 _text = GetComponent<Text>();
48 _originalColor = _text.color;
49 }
50
51 private void OnEnable()
52 {
53 if (playOnEnable)
54 {
55 Play();
56 }
57 }
58
59 private void OnDisable()
60 {
61 Stop();
62 if (restoreOriginalColor && _text != null)
63 {
64 _text.color = _originalColor;
65 }
66 }
67
68 public void Play()
69 {
70 if (_text == null)
71 {
72 _text = GetComponent<Text>();
73 }
74 if (!(_text == null))
75 {
76 _originalColor = _text.color;
77 if (_routine != null)
78 {
79 StopCoroutine(_routine);
80 }
81 _routine = StartCoroutine(Run());
82 }
83 }
84
85 public void Stop()
86 {
87 if (_routine != null)
88 {
89 StopCoroutine(_routine);
90 _routine = null;
91 }
92 if (restoreOriginalColor && _text != null)
93 {
94 _text.color = _originalColor;
95 }
96 }
97
98 private IEnumerator Run()
99 {
100 if (duration <= 0f)
101 {
102 _routine = null;
103 yield break;
104 }
105 float elapsed = 0f;
106 while (elapsed < duration)
107 {
108 elapsed += Time.unscaledDeltaTime;
109 Color color = Color.HSVToRGB(Mathf.Repeat(elapsed * cyclesPerSecond, 1f), saturation, value);
110 color.a = _originalColor.a * alphaMultiplier;
111 _text.color = color;
112 yield return null;
113 }
115 {
116 _text.color = _originalColor;
117 }
118 _routine = null;
119 }
120}
IEnumerator Run()