333GameStudio

Fader

Dec 29th, 2015
1,353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2.  
  3. public var fadeSpeed : float = 1.5f;            // Speed that the screen fades to and from black.
  4.  
  5.  
  6. private var sceneStarting : boolean = true;     // Whether or not the scene is still fading in.
  7.  
  8.  
  9. function Awake ()
  10. {
  11.     // Set the texture so that it is the the size of the screen and covers it.
  12.     GetComponent.<GUITexture>().pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
  13. }
  14.  
  15.  
  16. function Update ()
  17. {
  18.     // If the scene is starting...
  19.     if(sceneStarting)
  20.         // ... call the StartScene function.
  21.         StartScene();
  22. }
  23.  
  24.  
  25. function FadeToClear ()
  26. {
  27.     // Lerp the colour of the texture between itself and transparent.
  28.     GetComponent.<GUITexture>().color = Color.Lerp(GetComponent.<GUITexture>().color, Color.clear, fadeSpeed * Time.deltaTime);
  29. }
  30.  
  31.  
  32. function FadeToBlack ()
  33. {
  34.     // Lerp the colour of the texture between itself and black.
  35.     GetComponent.<GUITexture>().color = Color.Lerp(GetComponent.<GUITexture>().color, Color.black, fadeSpeed * Time.deltaTime);
  36. }
  37.  
  38.  
  39. function StartScene ()
  40. {
  41.     // Fade the texture to clear.
  42.     FadeToClear();
  43.    
  44.     // If the texture is almost clear...
  45.     if(GetComponent.<GUITexture>().color.a <= 0.05f)
  46.     {
  47.         // ... set the colour to clear and disable the GUITexture.
  48.         GetComponent.<GUITexture>().color = Color.clear;
  49.         GetComponent.<GUITexture>().enabled = false;
  50.        
  51.         // The scene is no longer starting.
  52.         sceneStarting = false;
  53.     }
  54. }
  55.  
  56.  
  57. public function EndScene ()
  58. {
  59.     // Make sure the texture is enabled.
  60.     GetComponent.<GUITexture>().enabled = true;
  61.    
  62.     // Start fading towards black.
  63.     FadeToBlack();
  64.    
  65.     // If the screen is almost black...
  66.     if(GetComponent.<GUITexture>().color.a >= 0.95f)
  67.         // ... reload the level.
  68.         Application.LoadLevel(1);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment