-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathMenuManager.cs
More file actions
142 lines (115 loc) · 3.63 KB
/
MenuManager.cs
File metadata and controls
142 lines (115 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
public class MenuManager : MonoBehaviour
{
public MainMenu MainMenuPrefab;
public GameMenu GameMenuPrefab;
public PauseMenu PauseMenuPrefab;
public OptionsMenu OptionsMenuPrefab;
public AwesomeMenu AwesomeMenuPrefab;
private Stack<Menu> menuStack = new Stack<Menu>();
public static MenuManager Instance { get; set; }
private void Awake()
{
Instance = this;
MainMenu.Show();
}
private void OnDestroy()
{
Instance = null;
}
public void CreateInstance<T>() where T : Menu
{
var prefab = GetPrefab<T>();
Instantiate(prefab, transform);
}
public void OpenMenu(Menu instance, float animInTime)
{
if (menuStack.Count > 0)
{
var topCanvas = instance.GetComponent<Canvas>();
var previousCanvas = menuStack.Peek().GetComponent<Canvas>();
topCanvas.sortingOrder = previousCanvas.sortingOrder + 1;
}
menuStack.Push(instance);
StartCoroutine(DeactivateMenusUnder(instance, animInTime));
}
private IEnumerator DeactivateMenusUnder(Menu instance, float waitTime)
{
yield return new WaitForSeconds(waitTime);
//De-activate previous top menu
if (instance.DisableMenusUnderneath)
{
foreach (var menu in menuStack)
{
if (menu == instance)
continue;
menu.gameObject.SetActive(false);
if (menu.DisableMenusUnderneath)
break;
}
}
}
private T GetPrefab<T>() where T : Menu
{
// Get prefab dynamically, based on public fields set from Unity
// You can use private fields with SerializeField attribute too
var fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach (var field in fields)
{
var prefab = field.GetValue(this) as T;
if (prefab != null)
{
return prefab;
}
}
throw new MissingReferenceException("Prefab not found for type " + typeof(T));
}
public void CloseMenu(Menu topMenu, float animOutTime)
{
if (menuStack.Count == 0)
{
Debug.LogErrorFormat(topMenu, "{0} cannot be closed because menu stack is empty", topMenu.GetType());
return;
}
if (menuStack.Peek() != topMenu)
{
Debug.LogErrorFormat(topMenu, "{0} cannot be closed because it is not on top of stack", topMenu.GetType());
return;
}
menuStack.Pop();
StartCoroutine(CloseTopMenu(topMenu, animOutTime));
ReactivateOldMenu();
}
private IEnumerator CloseTopMenu(Menu instance, float waitTime)
{
yield return new WaitForSeconds(waitTime);
if (instance == null)
yield break;
if (instance.DestroyWhenClosed)
Destroy(instance.gameObject);
else
instance.gameObject.SetActive(false);
}
private void ReactivateOldMenu()
{
// If a re-activated menu is an overlay we need to activate the menu under it
foreach (var menu in menuStack)
{
menu.gameObject.SetActive(true);
if (menu.DisableMenusUnderneath)
break;
}
}
private void Update()
{
// On Android the back button is sent as Esc
if (Input.GetKeyDown(KeyCode.Escape) && menuStack.Count > 0)
{
menuStack.Peek().OnBackPressed();
}
}
}