Advertisement
Guest User

GameJamMenuTemplateFix

a guest
Apr 20th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.60 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Audio;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6.  
  7.  
  8. public class StartOptions : MonoBehaviour {
  9.  
  10.  
  11.     public MenuSettings menuSettingsData;
  12.     public int sceneToStart = 1;                                        //Index number in build settings of scene to load if changeScenes is true
  13.     public bool changeScenes;                                           //If true, load a new scene when Start is pressed, if false, fade out UI and continue in single scene
  14.     public bool changeMusicOnStart;                                     //Choose whether to continue playing menu music or start a new music clip
  15.     public CanvasGroup fadeOutImageCanvasGroup;                         //Canvas group used to fade alpha of image which fades in before changing scenes
  16.     public Image fadeImage;                                             //Reference to image used to fade out before changing scenes
  17.  
  18.     [HideInInspector] public bool inMainMenu = true;                    //If true, pause button disabled in main menu (Cancel in input manager, default escape key)
  19.     [HideInInspector] public AnimationClip fadeAlphaAnimationClip;      //Animation clip fading out UI elements alpha
  20.  
  21.  
  22.     private PlayMusic playMusic;                                        //Reference to PlayMusic script
  23.     private float fastFadeIn = .01f;                                    //Very short fade time (10 milliseconds) to start playing music immediately without a click/glitch
  24.     private ShowPanels showPanels;                                      //Reference to ShowPanels script on UI GameObject, to show and hide panels
  25.     private CanvasGroup menuCanvasGroup;
  26.  
  27.  
  28.     void Awake()
  29.     {
  30.         //Get a reference to ShowPanels attached to UI object
  31.         showPanels = GetComponent<ShowPanels> ();
  32.  
  33.         //Get a reference to PlayMusic attached to UI object
  34.         playMusic = GetComponent<PlayMusic> ();
  35.  
  36.         //Get a reference to the CanvasGroup attached to the main menu so that we can fade it's alpha
  37.         menuCanvasGroup = GetComponent<CanvasGroup>();
  38.  
  39.         fadeImage.color = menuSettingsData.sceneChangeFadeColor;
  40.     }
  41.  
  42.  
  43.     public void StartButtonClicked()
  44.     {
  45.         //If changeMusicOnStart is true, fade out volume of music group of AudioMixer by calling FadeDown function of PlayMusic
  46.         //To change fade time, change length of animation "FadeToColor"
  47.         if (menuSettingsData.musicLoopToChangeTo != null)
  48.         {
  49.             playMusic.FadeDown(menuSettingsData.menuFadeTime);
  50.         }
  51.  
  52.         //If changeScenes is true, start fading and change scenes halfway through animation when screen is blocked by FadeImage
  53.         if (menuSettingsData.nextSceneIndex != 0)
  54.         {
  55.             //Use invoke to delay calling of LoadDelayed by half the length of fadeColorAnimationClip
  56.             Invoke ("LoadDelayed", menuSettingsData.menuFadeTime);
  57.  
  58.             StartCoroutine(FadeCanvasGroupAlpha(0f, 1f, fadeOutImageCanvasGroup));
  59.  
  60.         }
  61.  
  62.         //If changeScenes is false, call StartGameInScene
  63.         else
  64.         {
  65.             //Call the StartGameInScene function to start game without loading a new scene.
  66.             StartGameInScene();
  67.         }
  68.  
  69.     }
  70.  
  71.     void OnEnable()
  72.     {
  73.         SceneManager.sceneLoaded += SceneWasLoaded;
  74.     }
  75.  
  76.     void OnDisable()
  77.     {
  78.         SceneManager.sceneLoaded -= SceneWasLoaded;
  79.     }
  80.  
  81.     //Once the level has loaded, check if we want to call PlayLevelMusic
  82.     void SceneWasLoaded(Scene scene, LoadSceneMode mode)
  83.     {
  84.         //if changeMusicOnStart is true, call the PlayLevelMusic function of playMusic
  85.         if (menuSettingsData.musicLoopToChangeTo != null)
  86.         {
  87.             playMusic.PlayLevelMusic ();
  88.         }  
  89.     }
  90.  
  91.  
  92.     public void LoadDelayed()
  93.     {
  94.         //Pause button now works if escape is pressed since we are no longer in Main menu.
  95.         inMainMenu = false;
  96.  
  97.         //Hide the main menu UI element
  98.         showPanels.HideMenu ();
  99.  
  100.         //Load the selected scene, by scene index number in build settings
  101.         SceneManager.LoadScene (sceneToStart);
  102.     }
  103.  
  104.     public void HideDelayed()
  105.     {
  106.         //Hide the main menu UI element after fading out menu for start game in scene
  107.         showPanels.HideMenu();
  108.     }
  109.  
  110.     public void StartGameInScene()
  111.     {
  112.         //Pause button now works if escape is pressed since we are no longer in Main menu.
  113.         inMainMenu = false;
  114.  
  115.         //If there is a second music clip in MenuSettings, fade out volume of music group of AudioMixer by calling FadeDown function of PlayMusic
  116.         if (menuSettingsData.musicLoopToChangeTo != null)
  117.         {
  118.             //Wait until game has started, then play new music
  119.             Invoke ("PlayNewMusic", menuSettingsData.menuFadeTime);
  120.         }
  121.        
  122.         StartCoroutine(FadeCanvasGroupAlpha(1f,0f, menuCanvasGroup));
  123.     }
  124.  
  125.     public IEnumerator FadeCanvasGroupAlpha(float startAlpha, float endAlpha, CanvasGroup canvasGroupToFadeAlpha)
  126.     {
  127.  
  128.         float elapsedTime = 0f;
  129.         float totalDuration = menuSettingsData.menuFadeTime;
  130.  
  131.         while (elapsedTime < totalDuration)
  132.         {
  133.             elapsedTime += Time.deltaTime;
  134.             float currentAlpha = Mathf.Lerp(startAlpha, endAlpha, elapsedTime / totalDuration);
  135.             canvasGroupToFadeAlpha.alpha = currentAlpha;
  136.             yield return null;
  137.         }
  138.  
  139.         HideDelayed();
  140.  
  141.         elapsedTime = 0f;
  142.         while (elapsedTime < totalDuration)
  143.         {
  144.             elapsedTime += Time.deltaTime;
  145.             float currentAlpha = Mathf.Lerp(endAlpha, startAlpha, elapsedTime / totalDuration);
  146.             canvasGroupToFadeAlpha.alpha = currentAlpha;
  147.             yield return null;
  148.         }
  149.  
  150.         Debug.Log("Coroutine done. Game started in same scene! Put your game starting stuff here.");
  151.  
  152.     }
  153.    
  154.  
  155.     public void PlayNewMusic()
  156.     {
  157.         //Fade up music nearly instantly without a click
  158.         playMusic.FadeUp (fastFadeIn);
  159.         //Play second music clip from MenuSettings
  160.         playMusic.PlaySelectedMusic (menuSettingsData.musicLoopToChangeTo);
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement