Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using UnityEditor;
- using Cinemachine;
- using System.IO;
- //using UnityStandardAssets.Characters.ThirdPerson;
- public class MenuController : MonoBehaviour
- {
- #region Default Values
- [Header("Default Menu Values")]
- [SerializeField] private float defaultVolume;
- [SerializeField] private int defaultSen;
- [SerializeField] private bool defaultInvertY;
- [Header("Levels To Load")]
- public string _newGameButtonLevel;
- private string levelToLoad;
- public SceneFader sceneFader;
- public GameObject player;
- public GameObject mainmenuFreshNewGame;
- public GameObject mainmenuContinueGame;
- private string SAVE_FOLDER;
- public GameObject mainMenuCanvases;
- private int menuNumber;
- #endregion
- #region Menu Dialogs
- [Header("Main Menu Components")]
- [SerializeField] private GameObject menuDefaultCanvas;
- [SerializeField] private GameObject GeneralSettingsCanvas;
- [SerializeField] private GameObject graphicsMenu;
- [SerializeField] private GameObject soundMenu;
- [SerializeField] private GameObject controlsMenu;
- [SerializeField] private GameObject confirmationMenu;
- [Space(10)]
- [Header("Menu Popout Dialogs")]
- [SerializeField] private GameObject noSaveDialog;
- [SerializeField] private GameObject newGameDialog;
- [SerializeField] private GameObject loadGameDialog;
- #endregion
- #region Slider Linking
- [Header("Menu Sliders")]
- [SerializeField] private Text controllerSenText;
- [SerializeField] private Slider controllerSenSlider;
- public float controlSenFloat = 2f;
- [Space(10)]
- [SerializeField] private Text volumeText;
- [SerializeField] private Slider volumeSlider;
- [Space(10)]
- [SerializeField] private Toggle invertYToggle;
- #endregion
- public static bool LoadSceneForSavedGame = false;
- private void Awake()
- {
- SAVE_FOLDER = Application.dataPath + "/save_";
- string fileName = Path.Combine(SAVE_FOLDER, "savegame.txt");
- if(File.Exists(fileName))
- {
- mainmenuFreshNewGame.SetActive(false);
- mainmenuContinueGame.SetActive(true);
- }
- }
- #region Initialisation - Button Selection & Menu Order
- private void Start()
- {
- menuNumber = 1;
- }
- #endregion
- //MAIN SECTION
- public IEnumerator ConfirmationBox()
- {
- confirmationMenu.SetActive(true);
- yield return new WaitForSeconds(2);
- confirmationMenu.SetActive(false);
- }
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- if (menuNumber == 2 || menuNumber == 8)
- {
- GoBackToMainMenu();
- ClickSound();
- }
- else if (menuNumber == 3 || menuNumber == 4 || menuNumber == 5)
- {
- GoBackToOptionsMenu();
- ClickSound();
- }
- else if (menuNumber == 6) //CONTROLS MENU
- {
- ClickSound();
- }
- }
- }
- private void ClickSound()
- {
- GetComponent<AudioSource>().Play();
- }
- #region Menu Mouse Clicks
- public void MouseClick(string buttonType)
- {
- if (buttonType == "Controls")
- {
- transform.GetComponent<AudioSource>().Play();
- controlsMenu.SetActive(true);
- menuNumber = 6;
- }
- if (buttonType == "Graphics")
- {
- transform.GetComponent<AudioSource>().Play();
- mainMenuCanvases.SetActive(false);
- GeneralSettingsCanvas.SetActive(false);
- graphicsMenu.SetActive(true);
- menuNumber = 3;
- }
- if (buttonType == "Sound")
- {
- transform.GetComponent<AudioSource>().Play();
- mainMenuCanvases.SetActive(false);
- GeneralSettingsCanvas.SetActive(false);
- soundMenu.SetActive(true);
- menuNumber = 4;
- }
- if (buttonType == "Exit")
- {
- transform.GetComponent<AudioSource>().Play();
- Debug.Log("YES QUIT!");
- Application.Quit();
- }
- if (buttonType == "Options")
- {
- transform.GetComponent<AudioSource>().Play();
- menuDefaultCanvas.SetActive(false);
- GeneralSettingsCanvas.SetActive(true);
- menuNumber = 2;
- }
- if (buttonType == "NewGame")
- {
- transform.GetComponent<AudioSource>().Play();
- mainMenuCanvases.SetActive(false);
- menuDefaultCanvas.SetActive(false);
- newGameDialog.SetActive(true);
- menuNumber = 7;
- }
- }
- /*IEnumerator DisplaySavedGamesWindow(float time, bool direction)
- {
- float currentTime = 0;
- while (currentTime < time)
- {
- var scaler = currentTime / time;
- canvasScaler.scaleFactor = scaler;
- currentTime += Time.deltaTime;
- yield return null;
- }
- }*/
- #endregion
- public void VolumeSlider(float volume)
- {
- AudioListener.volume = volume;
- volumeText.text = volume.ToString("0.0");
- }
- public void VolumeApply()
- {
- PlayerPrefs.SetFloat("masterVolume", AudioListener.volume);
- Debug.Log(PlayerPrefs.GetFloat("masterVolume"));
- StartCoroutine(ConfirmationBox());
- }
- public void ControllerSen()
- {
- controllerSenText.text = controllerSenSlider.value.ToString("0");
- controlSenFloat = controllerSenSlider.value;
- }
- #region ResetButton
- public void ResetButton(string GraphicsMenu)
- {
- if (GraphicsMenu == "Audio")
- {
- AudioListener.volume = defaultVolume;
- volumeSlider.value = defaultVolume;
- volumeText.text = defaultVolume.ToString("0.0");
- VolumeApply();
- }
- if (GraphicsMenu == "Graphics")
- {
- controllerSenText.text = defaultSen.ToString("0");
- controllerSenSlider.value = defaultSen;
- controlSenFloat = defaultSen;
- invertYToggle.isOn = false;
- }
- }
- #endregion
- #region Dialog Options - This is where we load what has been saved in player prefs!
- public void ClickNewGameDialog(string ButtonType)
- {
- if (ButtonType == "Yes")
- {
- // Here to use a script to load the start game scene slowly smooth fade to black
- // Then fade back to transparent when loading/loaded the scene game !!!!!
- //SceneManager.LoadScene(_newGameButtonLevel);
- // When making new game to reset everything state scene everythig.
- // Including the uiSceneText for example.
- // To check how to reset everything to default !
- LoadSceneForSavedGame = false;
- newGameDialog.SetActive(false);
- var scenesCount = SceneManager.sceneCount;
- List<Scene> scene = new List<Scene>();
- for (int i = 0; i < scenesCount; i++)
- {
- scene.Add(SceneManager.GetSceneByBuildIndex(0));
- }
- if(scene.Count > 1)
- {
- //SceneManager.UnloadSceneAsync(1);
- }
- Time.timeScale = 1f;
- StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel));
- }
- if (ButtonType == "No")
- {
- GoBackToMainMenu();
- }
- }
- public void ContinueGameButton()
- {
- transform.GetComponent<AudioSource>().Play();
- LoadSceneForSavedGame = true;
- newGameDialog.SetActive(false);
- StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel));
- }
- #endregion
- #region Back to Menus
- public void GoBackToOptionsMenu()
- {
- transform.GetComponent<AudioSource>().Play();
- GeneralSettingsCanvas.SetActive(true);
- graphicsMenu.SetActive(false);
- soundMenu.SetActive(false);
- VolumeApply();
- menuNumber = 2;
- }
- public void GoBackToMainMenu()
- {
- transform.GetComponent<AudioSource>().Play();
- mainMenuCanvases.SetActive(true);
- menuDefaultCanvas.SetActive(true);
- newGameDialog.SetActive(false);
- loadGameDialog.SetActive(false);
- noSaveDialog.SetActive(false);
- GeneralSettingsCanvas.SetActive(false);
- graphicsMenu.SetActive(false);
- soundMenu.SetActive(false);
- menuNumber = 1;
- }
- public void ClickQuitOptions()
- {
- GoBackToMainMenu();
- }
- public void ClickNoSaveDialog()
- {
- GoBackToMainMenu();
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement