-
Notifications
You must be signed in to change notification settings - Fork 0
/
PopupText.cs
70 lines (59 loc) · 1.98 KB
/
PopupText.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
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PopupText : MonoBehaviour
{
Text uiText;
GameObject popup;
Color startColor = new Color(255, 255, 255, 0);
Color endColor = new Color(255, 255, 255, 1);
void Start()
{
uiText = GetComponent<Text>();
popup = Resources.Load<GameObject>("Prefabs/RandomPopup");
string text_on_screen = "Unknown Level";
if (SceneManager.GetActiveScene().name == "level1") text_on_screen = "1. Nature Calls";
if (SceneManager.GetActiveScene().name == "level2") text_on_screen = "2. First Line of Defence";
if (SceneManager.GetActiveScene().name == "level3") text_on_screen = "3. The Outer Shell";
if (SceneManager.GetActiveScene().name == "level4") text_on_screen = "4. 1-UP";
if (SceneManager.GetActiveScene().name == "level5") text_on_screen = "5. Pay up";
Popup(text_on_screen);
}
public void Popup(string text)
{
uiText.text = text;
StartCoroutine(_Popup());
}
private IEnumerator _Popup()
{
yield return new WaitForSeconds(1);
yield return StartCoroutine(FadeIn());
yield return new WaitForSeconds(3);
yield return StartCoroutine(FadeOut());
}
private IEnumerator FadeIn()
{
float timestamp = 0;
do
{
timestamp += Time.deltaTime;
uiText.color = Color.Lerp(startColor, endColor, timestamp);
yield return new WaitForEndOfFrame();
} while (uiText.color != endColor);
}
private IEnumerator FadeOut()
{
float timestamp = 0;
do
{
timestamp += Time.deltaTime;
uiText.color = Color.Lerp(endColor, startColor, timestamp);
yield return new WaitForEndOfFrame();
} while (uiText.color != startColor);
}
public void SpawnRandomPopup()
{
Instantiate(popup, transform);
}
}