-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSplashScreen.js
168 lines (134 loc) · 3.36 KB
/
SplashScreen.js
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
158
159
160
161
162
163
164
165
166
167
168
// SplashScreen Script
//
// http://wiki.unity3d.com/index.php?title=SplashScreen
//
// This is the same script as above (only a few redundant things were removed), just converted to UnityScript
// to help out those sticking to unity script, and to help those just starting out with it.
// Quantum Fusion Studios
var guiDepth : int = 0;
var levelToLoad : String = "";
var splashLogo : Texture2D;
var fadeSpeed : float = 0.3;
var waitTime : float = 0.5;
var waitForInput : boolean = false;
var startAutomatically : boolean = true;
var timeFadingInFinished : float = 0.0;
var logoPositioning : LogoPositioning;
var splashType : SplashType;
enum SplashType {LoadNextLevelThenFadeOut, FadeOutThenLoadNextLevel}
enum LogoPositioning {Centered, Stretched}
private var alpha : float = 0.0;
private var status : FadeStatus = FadeStatus.FadeIn;
private var oldCam : Camera;
private var oldCamGO : GameObject;
private var splashLogoPos : Rect;
private var loadingNextLevel : boolean = false;
private enum FadeStatus {Paused, FadeIn, FadeWaiting, FadeOut}
function Start()
{
if (startAutomatically)
{
status = FadeStatus.FadeIn;
}
else
{
status = FadeStatus.Paused;
}
oldCam = Camera.main;
oldCamGO = Camera.main.gameObject;
if (logoPositioning == LogoPositioning.Centered)
{
splashLogoPos.x = (Screen.width * 0.5) - (splashLogo.width * 0.5);
splashLogoPos.y = (Screen.height * 0.5) - (splashLogo.height * 0.5);
splashLogoPos.width = splashLogo.width;
splashLogoPos.height = splashLogo.height;
}
else
{
splashLogoPos.x = 0;
splashLogoPos.y = 0;
splashLogoPos.width = Screen.width;
splashLogoPos.height = Screen.height;
}
if (splashType == SplashType.LoadNextLevelThenFadeOut)
{
DontDestroyOnLoad(this);
DontDestroyOnLoad(Camera.main);
}
if ((Application.levelCount <= 1) || (levelToLoad == ""))
{
Debug.LogWarning("Invalid levelToLoad value.");
}
}
function Update()
{
switch(status)
{
case FadeStatus.FadeIn:
alpha += fadeSpeed * Time.deltaTime;
break;
case FadeStatus.FadeWaiting:
if ((!waitForInput && Time.time >= timeFadingInFinished + waitTime) || (waitForInput && Input.anyKey))
{
status = FadeStatus.FadeOut;
}
break;
case FadeStatus.FadeOut:
alpha += -fadeSpeed * Time.deltaTime;
break;
}
}
function OnGUI()
{
GUI.depth = guiDepth;
if (splashLogo != null)
{
GUI.color = Color(GUI.color.r, GUI.color.g, GUI.color.b, Mathf.Clamp01(alpha));
GUI.DrawTexture(splashLogoPos, splashLogo);
if (alpha > 1.0)
{
status = FadeStatus.FadeWaiting;
timeFadingInFinished = Time.time;
alpha = 1.0;
if (splashType == SplashType.LoadNextLevelThenFadeOut)
{
oldCam.depth = -1000;
loadingNextLevel = true;
if ((Application.levelCount >= 1) && (levelToLoad != ""))
{
Application.LoadLevel(levelToLoad);
}
}
}
if (alpha < 0.0)
{
if (splashType == SplashType.FadeOutThenLoadNextLevel)
{
if ((Application.levelCount >= 1) && (levelToLoad != ""))
{
Application.LoadLevel(levelToLoad);
}
}
else
{
Destroy(oldCamGO);
}
}
}
}
function OnLevelWasLoaded(lvlIdx : int)
{
if (loadingNextLevel)
{
Destroy(oldCam);
}
}
function OnDrawGizmos()
{
Gizmos.color = Color(1, 0, 0, 0.5);
Gizmos.DrawCube(transform.position, Vector3(1, 1, 1));
}
function StartSplash()
{
status = FadeStatus.FadeIn;
}