HKI Core
UIScreenManager.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections.Generic;
3 
4 namespace HKI.Core.UI
5 {
9  [RequireComponent(typeof(Canvas))]
10  [DisallowMultipleComponent]
11  public class UIScreenManager : MonoBehaviour
12  {
13  // Public variable
14  public static UIScreenManager Instance { get; private set; }
15  [SerializeField] UIScreen StartScreen = null;
16 
17  // Private variables
18  Canvas rootCanvas = null;
19  Transform rootCanvasTransform = null;
20 
21  Dictionary<int, GameObject> istanciatedScreens = new Dictionary<int, GameObject>();
22 
23  List<UIScreen> screens = new List<UIScreen>();
24  List<GameObject> screenGOs = new List<GameObject>();
25 
26  // Awake function
27  void Awake()
28  {
29  if(Instance == null)
30  Instance = this;
31  else
32  Destroy(gameObject);
33 
34  rootCanvas = GetComponent<Canvas>();
35  rootCanvasTransform = transform;
36  }
37 
38  // Start function
39  void Start()
40  {
41  if(StartScreen != null)
42  StartScreen.Open();
43  }
44 
45  // OnDestroy
46  void OnDestroy()
47  {
48  if(Instance == this)
49  Instance = null;
50  }
51 
52  // Interface function
53  public void OpenScreen(UIScreen screen)
54  {
55  if(screen == null)
56  {
57  Debug.LogError("(UIScreenManager)" + name + " you are trying to open a screen that is null!");
58  return;
59  }
60 
61  if(screen.GetScreenPrefab == null)
62  {
63  Debug.LogError("(UIScreenManager)" + name + " you are trying to open a (UIScreen)" + screen.name + " but the screen has no prefab attached to it!");
64  return;
65  }
66 
67  GameObject screenGO = istanciatedScreens.ContainsKey(screen.GetScreenId) ? istanciatedScreens[screen.GetScreenId] : null;
68 
69  if (screenGO == null)
70  {
71  screenGO = GameObject.Instantiate<GameObject>(screen.GetScreenPrefab, rootCanvasTransform);
72  istanciatedScreens.Add(screen.GetScreenId, screenGO);
73  }
74  else
75  screenGO.SetActive(true);
76 
77  if(screens.Count > 0)
78  {
80  {
81  for(int i = screens.Count - 1; i >= 0; i--)
82  {
83  screenGOs[i].SetActive(false);
84 
85  if(screens[i].GetDisableScreensUnderneath)
86  break;
87  }
88  }
89 
90  Canvas topCanvas = screenGO.GetComponent<Canvas>();
91  Canvas previousCanvas = screenGOs[screenGOs.Count - 1].GetComponent<Canvas>();
92  topCanvas.sortingOrder = previousCanvas.sortingOrder + 1;
93  }
94  else
95  {
96  Canvas topCanvas = screenGO.GetComponent<Canvas>();
97  topCanvas.sortingOrder = rootCanvas.sortingOrder + 1;
98  }
99 
100  screens.Add(screen);
101  screenGOs.Add(screenGO);
102  }
103 
104  public void CloseScreen(UIScreen screen)
105  {
106  if(screen == null)
107  {
108  Debug.LogError("(UIScreenManager)" + name + " you are trying to close a screen that is null!");
109  return;
110  }
111 
112  if(screens.Count == 0)
113  {
114  Debug.LogError("(UIScreenManager)" + name + " you are trying to open a (UIScreen)" + screen.name + " but the screens stack is empty!");
115  return;
116  }
117 
118  if(screens[screens.Count - 1] != screen)
119  {
120  Debug.LogError("(UIScreenManager)" + name + " you are trying to open a (UIScreen)" + screen.name + " but it is not on top of screens stack!");
121  return;
122  }
123 
124  CloseTopMenu();
125  }
126 
127  public void CloseTopMenu()
128  {
129  int lastIndex = screens.Count - 1;
130  UIScreen screen = screens[lastIndex];
131  screens.RemoveAt(lastIndex);
132 
133  GameObject screenGO = screenGOs[lastIndex];
134  screenGOs.RemoveAt(lastIndex);
135 
136  if (screen.GetDestroyWhenClosed)
137  Destroy(screenGO);
138  else
139  screenGO.SetActive(false);
140 
141  for (int i = screens.Count - 1; i >= 0; i--)
142  {
143  screenGOs[i].SetActive(true);
144 
145  if(screens[i].GetDisableScreensUnderneath)
146  break;
147  }
148  }
149  }
150 }
void OpenScreen(UIScreen screen)
The UIScreenManager is a simple window manager.
void CloseScreen(UIScreen screen)
This ScriptableObject holds all the information that is required by the UIScreenManager to display a ...
Definition: UIScreen.cs:9
GameObject GetScreenPrefab
Definition: UIScreen.cs:21
bool GetDisableScreensUnderneath
Definition: UIScreen.cs:23
bool GetDestroyWhenClosed
Definition: UIScreen.cs:22