Advertisement
EmmyDev

Menu System

Dec 29th, 2022 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using TMPro;
  6.  
  7. public class MenuSystem : MonoBehaviour
  8. {
  9.     [SerializeField] private GameObject _overlayEffects;
  10.     [SerializeField] private TextMeshProUGUI _endText;
  11.     [SerializeField] private GameObject _firstEffect;
  12.     [SerializeField] private TextMeshProUGUI _timerDisplay;
  13.     [SerializeField] private string _mainLevelName;
  14.     [SerializeField] private AudioSource _audioSource;
  15.  
  16.     private bool _timerActive;
  17.     private float _currentTime;
  18.  
  19.     private void Start()
  20.     {
  21.         DontDestroyOnLoad(this.gameObject);
  22.         Invoke("RemoveFirstFade", 0.5f);
  23.         if(PlayerPrefs.GetFloat("BestTime") != 0)
  24.         {
  25.             _timerDisplay.text = "Highscore " + string.Format("{0:00}:{1:00}", PlayerPrefs.GetFloat("BestTime")/60, PlayerPrefs.GetFloat("BestTime")%60);
  26.         }
  27.         else
  28.         {
  29.             _timerDisplay.text = "No Highscore";
  30.         }
  31.          
  32.     }
  33.     public void WinEffect()
  34.     {
  35.         _endText.text = "Finished!";
  36.         _endText.color = Color.white;
  37.         _audioSource.GetComponent<Animator>().SetTrigger("FadeOut");
  38.         _timerActive = false;
  39.         _overlayEffects.SetActive(true);
  40.         if(_currentTime < PlayerPrefs.GetFloat("BestTime") || PlayerPrefs.GetFloat("BestTime") == 0)
  41.         {
  42.             PlayerPrefs.SetFloat("BestTime", _currentTime);
  43.         }
  44.        
  45.         Invoke("LoadMenu", 3f);
  46.  
  47.        
  48.        
  49.     }
  50.     public void LooseEffect()
  51.     {
  52.         _endText.text = "You lost!";
  53.         _endText.color = Color.red;
  54.         _audioSource.GetComponent<Animator>().SetTrigger("FadeOut");
  55.         _timerActive = false;
  56.         _overlayEffects.SetActive(true);
  57.        
  58.        
  59.         Invoke("LoadMenu", 3f);
  60.  
  61.        
  62.        
  63.     }
  64.  
  65.     public void LoadMenu()
  66.     {
  67.         //clears curent manager
  68.         RemoveManager();
  69.         _audioSource.Stop();
  70.         SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
  71.        
  72.        
  73.     }
  74.     public void LoadLevel()
  75.     {
  76.         _audioSource.GetComponent<Animator>().SetTrigger("FadeIn");
  77.         _audioSource.Play();
  78.         _currentTime = 0f;
  79.         _timerActive = true;
  80.         SceneManager.LoadScene(_mainLevelName, LoadSceneMode.Single);
  81.     }
  82.     void RemoveManager()
  83.     {
  84.         Destroy(this.gameObject);
  85.     }
  86.     void RemoveFirstFade()
  87.     {
  88.         Destroy(_firstEffect);
  89.     }
  90.     void Update()
  91.     {
  92.         Scene scene = SceneManager.GetActiveScene();
  93.         if(_timerActive && scene.name == _mainLevelName)
  94.         {
  95.             float minutes = Mathf.FloorToInt(_currentTime / 60);
  96.             float seconds = Mathf.FloorToInt(_currentTime % 60);
  97.             _timerDisplay.text = string.Format("{0:00}:{1:00}", minutes, seconds); //(minutes).ToString() + ":" (minutes).ToString();
  98.             _currentTime += Time.deltaTime;
  99.            
  100.         }
  101.        
  102.        
  103.         //clear hi-scores
  104.         if(Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.R))
  105.         {
  106.             PlayerPrefs.DeleteAll();
  107.             _timerDisplay.text = "No Highscore";
  108.         }
  109.        
  110.     }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement