forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFadeManager.cs
More file actions
133 lines (114 loc) · 3.66 KB
/
FadeManager.cs
File metadata and controls
133 lines (114 loc) · 3.66 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
125
126
127
128
129
130
131
132
133
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using UnityEngine;
#if UNITY_WSA
#if UNITY_2017_2_OR_NEWER
using UnityEngine.XR.WSA;
#else
using UnityEngine.VR;
using UnityEngine.VR.WSA;
#endif
#endif
namespace HoloToolkit.Unity
{
public class FadeManager : Singleton<FadeManager>
{
[Tooltip("If true, the FadeManager will update the shared material. Useful for fading multiple cameras that each render different layers.")]
public bool FadeSharedMaterial;
private Material fadeMaterial;
private Color fadeColor = Color.black;
private enum FadeState
{
idle = 0,
fadingOut,
FadingIn
}
public bool Busy
{
get
{
return currentState != FadeState.idle;
}
}
private FadeState currentState;
private float startTime;
private float fadeOutTime;
private Action fadeOutAction;
private float fadeInTime;
private Action fadeInAction;
protected override void Awake()
{
// We want to check before calling base Awake
#if UNITY_WSA
#if UNITY_2017_2_OR_NEWER
if (!HolographicSettings.IsDisplayOpaque)
#else
if (VRDevice.isPresent)
#endif
{
Destroy(gameObject);
return;
}
#endif
base.Awake();
currentState = FadeState.idle;
fadeMaterial = FadeSharedMaterial
? GetComponentInChildren<MeshRenderer>().sharedMaterial
: GetComponentInChildren<MeshRenderer>().material;
}
private void Update()
{
if (Busy)
{
CalculateFade();
}
}
private void CalculateFade()
{
float actionTime = currentState == FadeState.fadingOut ? fadeOutTime : fadeInTime;
float timeBusy = Time.realtimeSinceStartup - startTime;
float timePercentUsed = timeBusy / actionTime;
if (timePercentUsed >= 1.0f)
{
Action callback = currentState == FadeState.fadingOut ? fadeOutAction : fadeInAction;
if (callback != null)
{
callback();
}
fadeColor.a = currentState == FadeState.fadingOut ? 1 : 0;
fadeMaterial.color = fadeColor;
currentState = currentState == FadeState.fadingOut ? FadeState.FadingIn : FadeState.idle;
startTime = Time.realtimeSinceStartup;
}
else
{
fadeColor.a = currentState == FadeState.fadingOut ? timePercentUsed : 1 - timePercentUsed;
fadeMaterial.color = fadeColor;
}
}
protected override void OnDestroy()
{
if (fadeMaterial != null && !FadeSharedMaterial)
{
Destroy(fadeMaterial);
}
base.OnDestroy();
}
public bool DoFade(float _fadeOutTime, float _fadeInTime, Action _fadedOutAction, Action _fadedInAction)
{
if (Busy)
{
Debug.Log("Already fading");
return false;
}
fadeOutTime = _fadeOutTime;
fadeOutAction = _fadedOutAction;
fadeInTime = _fadeInTime;
fadeInAction = _fadedInAction;
startTime = Time.realtimeSinceStartup;
currentState = FadeState.fadingOut;
return true;
}
}
}