Tanoda
MenuManager.cs
Go to the documentation of this file.
1
5
6using System.Collections.Generic;
7using System.Reflection;
8
10{
11 [AddComponentMenu("UI/Extensions/Menu Manager")]
12 [DisallowMultipleComponent]
13 public class MenuManager : MonoBehaviour
14 {
15 [SerializeField]
16 private Menu[] menuScreens;
17
19 {
20 get { return menuScreens; }
21 set { menuScreens = value; }
22 }
23
24 [SerializeField]
25 private int startScreen = 0;
26
27 public int StartScreen
28 {
29 get { return startScreen; }
30 set { startScreen = value; }
31 }
32
33 private Stack<Menu> menuStack = new Stack<Menu>();
34
35 public static MenuManager Instance { get; set; }
36
37 private void Start()
38 {
39 Instance = this;
40 if (MenuScreens.Length > 0 + StartScreen)
41 {
42 var startMenu = CreateInstance(MenuScreens[StartScreen].name);
43 OpenMenu(startMenu.GetMenu());
44 }
45 else
46 {
47 Debug.LogError("Not enough Menu Screens configured");
48 }
49 }
50
51 private void OnDestroy()
52 {
53 Instance = null;
54 }
55
56 public GameObject CreateInstance(string MenuName)
57 {
58 var prefab = GetPrefab(MenuName);
59
60 return Instantiate(prefab, transform);
61 }
62
63 public void CreateInstance(string MenuName, out GameObject menuInstance)
64 {
65 var prefab = GetPrefab(MenuName);
66
67 menuInstance = Instantiate(prefab, transform);
68 }
69
70 public void OpenMenu(Menu menuInstance)
71 {
72 // De-activate top menu
73 if (menuStack.Count > 0)
74 {
75 if (menuInstance.DisableMenusUnderneath)
76 {
77 foreach (var menu in menuStack)
78 {
79 menu.gameObject.SetActive(false);
80
81 if (menu.DisableMenusUnderneath)
82 break;
83 }
84 }
85
86 var topCanvas = menuInstance.GetComponent<Canvas>();
87 var previousCanvas = menuStack.Peek().GetComponent<Canvas>();
88 topCanvas.sortingOrder = previousCanvas.sortingOrder + 1;
89 }
90
91 menuStack.Push(menuInstance);
92 }
93
94 private GameObject GetPrefab(string PrefabName)
95 {
96 for (int i = 0; i < MenuScreens.Length; i++)
97 {
98 if (MenuScreens[i].name == PrefabName)
99 {
100 return MenuScreens[i].gameObject;
101 }
102 }
103 throw new MissingReferenceException("Prefab not found for " + PrefabName);
104 }
105
106 public void CloseMenu(Menu menu)
107 {
108 if (menuStack.Count == 0)
109 {
110 Debug.LogErrorFormat(menu, "{0} cannot be closed because menu stack is empty", menu.GetType());
111 return;
112 }
113
114 if (menuStack.Peek() != menu)
115 {
116 Debug.LogErrorFormat(menu, "{0} cannot be closed because it is not on top of stack", menu.GetType());
117 return;
118 }
119
120 CloseTopMenu();
121 }
122
123 public void CloseTopMenu()
124 {
125 var menuInstance = menuStack.Pop();
126
127 if (menuInstance.DestroyWhenClosed)
128 Destroy(menuInstance.gameObject);
129 else
130 menuInstance.gameObject.SetActive(false);
131
132 // Re-activate top menu
133 // If a re-activated menu is an overlay we need to activate the menu under it
134 foreach (var menu in menuStack)
135 {
136 menu.gameObject.SetActive(true);
137
138 if (menu.DisableMenusUnderneath)
139 break;
140 }
141 }
142
143 private void Update()
144 {
145 // On Android the back button is sent as Esc
146 if (Input.GetKeyDown(KeyCode.Escape) && menuStack.Count > 0)
147 {
148 menuStack.Peek().OnBackPressed();
149 }
150 }
151 }
152
153 public static class MenuExtensions
154 {
155 public static Menu GetMenu(this GameObject go)
156 {
157 return go.GetComponent<Menu>();
158 }
159 }
160
161}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
GameObject CreateInstance(string MenuName)
Definition: MenuManager.cs:56
void OpenMenu(Menu menuInstance)
Definition: MenuManager.cs:70
void CreateInstance(string MenuName, out GameObject menuInstance)
Definition: MenuManager.cs:63
Credit Erdener Gonenc - @PixelEnvision.