-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUIManager.cs
168 lines (165 loc) · 5.42 KB
/
UIManager.cs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public enum WindowGroup
{
Uncategorized = 0,
UpperLeft = 1,
UpperCenter = 2,
UpperRight = 3,
MiddleLeft = 4,
MiddleCenter = 5,
LowerLeft = 6,
LowerCenter = 7,
LowerRight = 8,
MiddleCenterHorizontal = 9,
}
public class UIManager : MonoBehaviour
{
public static UIManager Instance;
public WindowUI windowInstance;
List<WindowUI> windows = new List<WindowUI>();
[SerializeField] UIInput uiInput;
public static UIInput UIInput => Instance.uiInput;
public WindowSetting WindowSetting;
[SerializeField] List<Canvas> uiCanvas = new List<Canvas>();
[SerializeField] List<Transform> uiLayout = new List<Transform>();
public bool InMenu { get => inMenu; set => inMenu = value; }
bool inMenu = false;
[SerializeField] bool overlayMode = false;
public bool OverlayMode => overlayMode;
bool defaultUIStarted = false;
public bool DefaultUIStarted => defaultUIStarted;
List<GeneralUISystem> uiSystems;
public void Awake()
{
Instance = this;
}
void Start()
{
uiSystems = new List<GeneralUISystem>(FindObjectsByType<GeneralUISystem>(FindObjectsSortMode.None));
foreach (GeneralUISystem system in uiSystems)
{
if (!system.enabled)
continue;
system.StartUp();
}
SetOverlayMode(overlayMode);
defaultUIStarted = true;
}
public void SetOverlayMode(bool enable)
{
overlayMode = enable;
foreach (Canvas canvas in uiCanvas)
{
canvas.renderMode = enable ? RenderMode.ScreenSpaceOverlay : RenderMode.ScreenSpaceCamera;
if (!overlayMode && canvas.worldCamera == null)
canvas.worldCamera = Camera.main;
}
ClearAllWindowLocation();
}
public Vector2 UIBoundRetriever(Transform windowTransform, Vector3 elementPosition)
{
if (!overlayMode)
{
return RectTransformUtility.WorldToScreenPoint(Camera.main, windowTransform.TransformPoint(elementPosition));
}
else
{
Vector3 result = windowTransform.TransformPoint(elementPosition);
return new Vector2(result.x, result.y);
}
}
public void LanguageUpdate(int foo)
{
WindowRefresh();
ClearAllWindowLocation();
}
public void WindowRefresh()
{
foreach (var window in windows)
{
window.RefreshSize();
window.InvokeUpdate();
}
}
internal void CloseWindow(WindowUI window)
{
window.Close();
}
public void DelistWindow(WindowUI window)
{
windows.Remove(window);
Destroy(window.gameObject);
}
public WindowUI NewWindow(string name, WindowGroup group, WindowSetup setup, GeneralUISystem masterUI)
{
WindowUI window = InstanceWindow(group, name);
window.Initialize(name, setup, masterUI);
windows.Add(window);
return window;
}
public WindowUI NewWindow(string name, Transform group, WindowSetup setup, GeneralUISystem masterUI)
{
WindowUI window = InstanceWindow(group, name);
window.Initialize(name, setup, masterUI);
windows.Add(window);
return window;
}
public Transform InstantiateLayout(WindowGroup group, string name = "")
{
Transform layout = group switch
{
WindowGroup.LowerLeft => uiLayout[0],
WindowGroup.MiddleLeft => uiLayout[1],
WindowGroup.MiddleCenter => uiLayout[2],
WindowGroup.LowerCenter => uiLayout[3],
WindowGroup.UpperLeft => uiLayout[4],
WindowGroup.LowerRight => uiLayout[5],
WindowGroup.UpperCenter => uiLayout[6],
WindowGroup.UpperRight => uiLayout[7],
WindowGroup.MiddleCenterHorizontal => uiLayout[8],
_ => throw new System.NotImplementedException(),
};
GameObject newLayout = Instantiate(layout.gameObject, layout.parent.parent);
if (name != string.Empty)
newLayout.name = name;
return newLayout.transform;
}
WindowUI InstanceWindow(WindowGroup group, string name)
{
Transform layout = InstantiateLayout(group);
WindowUI window = Instantiate(windowInstance, layout.transform);
layout.gameObject.name = name;
window.gameObject.name = name;
// This is to save up performance
// window.gameObject.SetActive(true);
return window;
}
WindowUI InstanceWindow(Transform layout, string name)
{
WindowUI window = Instantiate(windowInstance, layout.transform);
window.gameObject.name = name;
// This is to save up performance
// window.gameObject.SetActive(true);
return window;
}
public void RegisterUISystem(GeneralUISystem uiSystem)
{
if (uiSystems.Contains(uiSystem))
return;
uiSystems.Add(uiSystem);
}
public void ClearAllWindowLocation()
{
foreach (GeneralUISystem ui in uiSystems)
{
if (ui is not GeneralUISystemWithNavigation)
continue;
if (Screen.fullScreenMode == FullScreenMode.ExclusiveFullScreen)
((GeneralUISystemWithNavigation)ui).ClearWindowLocation(1.5f);
else
((GeneralUISystemWithNavigation)ui).ClearWindowLocation(.5f);
}
}
}