all repos — RPG @ 3551efcf4f46d0b1cbb9cdb7fff743c4354f4129

Fully functional 3D turn based role playing game coded in C# and base Unity.

Assets/Scripts/Menu/MenuManager.cs (view raw)

  1using UnityEngine;
  2using UnityEngine.Audio;
  3using UnityEngine.SceneManagement;
  4using UnityEngine.UI;
  5
  6public class MenuManager : MonoBehaviour
  7{
  8    public GameObject loadPanel;
  9    public GameObject creditsPanel;
 10    public GameObject settingsPanel;
 11    public AudioMixer mixer;
 12
 13    private SettingsManager sm;
 14    enum Panel
 15    {
 16        None = -1,
 17        Load = 0,
 18        Credits = 1,
 19        Settings = 2
 20    }
 21    Panel currentPanel = Panel.None;
 22
 23    private void Start() //remember this is shared between gameover and main menu
 24    {
 25        UI.showCursor(true);
 26        //set music menu
 27        mixer.SetFloat("MusicVolume", Mathf.Log10(PlayerPrefs.GetFloat("MusicVolume", 0.75f)) * 20);
 28        mixer.SetFloat("SfxVolume", Mathf.Log10(PlayerPrefs.GetFloat("SfxVolume", 0.75f)) * 20);
 29
 30        GameMaster.Instance.resetGame();
 31        if (settingsPanel)
 32        {
 33            sm = settingsPanel.GetComponent<SettingsManager>();
 34            settingsPanel.SetActive(false); //TODO: start with settings panel open or set mixer volume in another way
 35        }
 36    }
 37
 38    #region Menu buttons
 39    public void NewGame()
 40    {
 41        SceneManager.LoadScene("Tutorial");
 42    }
 43
 44    public void LoadGame()
 45    {
 46        hideAllPanels();
 47        if(currentPanel == Panel.Load)
 48        {
 49            currentPanel = Panel.None;
 50        } else
 51        {
 52            currentPanel = Panel.Load;
 53            Button btn;
 54            //Fill load slots
 55            for (int i = 0; i < loadPanel.transform.childCount; i++)
 56            {
 57                btn = loadPanel.transform.GetChild(i).GetComponent<Button>();
 58                UI.setSLButtonText(btn, i, true);
 59            }
 60            loadPanel.SetActive(true);
 61        }
 62    }
 63
 64    public void Credits()
 65    {
 66        hideAllPanels();
 67        if (currentPanel == Panel.Credits)
 68        {
 69            currentPanel = Panel.None;
 70        }
 71        else
 72        {
 73            currentPanel = Panel.Credits;
 74            creditsPanel.SetActive(true);
 75        }
 76    }
 77
 78    public void Settings()
 79    {
 80        hideAllPanels();
 81        if (currentPanel == Panel.Settings)
 82        {
 83            currentPanel = Panel.None;
 84        }
 85        else
 86        {
 87            currentPanel = Panel.Settings;
 88            settingsPanel.SetActive(true);
 89        }
 90    }
 91
 92    public void Quit()
 93    {
 94        Application.Quit();
 95    }
 96    #endregion
 97    
 98    #region GameOver Button
 99    public void BackToMainMenu()
100    {
101        SceneManager.LoadScene("Menu");
102    }
103    #endregion
104
105    private void hideAllPanels()
106    {
107        loadPanel.SetActive(false);
108        creditsPanel.SetActive(false);
109        settingsPanel.SetActive(false);
110    }
111}