using UnityEngine; using System.Collections; public class LoadingScreen : MonoBehaviour { Texture2D emptyProgressBar, fullProgressBar; float emptyLoadingBarWidth, emptyLoadingBarHeight; static string levelName; float fullLoadingBarWidth,fullLoadingBarHeight, fullLoadingBarMarginMultiplier; float marginFromBottom, fullToEmptyMultiplier; AsyncOperation async; public static void StartLoading(string nextlevelName){ Application.LoadLevel ("LoadingScreen"); levelName = nextlevelName; } void Start(){ //set default parameter variables fullProgressBar = Texture2D.whiteTexture; emptyProgressBar = Texture2D.blackTexture; emptyLoadingBarWidth = 300; emptyLoadingBarHeight = 40; marginFromBottom = 10; fullToEmptyMultiplier = .7f; //setup Full Loading Bar using the Empty Loading Bar fullLoadingBarWidth = emptyLoadingBarWidth * fullToEmptyMultiplier; fullLoadingBarHeight = emptyLoadingBarHeight * fullToEmptyMultiplier; fullLoadingBarMarginMultiplier = (1-fullToEmptyMultiplier)/2; //set up aSyncTask StartCoroutine ("load"); } IEnumerator load() { Debug.LogWarning("ASYNC LOAD STARTED - " + "DO NOT EXIT PLAY MODE UNTIL SCENE LOADS... UNITY WILL CRASH"); async = Application.LoadLevelAsync(levelName); async.allowSceneActivation = false; yield return async; } void OnGUI() { GUI.DrawTexture(new Rect(centerX(emptyLoadingBarWidth) , bottomY (emptyLoadingBarHeight) , emptyLoadingBarWidth, emptyLoadingBarHeight), emptyProgressBar); if (async != null) { //Debug.Log("Loading Progress: "+async.progress); if(async.progress <.9f){//Due to some stupid Unity thing, .9 = completed drawLoadingBar(async.progress); }else{ float continueBtnWidth = 75; float continueBtnHeight = 25; if(GUI.Button(new Rect(centerX(continueBtnWidth) , bottomY (emptyLoadingBarHeight) - emptyLoadingBarHeight, continueBtnWidth, continueBtnHeight), "Continue")){ Debug.Log("Leaving Loading Screen"); async.allowSceneActivation = true; } drawLoadingBar(1); } } } void drawLoadingBar(float currentProgress){ //not displaying for some reason GUI.DrawTexture(new Rect(centerX(emptyLoadingBarWidth), bottomY (emptyLoadingBarHeight), emptyLoadingBarWidth * currentProgress, emptyLoadingBarHeight), emptyProgressBar); GUI.DrawTexture(new Rect(centerX(fullLoadingBarWidth), bottomY (fullLoadingBarHeight) - fullLoadingBarMarginMultiplier * emptyLoadingBarHeight, fullLoadingBarWidth * currentProgress, fullLoadingBarHeight), fullProgressBar); } float centerX(float item_width){ return Screen.width / 2 - item_width / 2; } float bottomY(float item_height){ return Screen.height - item_height - marginFromBottom; } }