CanvasFade.cs
4.36 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using Sirenix.OdinInspector;
namespace Globacore
{
namespace UI
{
/// <summary>
/// Simple Tools to fade a canvas group In/Out
/// </summary>
[RequireComponent(typeof(CanvasGroup))]
public class CanvasFade : MonoBehaviour
{
#region Public Variables
[Header("Fade In Settings")]
//[BoxGroup("Fade In Settings")]
public float m_fadeInTime = 1.0f;
//[BoxGroup("Fade In Settings")]
public float m_fadeInDelay = 0.0f;
[Header("Fade Out Settings")]
//[BoxGroup("Fade In Settings")]
public float m_fadeOutTime = 1.0f;
//[BoxGroup("Fade In Settings")]
public float m_fadeOutDelay = 0.0f;
#endregion
#region Private Variables
private CanvasGroup canvasGroup;
#endregion
#region Init
private void Start()
{
canvasGroup = GetComponent<CanvasGroup>();
}
#endregion
#region Public Methods
/// <summary>
/// fade in this canvas group with values set in the inspector
/// </summary>
public void FadeIn()
{
StartCoroutine(CanvasFadeIn(m_fadeInTime, m_fadeInDelay));
}
/// <summary>
/// Fade In this CanvasGroup with override values
/// </summary>
/// <param name="floatTime"></param>
/// <param name="delay"></param>
public void FadeIn(float floatTime, float delay)
{
StartCoroutine(CanvasFadeIn(floatTime, delay));
}
/// <summary>
/// fade out this canvas group with values set in the inspector
/// </summary>
public void FadeOut()
{
StartCoroutine(CanvasFadeOut(m_fadeOutTime, m_fadeOutDelay));
}
/// <summary>
/// Fade out this CanvasGroup with override values
/// </summary>
/// <param name="floatTime"></param>
/// <param name="delay"></param>
public void FadeOut(float floatTime, float delay)
{
StartCoroutine(CanvasFadeOut(floatTime, delay));
}
#endregion
#region Private Methods
/// <summary>
/// Fade in the canvas group this is attached to
/// </summary>
/// <param name="fadeTime"></param>
/// <param name="delay"></param>
/// <returns></returns>
private IEnumerator CanvasFadeIn(float fadeTime, float delay)
{
yield return new WaitForSeconds(delay);
float elapsedTime = 0;
while (elapsedTime < fadeTime)
{
if (elapsedTime + Time.deltaTime < fadeTime)
{
elapsedTime += Time.deltaTime;
}
else
{
elapsedTime += (fadeTime - elapsedTime);
}
//Debug.Log(elapsedTime);
canvasGroup.alpha = elapsedTime / fadeTime;
yield return null;
}
}
/// <summary>
/// Fade out the canvas group this is attached to
/// </summary>
/// <param name="fadeTime"></param>
/// <param name="delay"></param>
/// <returns></returns>
private IEnumerator CanvasFadeOut(float fadeTime, float delay)
{
float elapsedTime = 0;
yield return new WaitForSeconds(delay);
while (elapsedTime < fadeTime)
{
if (elapsedTime + Time.deltaTime < fadeTime)
{
elapsedTime += Time.deltaTime;
}
else
{
elapsedTime += (fadeTime - elapsedTime);
}
canvasGroup.alpha = 1 - (elapsedTime / fadeTime);
yield return null;
}
}
#endregion
}
}
}