-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fade.cs
52 lines (39 loc) · 2.1 KB
/
Fade.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
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class Fade : MonoBehaviour
{
public Texture2D fadeOutTexture; // the texture that will overlay the screen. This can be a black image or a loading graphic
public float fadeSpeed = 0.8f; // the fading speed
private int drawDepth = -1000; // the texture's order in the draw hierarchy: a low number means it renders on top
private float alpha = 0.8f; // the texture's alpha value between 0 and 1
private int fadeDir = -1; // the direction to fade: in = -1 or out = 1
void OnGUI()
{
if (GameController.instance.previousScene != "Persistent")
{
//fadeSpeed = 0.8f;
// fade out/in the alpha value using a direction, a speed and Time.deltaTime to convert the operation to seconds
alpha += fadeDir * fadeSpeed * Time.deltaTime;
// force (clamp) the number to be between 0 and 1 because GUI.color uses Alpha values between 0 and 1
alpha = Mathf.Clamp01(alpha);
// set color of our GUI (in this case our texture). All color values remain the same & the Alpha is set to the alpha variable
GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);
GUI.depth = drawDepth; // make the black texture render on top (drawn last)
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeOutTexture); // draw the texture to fit the entire screen area
}
}
// sets fadeDir to the direction parameter making the scene fade in if -1 and out if 1
public float BeginFade(int direction)
{
fadeDir = direction;
return (fadeSpeed);
}
// OnLevelWasLoaded is called when a level is loaded. It takes loaded level index (int) as a parameter so you can limit the fade in to certain scenes.
void SceneLoaded()
{
// alpha = 1;
// use this if the alpha is not set to 1 by default
BeginFade(-1); // call the fade in function
}
}