aboutsummaryrefslogtreecommitdiff
path: root/td/Assets/Scripts/FlashColor.cs
blob: b5da678cbf71b824865cd7ae5b3fa1da6498f6db (plain) (blame)
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class FlashColor : MonoBehaviour {

	public Color Color1;
	public Color Color2;
	public float TransitionTime;
	public bool HideAfterCountdown;
	public float DisplayTime;

	private Text _text;
	public float TimeSinceBorn;

	void Start() {
		TimeSinceBorn = 0;
		_text = gameObject.GetComponent<Text>();
		StartCoroutine("Flash");
	}
	
	void OnEnable() {
		TimeSinceBorn = 0;
		_text = gameObject.GetComponent<Text>();
		StartCoroutine("Flash");
	}

	void Update () {
		if (HideAfterCountdown) {
			TimeSinceBorn += Time.deltaTime;
			if (TimeSinceBorn >= DisplayTime) {
				gameObject.SetActive(false);
			}
		}
	}

	IEnumerator Flash() {
		while (true) {
			float elapsedTime = 0.0f;
			while (elapsedTime < TransitionTime) {
				elapsedTime += Time.deltaTime;
				_text.color = Color.Lerp(Color1, Color2, (elapsedTime / TransitionTime));
				yield return null;
			}

			elapsedTime = 0.0f;
			while (elapsedTime < TransitionTime) {
				elapsedTime += Time.deltaTime;
				_text.color = Color.Lerp(Color2, Color1, (elapsedTime / TransitionTime));
				yield return null;
			}
			yield return null;
		}
	}

	
}