Advertisement
Guest User

Untitled

a guest
Jul 18th, 2021
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using UnityEngine.EventSystems;
  7. using UnityEditor;
  8. using Cinemachine;
  9. using System.IO;
  10. //using UnityStandardAssets.Characters.ThirdPerson;
  11.  
  12.  
  13. public class MenuController : MonoBehaviour
  14. {
  15. #region Default Values
  16. [Header("Default Menu Values")]
  17. [SerializeField] private float defaultVolume;
  18. [SerializeField] private int defaultSen;
  19. [SerializeField] private bool defaultInvertY;
  20.  
  21. [Header("Levels To Load")]
  22. public string _newGameButtonLevel;
  23. private string levelToLoad;
  24.  
  25. public SceneFader sceneFader;
  26. public GameObject player;
  27.  
  28. public GameObject mainmenuFreshNewGame;
  29. public GameObject mainmenuContinueGame;
  30. private string SAVE_FOLDER;
  31. public GameObject mainMenuCanvases;
  32.  
  33. private int menuNumber;
  34. #endregion
  35.  
  36. #region Menu Dialogs
  37. [Header("Main Menu Components")]
  38. [SerializeField] private GameObject menuDefaultCanvas;
  39. [SerializeField] private GameObject GeneralSettingsCanvas;
  40. [SerializeField] private GameObject graphicsMenu;
  41. [SerializeField] private GameObject soundMenu;
  42. [SerializeField] private GameObject controlsMenu;
  43. [SerializeField] private GameObject confirmationMenu;
  44. [Space(10)]
  45. [Header("Menu Popout Dialogs")]
  46. [SerializeField] private GameObject noSaveDialog;
  47. [SerializeField] private GameObject newGameDialog;
  48. [SerializeField] private GameObject loadGameDialog;
  49. #endregion
  50.  
  51. #region Slider Linking
  52. [Header("Menu Sliders")]
  53. [SerializeField] private Text controllerSenText;
  54. [SerializeField] private Slider controllerSenSlider;
  55. public float controlSenFloat = 2f;
  56. [Space(10)]
  57. [SerializeField] private Text volumeText;
  58. [SerializeField] private Slider volumeSlider;
  59. [Space(10)]
  60. [SerializeField] private Toggle invertYToggle;
  61. #endregion
  62.  
  63. public static bool LoadSceneForSavedGame = false;
  64.  
  65. private void Awake()
  66. {
  67. SAVE_FOLDER = Application.dataPath + "/save_";
  68. string fileName = Path.Combine(SAVE_FOLDER, "savegame.txt");
  69. if(File.Exists(fileName))
  70. {
  71. mainmenuFreshNewGame.SetActive(false);
  72. mainmenuContinueGame.SetActive(true);
  73. }
  74. }
  75.  
  76. #region Initialisation - Button Selection & Menu Order
  77. private void Start()
  78. {
  79. menuNumber = 1;
  80. }
  81. #endregion
  82.  
  83. //MAIN SECTION
  84. public IEnumerator ConfirmationBox()
  85. {
  86. confirmationMenu.SetActive(true);
  87. yield return new WaitForSeconds(2);
  88. confirmationMenu.SetActive(false);
  89. }
  90.  
  91. private void Update()
  92. {
  93. if (Input.GetKeyDown(KeyCode.Escape))
  94. {
  95. if (menuNumber == 2 || menuNumber == 8)
  96. {
  97. GoBackToMainMenu();
  98. ClickSound();
  99. }
  100.  
  101. else if (menuNumber == 3 || menuNumber == 4 || menuNumber == 5)
  102. {
  103. GoBackToOptionsMenu();
  104. ClickSound();
  105. }
  106.  
  107. else if (menuNumber == 6) //CONTROLS MENU
  108. {
  109. ClickSound();
  110. }
  111. }
  112. }
  113.  
  114. private void ClickSound()
  115. {
  116. GetComponent<AudioSource>().Play();
  117. }
  118.  
  119. #region Menu Mouse Clicks
  120. public void MouseClick(string buttonType)
  121. {
  122. if (buttonType == "Controls")
  123. {
  124. transform.GetComponent<AudioSource>().Play();
  125. controlsMenu.SetActive(true);
  126. menuNumber = 6;
  127. }
  128.  
  129. if (buttonType == "Graphics")
  130. {
  131. transform.GetComponent<AudioSource>().Play();
  132. mainMenuCanvases.SetActive(false);
  133. GeneralSettingsCanvas.SetActive(false);
  134. graphicsMenu.SetActive(true);
  135. menuNumber = 3;
  136. }
  137.  
  138. if (buttonType == "Sound")
  139. {
  140. transform.GetComponent<AudioSource>().Play();
  141. mainMenuCanvases.SetActive(false);
  142. GeneralSettingsCanvas.SetActive(false);
  143. soundMenu.SetActive(true);
  144. menuNumber = 4;
  145. }
  146.  
  147. if (buttonType == "Exit")
  148. {
  149. transform.GetComponent<AudioSource>().Play();
  150. Debug.Log("YES QUIT!");
  151. Application.Quit();
  152. }
  153.  
  154. if (buttonType == "Options")
  155. {
  156. transform.GetComponent<AudioSource>().Play();
  157. menuDefaultCanvas.SetActive(false);
  158. GeneralSettingsCanvas.SetActive(true);
  159. menuNumber = 2;
  160. }
  161.  
  162. if (buttonType == "NewGame")
  163. {
  164. transform.GetComponent<AudioSource>().Play();
  165. mainMenuCanvases.SetActive(false);
  166. menuDefaultCanvas.SetActive(false);
  167. newGameDialog.SetActive(true);
  168. menuNumber = 7;
  169. }
  170. }
  171.  
  172. /*IEnumerator DisplaySavedGamesWindow(float time, bool direction)
  173. {
  174. float currentTime = 0;
  175. while (currentTime < time)
  176. {
  177. var scaler = currentTime / time;
  178. canvasScaler.scaleFactor = scaler;
  179. currentTime += Time.deltaTime;
  180.  
  181.  
  182. yield return null;
  183. }
  184. }*/
  185. #endregion
  186.  
  187. public void VolumeSlider(float volume)
  188. {
  189. AudioListener.volume = volume;
  190. volumeText.text = volume.ToString("0.0");
  191. }
  192.  
  193. public void VolumeApply()
  194. {
  195. PlayerPrefs.SetFloat("masterVolume", AudioListener.volume);
  196. Debug.Log(PlayerPrefs.GetFloat("masterVolume"));
  197. StartCoroutine(ConfirmationBox());
  198. }
  199.  
  200. public void ControllerSen()
  201. {
  202. controllerSenText.text = controllerSenSlider.value.ToString("0");
  203. controlSenFloat = controllerSenSlider.value;
  204. }
  205.  
  206. #region ResetButton
  207. public void ResetButton(string GraphicsMenu)
  208. {
  209. if (GraphicsMenu == "Audio")
  210. {
  211. AudioListener.volume = defaultVolume;
  212. volumeSlider.value = defaultVolume;
  213. volumeText.text = defaultVolume.ToString("0.0");
  214. VolumeApply();
  215. }
  216.  
  217. if (GraphicsMenu == "Graphics")
  218. {
  219. controllerSenText.text = defaultSen.ToString("0");
  220. controllerSenSlider.value = defaultSen;
  221. controlSenFloat = defaultSen;
  222.  
  223. invertYToggle.isOn = false;
  224. }
  225. }
  226. #endregion
  227.  
  228. #region Dialog Options - This is where we load what has been saved in player prefs!
  229. public void ClickNewGameDialog(string ButtonType)
  230. {
  231. if (ButtonType == "Yes")
  232. {
  233. // Here to use a script to load the start game scene slowly smooth fade to black
  234. // Then fade back to transparent when loading/loaded the scene game !!!!!
  235. //SceneManager.LoadScene(_newGameButtonLevel);
  236.  
  237. // When making new game to reset everything state scene everythig.
  238. // Including the uiSceneText for example.
  239. // To check how to reset everything to default !
  240. LoadSceneForSavedGame = false;
  241. newGameDialog.SetActive(false);
  242.  
  243. var scenesCount = SceneManager.sceneCount;
  244.  
  245. List<Scene> scene = new List<Scene>();
  246. for (int i = 0; i < scenesCount; i++)
  247. {
  248. scene.Add(SceneManager.GetSceneByBuildIndex(0));
  249. }
  250.  
  251. if(scene.Count > 1)
  252. {
  253. //SceneManager.UnloadSceneAsync(1);
  254. }
  255.  
  256. Time.timeScale = 1f;
  257.  
  258. StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel));
  259. }
  260.  
  261. if (ButtonType == "No")
  262. {
  263. GoBackToMainMenu();
  264. }
  265. }
  266.  
  267. public void ContinueGameButton()
  268. {
  269. transform.GetComponent<AudioSource>().Play();
  270. LoadSceneForSavedGame = true;
  271. newGameDialog.SetActive(false);
  272. StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel));
  273. }
  274.  
  275. #endregion
  276.  
  277. #region Back to Menus
  278. public void GoBackToOptionsMenu()
  279. {
  280. transform.GetComponent<AudioSource>().Play();
  281. GeneralSettingsCanvas.SetActive(true);
  282. graphicsMenu.SetActive(false);
  283. soundMenu.SetActive(false);
  284.  
  285. VolumeApply();
  286.  
  287. menuNumber = 2;
  288. }
  289.  
  290. public void GoBackToMainMenu()
  291. {
  292. transform.GetComponent<AudioSource>().Play();
  293. mainMenuCanvases.SetActive(true);
  294. menuDefaultCanvas.SetActive(true);
  295. newGameDialog.SetActive(false);
  296. loadGameDialog.SetActive(false);
  297. noSaveDialog.SetActive(false);
  298. GeneralSettingsCanvas.SetActive(false);
  299. graphicsMenu.SetActive(false);
  300. soundMenu.SetActive(false);
  301. menuNumber = 1;
  302. }
  303.  
  304. public void ClickQuitOptions()
  305. {
  306. GoBackToMainMenu();
  307. }
  308.  
  309. public void ClickNoSaveDialog()
  310. {
  311. GoBackToMainMenu();
  312. }
  313. #endregion
  314. }
  315.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement