shadowx320

SplashScreen.cs

Aug 23rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. // <summary>
  7. // Splash screen.cs
  8. // Bj Thomas
  9. // 8/6/2018
  10. // <summary>
  11.  
  12. [RequireComponent(typeof(AudioSource))] //Add audio source when attaching the script
  13.  
  14. public class SplashScreen : MonoBehaviour {
  15.  
  16. public Texture2D _splashScreenBackground; //Creates slot in inspector to assign splash screen background image
  17. public Texture2D _splashScreenText; //Creates slot in inpector to assign splash screen text
  18.  
  19. private AudioSource _splashScreenAudio; //Defines naming convention for audio source component
  20. public AudioClip _splashScreenMusic; //Creates slot in inspector to assign splash screen music
  21.  
  22. private float _splashScreenFadeValue; //Defines fade value
  23. private float _splashScreenFadeSpeed = 0.15f; //Defines fade speed
  24.  
  25. private SplashScreenController _splashScreenController; //Defines naming convention for splash screen controller
  26.  
  27. private enum SplashScreenController { //Defines states for splash screen
  28. SplashScreenFadeIn = 0,
  29. SplashScreenFadeOut = 1
  30. }
  31.  
  32. private void Awake(){
  33. _splashScreenFadeValue = 0; //Fade value equals zero on start up
  34. }
  35.  
  36. // Use this for initialization
  37. void Start() {
  38. Cursor.visible = false; //Set the cursor visable state to false
  39. Cursor.lockState = CursorLockMode.Locked; //and lock the cursor
  40.  
  41. _splashScreenAudio = GetComponent<AudioSource>(); //Splash screen audio equals the audio source
  42.  
  43. _splashScreenAudio.volume = 0; //Audio volume equals zero on startup
  44. _splashScreenAudio.clip = _splashScreenMusic; //Audio clip equals splash screen music
  45. _splashScreenAudio.loop = true; //Set audio to loop
  46. _splashScreenAudio.Play(); //Play audio
  47.  
  48. _splashScreenController = //State equals
  49. SplashScreen.SplashScreenController.SplashScreenFadeIn; //fade in on start up
  50.  
  51. StartCoroutine("SplashScreenManager"); //Start SplashScreenManager function
  52. }
  53.  
  54. // Update is called once per frame
  55. void Update() {
  56.  
  57. }
  58.  
  59. private IEnumerator SplashScreenManager(){
  60. while (true) {
  61. switch(_splashScreenController)
  62. {
  63. case SplashScreenController.SplashScreenFadeIn:
  64. SplashScreenFadeIn();
  65. break;
  66. case SplashScreenController.SplashScreenFadeOut:
  67. SplashScreenFadeOut();
  68. break;
  69. }
  70. yield return null;
  71. }
  72. }
  73.  
  74. private void SplashScreenFadeIn(){
  75. Debug.Log("SplashScreenFadeIn");
  76.  
  77. _splashScreenAudio.volume += _splashScreenFadeSpeed * Time.deltaTime; //Increase volume by fade speed
  78. _splashScreenFadeValue += _splashScreenFadeSpeed * Time.deltaTime; //Increase volume by fade value
  79.  
  80. if (_splashScreenFadeValue > 1) //if fade value is greater than one
  81. _splashScreenFadeValue = 1; //Then set fade value to one
  82.  
  83. if (_splashScreenFadeValue == 1) //if fade value equals one
  84. _splashScreenController = //set splash screen controller to equal
  85. SplashScreen.SplashScreenController.SplashScreenFadeOut; //Splash screen fade out
  86. }
  87.  
  88. private void SplashScreenFadeOut(){
  89. Debug.Log("SplashScreenFadeOut");
  90.  
  91. _splashScreenAudio.volume -= _splashScreenFadeSpeed * Time.deltaTime; //Decrease volume by fade speed
  92. _splashScreenFadeValue -= _splashScreenFadeSpeed * Time.deltaTime; //Decrease volume by fade value
  93.  
  94. if (_splashScreenFadeValue < 0) //if fade value is less than zero
  95. _splashScreenFadeValue = 0; //Then set fade value to zero
  96.  
  97. if (_splashScreenFadeValue == 0) //if fade value equals zero
  98. SceneManager.LoadScene("ControllerWarning"); //load scene ControllerWarning
  99. }
  100.  
  101. private void OnGUI(){
  102. GUI.DrawTexture(new Rect(0, 0 //Draw texture starting at 0/0
  103. , Screen.width, Screen.height), //by the screen width and height
  104. _splashScreenBackground); //and draw the background texture
  105.  
  106. GUI.color = new Color(1, 1, 1, _splashScreenFadeValue); //GUI color is equal to (1, 1, 1) plus the fade value
  107.  
  108. GUI.DrawTexture(new Rect(0, 0 //Draw texture starting at 0/0
  109. , Screen.width, Screen.height), //by the screen width and height
  110. _splashScreenBackground); //and draw the background texture
  111. }
  112. }
Add Comment
Please, Sign In to add comment