Guest User

Untitled

a guest
Feb 21st, 2015
62
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class SceneManager : MonoBehaviour {
  6.     public static SceneManager Instance;        // Singleton
  7.  
  8.     public string[] levelNames;                 // Scenes to progressively load, in descending array order.
  9.     public int gameLevelNum = 0;
  10.     public GameObject splashObj;                // Put a canvas here (with image child object) for splash screen loading.
  11.     public AnimationCurve Interpolation;        // Interpolates the splash screen with the curve.
  12.     public float Duration = 1f;         // The duration of the animation in seconds.
  13.     private CanvasGroup splashCanvasGroup;      // Used to grab the alpha of the canvas group.
  14.  
  15.     public CanvasGroup blackOverlay;        //Canvas for black overlay
  16.  
  17.     private GameObject canvasObj;           // The canvas game object.
  18.  
  19.     #if UNITY_EDITOR
  20.         bool SkipSplash;
  21.         string FirstScene;
  22.     #endif
  23.  
  24.     void Start () {
  25.         // If there is no instance of this class, set it.
  26.         if (Instance == null)
  27.         {
  28.             DontDestroyOnLoad (gameObject); // Don't destroy this object
  29.             Instance = this;
  30.             if (splashObj != null) {
  31.                 canvasObj = (GameObject)Instantiate (splashObj);
  32.                 canvasObj.transform.SetParent (transform);
  33.                 canvasObj.transform.position = Vector3.zero;
  34.                 splashCanvasGroup = canvasObj.GetComponent<CanvasGroup> ();
  35.                 canvasObj.GetComponent<Canvas> ().sortingOrder = 999;
  36.                 DontDestroyOnLoad (splashObj);
  37.             }
  38.             DontDestroyOnLoad (blackOverlay);
  39.             blackOverlay.GetComponent<Canvas>().sortingOrder = 998;
  40.             Instance.StartCoroutine (LoadNextLevelFadeIn());
  41.         } else {
  42.             Debug.LogError ("There is already a SceneManager in the scene.");
  43.             GameObject.Destroy (this);
  44.         }
  45.     }
  46.  
  47.     public IEnumerator PlayFadeAnimation(float start, float end, CanvasGroup target) {
  48.         // Start a lerp value from zero
  49.         float lerpValue = 0f;
  50.         while (lerpValue <= 1f) {
  51.             //Incerement the value each frame
  52.             lerpValue += Time.deltaTime / Duration;
  53.             //Set the alpha by the interpolated lerp value
  54.             target.alpha = Mathf.Lerp (start, end, Interpolation.Evaluate(lerpValue));
  55.             yield return null;
  56.         }
  57.     }
  58.  
  59.     private IEnumerator PlayFadeAnimation(bool forward, CanvasGroup target) {
  60.         if (forward) {
  61.             yield return Instance.StartCoroutine(PlayFadeAnimation(0f, 1f, target));
  62.         } else {
  63.             yield return Instance.StartCoroutine(PlayFadeAnimation(1f, 0f, target));
  64.         }
  65.     }
  66.  
  67.     public IEnumerator LoadNextLevelFadeIn () {
  68.         yield return StartCoroutine(LoadLevelFadeIn (Application.loadedLevel + 1, true));
  69.     }
  70.  
  71.     public void SetCanvasEnabled(bool enabled) {
  72.         blackOverlay.gameObject.SetActive(enabled);
  73.         if (splashCanvasGroup != null) {
  74.             canvasObj.gameObject.SetActive (enabled);
  75.             splashCanvasGroup.gameObject.SetActive (enabled);
  76.         }
  77.     }
  78.  
  79.     public void LoadLevelFadeInDelegate(int index, bool animate = true) {
  80.         if (animate)
  81.             Instance.StartCoroutine(LoadLevelFadeIn (index));
  82.         else
  83.             Application.LoadLevel(index);
  84.     }
  85.  
  86.     public void LoadLevelFadeInDelegate(string name, bool animate = true) {
  87.         if (animate)
  88.             Instance.StartCoroutine(LoadLevelFadeIn (name));
  89.         else
  90.             Application.LoadLevel(name);
  91.     }
  92.  
  93.     public IEnumerator LoadLevelFadeIn (int index, bool showSplash = false) {
  94.         SetCanvasEnabled (true);
  95.         yield return Instance.StartCoroutine(PlayFadeAnimation(true, blackOverlay));
  96.         if (showSplash && splashCanvasGroup != null)
  97.             yield return Instance.StartCoroutine(PlayFadeAnimation(true, splashCanvasGroup));
  98.         AsyncOperation async = Application.LoadLevelAsync (index);
  99.         yield return async; // Wait for the async operation and animation to complete.
  100.         if (showSplash && splashCanvasGroup != null)
  101.             yield return Instance.StartCoroutine(PlayFadeAnimation(false, splashCanvasGroup)); // remove overlay
  102.         yield return Instance.StartCoroutine(PlayFadeAnimation(false, blackOverlay));
  103.         SetCanvasEnabled (false);
  104.     }
  105.  
  106.     public IEnumerator LoadLevelFadeIn (string sceneName) {
  107.         SetCanvasEnabled (true);
  108.         yield return Instance.StartCoroutine(PlayFadeAnimation(true, blackOverlay));
  109.         AsyncOperation async = Application.LoadLevelAsync (sceneName);
  110.         yield return async; // Wait for the async operation and animation to complete.
  111.         yield return Instance.StartCoroutine(PlayFadeAnimation(false, blackOverlay));
  112.         SetCanvasEnabled (false);
  113.     }
  114.  
  115.     public void ResetGame () {
  116.         // reset the level index counter
  117.         gameLevelNum = 0;
  118.     }
  119.  
  120.     public void QuitGame(){
  121.         Application.Quit ();
  122.     }
  123.  
  124. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×