8000 menu system equipped with basic on/off functionality for pages. anima… · coderDarren/UnityCore@22f37b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 22f37b1

Browse files
committed
menu system equipped with basic on/off functionality for pages. animations included
1 parent 7c6abc5 commit 22f37b1

File tree

3 files changed

+172
-4
lines changed

3 files changed

+172
-4
lines changed

Menu/Page.cs

+63-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-

1+
using System.Threading.Tasks;
22
using UnityEngine;
33

44
namespace UnityCore {
@@ -7,7 +7,68 @@ namespace Menu {
77

88
public class Page : MonoBehaviour
99
{
10-
10+
public PageType type;
11+
public bool useAnimation;
12+
public bool isAnimating {get;private set;}
13+
14+
/*
15+
* Animaton Requirements...
16+
* - This class uses certain controls to determine page state
17+
* - Pages have three core states:
18+
* 1. Resting
19+
* 2. Turning On
20+
* 3. Turning Off
21+
* - The animator must have a control boolean called 'on'. Otherwise a flag will be thrown.
22+
*/
23+
private Animator m_Animator;
24+
25+
#region Unity Functions
26+
private void OnEnable() {
27+
CheckAnimatorIntegrity();
28+
}
29+
#endregion
30+
31+
#region Public Functions
32+
/// <summary>
33+
/// Call this to turn the page on or off by setting the control '_on'
34+
/// </summary>
35+
public async Task<bool> Animate(bool _on) {
36+
if (useAnimation) {
37+
m_Animator.SetBool("on", _on);
38+
39+
// wait for animation to finish
40+
isAnimating = true;
41+
while (m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1) {
42+
await Task.Delay(50);
43+
}
44+
isAnimating = false;
45+
46+
Log("Page ["+type+"] finished transitioning to "+(_on ? "<color=#0f0>on</color>." : "<color=#f00>off</color>."));
47+
}
48+
49+
return true;
50+
}
51+
#endregion
52+
53+
#region Private Functions
54+
private void CheckAnimatorIntegrity() {
55+
if (useAnimation) {
56+
// try to get animator
57+
m_Animator = GetComponent<Animator>();
58+
if (!m_Animator) {
59+
LogWarning("You opted to animate page ["+type+"], but no Animator component exists on the object.");
60+
}
61+
}
62+
}
63+
64+
private void Log(string _msg) {
65+
Debug.Log("[Page]: "+_msg);
66+
}
67+
68+
private void LogWarning(string _msg) {
69+
Debug.LogWarning("[Page]: "+_msg);
70+
}
71+
#endregion
1172
}
1273
}
1374
}

Menu/PageController.cs

+99-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-

1+
using System.Collections.Generic;
2+
using System.Collections;
3+
using System.Threading.Tasks;
24
using UnityEngine;
35

46
namespace UnityCore {
@@ -7,7 +9,102 @@ namespace Menu {
79

810
public class PageController : MonoBehaviour
911
{
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
11108
}
12109
}
13110
}

Menu/PageType.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace UnityCore {
2+
3+
namespace Menu {
4+
5+
public enum PageType {
6+
None,
7+
Loading
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)
0