-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
CanvasController.cs
157 lines (138 loc) · 4.74 KB
/
CanvasController.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif
#if UNITY_EDITOR
/// <summary>Inspector for the CanvasController component.</summary>
[CustomEditor(typeof(CanvasController))]
[ExecuteInEditMode]
public class CanvasControllerInspector : Editor
{
//Fetches or creates the CanvasGroup on enable
private void OnEnable()
{
CanvasController Controller = (CanvasController)target;
Controller.SetCanvasGroup();
}
}
#endif
/// <summary>Component that provides fade in/out functionality for a canvas group.</summary>
[DisallowMultipleComponent]
[RequireComponent(typeof(CanvasGroup))]
public class CanvasController : MonoBehaviour
{
//Private Variables
/// <summary>Default speed for fade in and fade out.</summary>
[Tooltip("Default speed for fade in and fade out.")]
public float DefaultSpeed = 0.4f;
/// <summary>Automatically fade on activating the GameObject.</summary>
[Tooltip("Automatically fade on activating the GameObject.")]
public bool AutoFade = true;
/// <summary>Blocks raycasts whilst fading.</summary>
[Tooltip("Blocks raycasts whilst fading.")]
public bool BlockRaycasts = true;
//Private Variables
/// <summary>The CanvasGroup to fade in and out.</summary>
private CanvasGroup Elements;
/// <summary>Whether or not the CanvasController is initialised.</summary>
private bool Initialised = false;
//Initialises
private void Awake() { Initialise(); }
/// <summary>Initialises the CanvasController.</summary>
public void Initialise() { Initialise(false); }
/// <summary>Sets the CanvasGroup of this CanvasController</summary>
public void SetCanvasGroup()
{
if (Elements == null) { Elements = GetComponent<CanvasGroup>(); }
if (Elements == null) { Elements = gameObject.AddComponent<CanvasGroup>(); }
}
/// <summary>Initialises the CanvasController.</summary>
/// <param name="Override">Re-initialises even if already initialised.</param>
public void Initialise(bool Override)
{
if (!Initialised || Override)
{
SetCanvasGroup();
Initialised = true;
}
}
/// <summary>Fades out.</summary>
public void FadeOut() { FadeOut(DefaultSpeed); }
/// <summary>Fades out.</summary>
/// <param name="Duration">Duration of the fade.</param>
public void FadeOut(float Duration)
{
if (gameObject.activeInHierarchy)
{
//Stops any other fading
StopAllCoroutines();
//Begins Fade
StartCoroutine(FadeOutComponents(Duration));
}
}
/// <summary>Fades out.</summary>
/// <param name="Duration">Duration of the fade.</param>
IEnumerator FadeOutComponents(float Duration)
{
//Blocks raycasting
bool WasRaycaster = false;
if (BlockRaycasts)
{
WasRaycaster = Elements.blocksRaycasts;
Elements.blocksRaycasts = false;
}
//Fades CanvasGroup
Elements.alpha = 1f;
while (Elements.alpha > 0)
{
if (Elements.gameObject.activeInHierarchy) { Elements.alpha -= Time.unscaledDeltaTime / Duration; }
yield return null;
}
Elements.alpha = 1f;
//Finishes fade
if (BlockRaycasts && WasRaycaster) { Elements.blocksRaycasts = true; }
gameObject.SetActive(false);
}
/// <summary>Fades in.</summary>
public void FadeIn() { FadeIn(DefaultSpeed); }
/// <summary>Fades in.</summary>
/// <param name="Duration">Duration of the fade.</param>
public void FadeIn(float Length)
{
if (gameObject.activeInHierarchy)
{
//Stops any other fading
if (!Initialised) { Initialise(); }
StopAllCoroutines();
//Begins Fade
StartCoroutine(FadeInComponents(Length));
}
}
/// <summary>Fades in.</summary>
/// <param name="Duration">Duration of the fade.</param>
IEnumerator FadeInComponents(float Duration)
{
//Blocks raycasting
bool WasRaycaster = false;
if (BlockRaycasts)
{
WasRaycaster = Elements.blocksRaycasts;
Elements.blocksRaycasts = false;
}
//Fades CanvasGroup
Elements.alpha = 0f;
while (Elements.alpha < 1f)
{
if (Elements.gameObject.activeInHierarchy) { Elements.alpha += Time.unscaledDeltaTime / Duration; }
yield return null;
}
Elements.alpha = 1f;
//Finishes fade
if (BlockRaycasts && WasRaycaster) { Elements.blocksRaycasts = true; }
}
//Auto fade in
void OnEnable() { if (AutoFade) { FadeIn(); } }
}