forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageFade.cs
150 lines (144 loc) · 4.84 KB
/
ImageFade.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Component that when attached to an Image will fade in or out based on isFadingIn
/// </summary>
[RequireComponent(typeof(Image))]
public class ImageFade : MonoBehaviour
{
[Header("Configuration")]
[Tooltip("The Time it takes for the object to fade")]
[SerializeField] float m_timeToFade = 1.5f;
[Tooltip("The Time it takes before the object begins its fading")]
[SerializeField] float m_delayTimeToStart = 1.5f;
[Tooltip("Boolean to determing if the image is fading in or out")]
[SerializeField] bool m_isFadingIn = true;
[Tooltip("Boolean to determing if the image is a button or out")]
[SerializeField] bool m_doesHaveChildText = false;
//Sibling Components
private Image m_image;
private Text m_text;
//Member variables
private Color m_startColor;
private Color m_buttonTextStartColor;
private Color m_desiredEndColor;
private float m_timeElapsed = 0.0f;
private void OnEnable()
{
m_timeElapsed = 0.0f;
GetSiblingComponents();
//set the starting color to the color of the object
SetStartingColor();
//Start the Fade Coroutine
StartCoroutine(Fade());
}
//Function that sets the image color value determined on if you want to fade in or out
private void SetStartingColor()
{
//start color is set to the images current color
m_startColor = m_image.color;
//create new varibles to be messed with later
Color modifiedColor = m_startColor;
Color modifiedTextColor = new Color();
if (m_doesHaveChildText)
{
//if this had childed text component buttonTextStartcolor is set to the text's current color
m_buttonTextStartColor = m_text.color;
modifiedTextColor = m_buttonTextStartColor;
}
//sets the starting colors alpha values
if (m_isFadingIn)
{
//if it is fading in set alpha values to 0
modifiedColor.a = 0.0f;
modifiedTextColor.a = 0.0f;
}
//Set the current colors to be the modified colors for fading
m_image.color = modifiedColor;
if (m_doesHaveChildText)
{
m_text.color = modifiedTextColor;
}
}
/// <summary>
/// Gets the Required Sibling components
/// </summary>
private void GetSiblingComponents()
{
m_image = GetComponent<Image>();
if (m_doesHaveChildText)
{
m_text = GetComponentInChildren<Text>();
}
}
/// <summary>
/// Coroutine that fades the Image by lerping the alpha from either 1-0 or 0-1
/// </summary>
IEnumerator Fade()
{
//Wait for the delay
yield return new WaitForSeconds(m_delayTimeToStart);
//create new color
Color newColor = m_startColor;
Color newTextColor = new Color();
if (m_doesHaveChildText)
{
newTextColor = m_text.color;
}
while (m_timeElapsed < m_timeToFade)
{
float time = m_timeElapsed / m_timeToFade;
if (m_isFadingIn)
{
//set new colors alpha to the lerp value of time
newColor.a = Mathf.Lerp(0, m_startColor.a, time);
if (m_doesHaveChildText)
{
newTextColor.a = Mathf.Lerp(0, m_buttonTextStartColor.a, time);
}
}
else
{
//set new colors alpha to the lerp value of time
newColor.a = Mathf.Lerp(m_startColor.a, 0, time);
if (m_doesHaveChildText)
{
newTextColor.a = Mathf.Lerp(m_buttonTextStartColor.a, 0, time);
}
}
//set image color to the new color
if (m_doesHaveChildText)
{
m_text.color = newTextColor;
}
m_image.color = newColor;
//add deltaTime to time elapsed
m_timeElapsed += Time.deltaTime;
yield return null;
}
//when the loop finishes set the colors to be what they are supposed to be
if (m_isFadingIn)
{
m_image.color = m_startColor;
if (m_doesHaveChildText)
{
m_text.color = m_buttonTextStartColor;
}
}
else
{
//if not is fading in set the alpha values to 0
Color desiredColor = m_startColor;
desiredColor.a = 0.0f;
m_image.color = desiredColor;
if(m_doesHaveChildText)
{
desiredColor = m_buttonTextStartColor;
desiredColor.a = 0.0f;
m_text.color = desiredColor;
}
}
}
}