1
-
1
+ using System . Collections . Generic ;
2
+ using System . Collections ;
3
+ using System . Threading . Tasks ;
2
4
using UnityEngine ;
3
5
4
6
namespace UnityCore {
@@ -7,7 +9,102 @@ namespace Menu {
7
9
8
10
public class PageController : MonoBehaviour
9
11
{
10
-
12
+ public static PageController instance ;
13
+
14
+ public bool debug ;
15
+ public PageType entryPage ;
16
+ public Page [ ] pages ;
17
+
18
+ private Hashtable m_Pages ;
19
+ private List < Page > m_OnList ;
20
+ private List < Page > m_OffList ;
21
+
22
+ #region Unity Functions
23
+ private void Awake ( ) {
24
+ if ( ! instance ) {
25
+ instance = this ;
26
+ m_Pages = new Hashtable ( ) ;
27
+ m_OnList = new List < Page > ( ) ;
28
+ m_OffList = new List < Page > ( ) ;
29
+ RegisterAllPages ( ) ;
30
+ TurnPageOn ( entryPage ) ;
31
+ }
32
+ }
33
+
34
+ private void Update ( ) {
35
+ if ( Input . GetKeyUp ( KeyCode . F ) ) {
36
+ TurnPageOn ( PageType . Loading ) ;
37
+ }
38
+ if ( Input . GetKeyUp ( KeyCode . G ) ) {
39
+ TurnPageOff ( PageType . Loading ) ;
40
+ }
41
+ }
42
+ #endregion
43
+
44
+ #region Public Functions
45
+ public async void TurnPageOn ( PageType _type ) {
46
+ if ( ! PageExists ( _type ) ) {
47
+ LogWarning ( "You are trying to turn a page on [" + _type + "] that has not been registered." ) ;
48
+ return ;
49
+ }
50
+
51
+ Page _page = GetPage ( _type ) ;
52
+ _page . gameObject . SetActive ( true ) ;
53
+ await _page . Animate ( true ) ;
54
+ }
55
+
56
+ public async void TurnPageOff ( PageType _type ) {
57
+ if ( ! PageExists ( _type ) ) {
58
+ LogWarning ( "You are trying to turn a page off [" + _type + "] that has not been registered." ) ;
59
+ return ;
60
+ }
61
+
62
+ Page _page = GetPage ( _type ) ;
63
+ await _page . Animate ( false ) ;
64
+ //_page.gameObject.SetActive(false);
65
+ }
66
+ #endregion
67
+
68
+ #region Private Functions
69
+ private void RegisterAllPages ( ) {
70
+ foreach ( Page _page in pages ) {
71
+ RegisterPage ( _page ) ;
72
+ }
73
+ }
74
+
75
+ private void RegisterPage ( Page _page ) {
76
+ if ( PageExists ( _page . type ) ) {
77
+ LogWarning ( "You are trying to register a page [" + _page . type + "] that has already been registered: <color=#f00>" + _page . gameObject . name + "</color>." ) ;
78
+ return ;
79
+ }
80
+
81
+ m_Pages . Add ( _page . type , _page ) ;
82
+ Log ( "Registered new page [" + _page . type + "]." ) ;
83
+ }
84
+
85
+ private Page GetPage ( PageType _type ) {
86
+ if ( ! PageExists ( _type ) ) {
87
+ LogWarning ( "You are trying to get a page [" + _type + "] that has not been registered." ) ;
88
+ return null ;
89
+ }
90
+
91
+ return ( Page ) m_Pages [ _type ] ;
92
+ }
93
+
94
+ private bool PageExists ( PageType _type ) {
95
+ return m_Pages . ContainsKey ( _type ) ;
96
+ }
97
+
98
+ private void Log ( string _msg ) {
99
+ if ( ! debug ) return ;
100
+ Debug . Log ( "[Page Controller]: " + _msg ) ;
101
+ }
102
+
103
+ private void LogWarning ( string _msg ) {
104
+ if ( ! debug ) return ;
105
+ Debug . LogWarning ( "[Page Controller]: " + _msg ) ;
106
+ }
107
+ #endregion
11
108
}
12
109
}
13
110
}
0 commit comments