Last active
March 22, 2016 14:10
-
-
Save moon-goon/d0d66c97df79bead34bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.SceneManagement; | |
public class LevelLoadManager : MonoBehaviour { | |
public GameObject player; | |
public GameObject trigger; | |
public Texture2D fadeOutTexture; // the texture that will overlay the screen. This can be a black image or a loading graphic | |
public float fadeSpeed = 0.2f; // the fading speed | |
public string levelToLoad; | |
private Vector3 currentPos; | |
private Vector3 currentRot; | |
private int drawDepth = -1000; // the texture's order in the draw hierarchy: a low number means it renders on top | |
private float alpha = 1.0f; // the texture's alpha value between 0 and 1 | |
private int fadeDir = -1; // the direction to fade: in = -1 or out = 1 | |
private bool showGUI = false; | |
void Start() { | |
// store player's position at the beginning of game | |
currentPos = new Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z); | |
// store player's rotation value when game starts | |
currentRot = new Vector3(player.transform.eulerAngles.x, player.transform.eulerAngles.y, player.transform.eulerAngles.y); | |
} | |
void OnGUI() { | |
if (showGUI) { | |
fadeOutTexture = (Texture2D)Resources.Load("blackFading"); | |
// 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; | |
// GUI.color uses Alpha values between 0 and 1 | |
// Clamps value between 0 and 1 and returns value. | |
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 | |
// display name of current scene | |
GUI.Label(new Rect(100, 100, 600, 600), "<size=60>" + SceneManager.GetActiveScene().name + "</size>"); | |
} | |
} | |
void LateUpdate() { | |
// when player dies (just for demonstration, if player fall off the ground ) | |
if(player.transform.position.y < -15) { | |
// StartCoroutine: loading scene with AsyncOperation | |
StartCoroutine(DisplayLoading(levelToLoad)); | |
} | |
} | |
public void loadLevel() { | |
StartCoroutine(DisplayLoading(levelToLoad)); | |
} | |
IEnumerator DisplayLoading(string nextLevel) { | |
// assigning Async task | |
AsyncOperation async = SceneManager.LoadSceneAsync(nextLevel); | |
// reset player's position | |
player.transform.position = currentPos; | |
// reset player's rotation value | |
player.transform.rotation = Quaternion.Euler(currentRot); | |
// while async is running | |
while (!async.isDone) { | |
Debug.Log(async.progress); | |
// enable GUI (fading screen) | |
showGUI = true; | |
// disable the game scene whil async | |
async.allowSceneActivation = false; | |
yield return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment