-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextFader.cs
More file actions
46 lines (39 loc) · 1004 Bytes
/
TextFader.cs
File metadata and controls
46 lines (39 loc) · 1004 Bytes
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
using UnityEngine;
using TMPro;
public class TextFader : MonoBehaviour
{
public TextMeshProUGUI textMesh;
public float fadeDuration = 1f;
private void Awake()
{
Color c = textMesh.color;
c.a = 0f;
textMesh.color = c;
}
public void FadeIn()
{
StopAllCoroutines();
StartCoroutine(FadeText(1f));
}
public void FadeOut()
{
StopAllCoroutines();
StartCoroutine(FadeText(0f));
}
private System.Collections.IEnumerator FadeText(float targetAlpha)
{
float startAlpha = textMesh.color.a;
float time = 0f;
while (time < fadeDuration)
{
Color c = textMesh.color;
c.a = Mathf.Lerp(startAlpha, targetAlpha, time / fadeDuration);
textMesh.color = c;
time += Time.deltaTime;
yield return null;
}
Color final = textMesh.color;
final.a = targetAlpha;
textMesh.color = final;
}
}