AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
FadeManager.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 System;
5 using UnityEngine;
6 
7 #if UNITY_WSA
8 #if UNITY_2017_2_OR_NEWER
9 using UnityEngine.XR.WSA;
10 #else
11 using UnityEngine.VR;
12 using UnityEngine.VR.WSA;
13 #endif
14 #endif
15 
16 namespace HoloToolkit.Unity
17 {
18  public class FadeManager : Singleton<FadeManager>
19  {
20  [Tooltip("If true, the FadeManager will update the shared material. Useful for fading multiple cameras that each render different layers.")]
21  public bool FadeSharedMaterial;
22 
23  private Material fadeMaterial;
24  private Color fadeColor = Color.black;
25 
26  private enum FadeState
27  {
28  idle = 0,
29  fadingOut,
30  FadingIn
31  }
32 
33  public bool Busy
34  {
35  get
36  {
37  return currentState != FadeState.idle;
38  }
39  }
40 
41  private FadeState currentState;
42  private float startTime;
43  private float fadeOutTime;
44  private Action fadeOutAction;
45  private float fadeInTime;
46  private Action fadeInAction;
47 
48  protected override void Awake()
49  {
50  // We want to check before calling base Awake
51 #if UNITY_WSA
52 #if UNITY_2017_2_OR_NEWER
53  if (!HolographicSettings.IsDisplayOpaque)
54 #else
55  if (VRDevice.isPresent)
56 #endif
57  {
58  Destroy(gameObject);
59  return;
60  }
61 #endif
62 
63  base.Awake();
64 
65  currentState = FadeState.idle;
66  fadeMaterial = FadeSharedMaterial
67  ? GetComponentInChildren<MeshRenderer>().sharedMaterial
68  : GetComponentInChildren<MeshRenderer>().material;
69  }
70 
71  private void Update()
72  {
73  if (Busy)
74  {
75  CalculateFade();
76  }
77  }
78 
79  private void CalculateFade()
80  {
81  float actionTime = currentState == FadeState.fadingOut ? fadeOutTime : fadeInTime;
82  float timeBusy = Time.realtimeSinceStartup - startTime;
83  float timePercentUsed = timeBusy / actionTime;
84  if (timePercentUsed >= 1.0f)
85  {
86  Action callback = currentState == FadeState.fadingOut ? fadeOutAction : fadeInAction;
87  if (callback != null)
88  {
89  callback();
90  }
91 
92  fadeColor.a = currentState == FadeState.fadingOut ? 1 : 0;
93  fadeMaterial.color = fadeColor;
94 
95  currentState = currentState == FadeState.fadingOut ? FadeState.FadingIn : FadeState.idle;
96  startTime = Time.realtimeSinceStartup;
97  }
98  else
99  {
100  fadeColor.a = currentState == FadeState.fadingOut ? timePercentUsed : 1 - timePercentUsed;
101  fadeMaterial.color = fadeColor;
102  }
103  }
104 
105  protected override void OnDestroy()
106  {
107  if (fadeMaterial != null && !FadeSharedMaterial)
108  {
109  Destroy(fadeMaterial);
110  }
111 
112  base.OnDestroy();
113  }
114 
115  public bool DoFade(float _fadeOutTime, float _fadeInTime, Action _fadedOutAction, Action _fadedInAction)
116  {
117  if (Busy)
118  {
119  Debug.Log("Already fading");
120  return false;
121  }
122 
123  fadeOutTime = _fadeOutTime;
124  fadeOutAction = _fadedOutAction;
125  fadeInTime = _fadeInTime;
126  fadeInAction = _fadedInAction;
127 
128  startTime = Time.realtimeSinceStartup;
129  currentState = FadeState.fadingOut;
130  return true;
131  }
132  }
133 }
bool DoFade(float _fadeOutTime, float _fadeInTime, Action _fadedOutAction, Action _fadedInAction)
Definition: FadeManager.cs:115
override void Awake()
Base Awake method that sets the Singleton&#39;s unique instance. Called by Unity when initializing a Mono...
Definition: FadeManager.cs:48
override void OnDestroy()
Base OnDestroy method that destroys the Singleton&#39;s unique instance. Called by Unity when destroying ...
Definition: FadeManager.cs:105
Singleton behaviour class, used for components that should only have one instance.
Definition: Singleton.cs:14