Advertisement
ChrisTutorials

Menu Manager - yaSingleton Example - Unity 2018 Tutorial

Jul 2nd, 2018
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using yaSingleton;
  5.  
  6. [CreateAssetMenu(fileName="MenuManager", menuName="Systems/MenuManager")]
  7. public class MenuManager : Singleton<MenuManager> {
  8.  
  9.     // public GameObject pauseMenuPrefab;
  10.     public GameObject pauseMenuPrefab;
  11.     public GameObject saveMenuPrefab;
  12.     public GameObject canvasPrefab;
  13.     private Canvas _canvas;
  14.     private List<GameMenu> activeMenus;
  15.  
  16.     protected override void Initialize() {
  17.         base.Initialize();
  18.         activeMenus = new List<GameMenu>();
  19.     }
  20.  
  21.     public Canvas SceneCanvas {
  22.         get {
  23.             // Check if canvas variable is set
  24.             if(_canvas == null) {
  25.                 // If not set, look in scene for canvas
  26.                 _canvas = FindObjectOfType<Canvas>();
  27.  
  28.                 if(_canvas == null) {
  29.                     // If no canvas in scene, create one
  30.                     _canvas = Instantiate(canvasPrefab).GetComponent<Canvas>();
  31.                 }
  32.             }
  33.  
  34.             // Return canvas
  35.             return _canvas;
  36.         }
  37.     }
  38.  
  39.     public void CreateMenu(GameMenu menu) {
  40.         // Creates a new game menu
  41.         GameObject newMenu = Instantiate(menu.gameObject, SceneCanvas.transform);
  42.         GameMenu menuComponent = newMenu.GetComponent<GameMenu>();
  43.         System.Type type = menuComponent.GetType();
  44.  
  45.         if(type == typeof(PauseMenu)) {
  46.             AddPauseMenuFunctionality((PauseMenu) menuComponent);
  47.         } else if(type == typeof(SaveMenu)) {
  48.             AddSaveMenuFunctionality((SaveMenu) menuComponent);
  49.         }
  50.  
  51.         // Assign functionality to the buttons
  52.         menuComponent.destroyMenuButton.onClick.AddListener(
  53.             delegate { DestroyMenu(menuComponent); }
  54.         );
  55.        
  56.         activeMenus.Add(menu);
  57.     }
  58.  
  59.     public void AddPauseMenuFunctionality(PauseMenu pauseMenu) {
  60.         pauseMenu.openSaveMenuButton.onClick.AddListener(
  61.                 delegate { ValidateAndCreateMenuPrefab(saveMenuPrefab); }
  62.             );
  63.         pauseMenu.openSaveMenuButton.onClick.AddListener(
  64.             delegate { DestroyMenu(pauseMenu); }
  65.         );
  66.     }
  67.  
  68.     public void AddSaveMenuFunctionality(SaveMenu saveMenu) {
  69.  
  70.     }
  71.  
  72.     public void DestroyMenu(GameMenu menu) {
  73.         if(menu != null) {
  74.             activeMenus.Remove(menu);
  75.             Destroy(menu.gameObject);
  76.         }
  77.     }
  78.  
  79.     /// <summary>
  80.     /// Checking if the prefab has a GameMenu script.
  81.     /// If so, then instantiate the prefab.
  82.     /// </summary>
  83.     /// <param name="prefab"></param>
  84.     private void ValidateAndCreateMenuPrefab(GameObject prefab) {
  85.         // Escape is down
  86.         GameMenu menu = prefab.GetComponent<GameMenu>();
  87.  
  88.         if(menu != null) {
  89.             CreateMenu(menu);
  90.         } else {
  91.             Debug.LogError("Pause Menu Prefab has no GameMenu script attached to it.");
  92.         }
  93.     }
  94.  
  95.     public override void OnUpdate() {
  96.         if(Input.GetKeyDown(KeyCode.Escape)) {
  97.             bool exists = false;
  98.  
  99.             // Checking to see if one exists
  100.             foreach(GameMenu menu in activeMenus) {
  101.                 if(menu.GetType() == typeof(PauseMenu)) {
  102.                     exists = true;
  103.                     break;
  104.                 }
  105.             }
  106.  
  107.             if(!exists) {
  108.                 // Escape is down
  109.                 ValidateAndCreateMenuPrefab(pauseMenuPrefab);
  110.             }
  111.         }
  112.     }
  113.    
  114.    
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement