AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
FadeObject.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 
6 namespace HoloToolkit.Examples.InteractiveElements
7 {
13  public class FadeObject : MonoBehaviour
14  {
15  [SerializeField]
16  private float fadeTime = 0.5f;
17 
21  public float FadeTime
22  {
23  get
24  {
25  return fadeTime;
26  }
27 
28  set
29  {
30  fadeTime = value;
31  }
32  }
33 
34  private bool autoFadeIn = false;
35 
39  public bool AutoFadeIn
40  {
41  get
42  {
43  return autoFadeIn;
44  }
45 
46  set
47  {
48  autoFadeIn = value;
49  }
50  }
51 
52  private float fadeCounter = 0;
53  private Color cachedColor;
54  private bool fadingIn = true;
55 
56  //cache material to prevent memory leak
57  private Material cachedMaterial;
58 
59  private void Awake()
60  {
61  cachedMaterial = this.GetComponent<Renderer>().material;
62  cachedColor = cachedMaterial.color;
63  }
64 
65  private void Start()
66  {
67  if (AutoFadeIn)
68  {
69  FadeIn(true);
70  }
71  }
72 
73  private void OnEnable()
74  {
75  if (AutoFadeIn)
76  {
77  FadeIn(true);
78  }
79  }
80 
85  public void FadeIn(bool resetFade)
86  {
87  if (cachedMaterial != null)
88  {
89  cachedColor = cachedMaterial.color;
90  }
91 
92  if (resetFade && cachedMaterial)
93  {
94  cachedColor.a = 0;
95  cachedMaterial.color = cachedColor;
96  }
97 
98  fadeCounter = 0;
99  fadingIn = true;
100  }
101 
106  public void ResetFade(float value)
107  {
108  if (cachedMaterial != null)
109  {
110  cachedColor = cachedMaterial.color;
111  cachedColor.a = value;
112  cachedMaterial.color = cachedColor;
113  }
114 
115  fadeCounter = 0;
116  }
117 
122  public void FadeOut(bool resetStartValue)
123  {
124  if (cachedMaterial != null)
125  {
126  cachedColor = cachedMaterial.color;
127  }
128 
129  if (resetStartValue && cachedMaterial)
130  {
131  cachedColor.a = 1;
132  cachedMaterial.color = cachedColor;
133  }
134 
135  fadeCounter = 0;
136  fadingIn = false;
137  }
138 
139  private void Update()
140  {
141  if (fadeCounter < FadeTime)
142  {
143  fadeCounter += Time.deltaTime;
144  if (fadeCounter > FadeTime)
145  {
146  fadeCounter = FadeTime;
147  }
148 
149  float percent = fadeCounter / FadeTime;
150 
151  if (!fadingIn)
152  {
153  percent = 1 - percent;
154  if (percent < cachedColor.a)
155  {
156  cachedColor.a = percent;
157  }
158  }
159  else
160  {
161  if (percent > cachedColor.a)
162  {
163  cachedColor.a = percent;
164  }
165  }
166 
167  if (cachedMaterial != null)
168  {
169  cachedMaterial.color = cachedColor;
170  }
171  }
172  }
173 
178  public void OnDestroy()
179  {
180  Destroy(cachedMaterial);
181  }
182  }
183 }
This class describes and performs a fade. It can be used to create a fadeIn or a fadeOut or both...
Definition: FadeObject.cs:13
void FadeOut(bool resetStartValue)
start the Fade out effect.
Definition: FadeObject.cs:122
void OnDestroy()
Event handler when object is deleted This cleans up cached material.
Definition: FadeObject.cs:178
void ResetFade(float value)
Resets the color to original before fade effect
Definition: FadeObject.cs:106
void FadeIn(bool resetFade)
Begins the Fade in effect
Definition: FadeObject.cs:85