Skip to content

Commit ff010d4

Browse files
committed
Scene controller system added
1 parent 0cf7da7 commit ff010d4

File tree

6 files changed

+240
-1
lines changed

6 files changed

+240
-1
lines changed

Data/DataController.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class DataController : MonoBehaviour
1111
private static readonly string DATA_HIGHSCORE = "highscore";
1212
private static readonly int DEFAULT_INT = 0;
1313

14+
public static DataController instance;
15+
1416
public bool debug;
1517

1618
#region Properties
@@ -42,6 +44,14 @@ public int Score {
4244
}
4345
#endregion
4446

47+
#region Unity Functions
48+
private void Awake() {
49+
if (!instance) {
50+
instance = this;
51+
}
52+
}
53+
#endregion
54+
4555
#region Private Functions
4656
private void SaveInt(string _data, int _value) {
4757
PlayerPrefs.SetInt(_data, _value);

Menu/Page.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public class Page : MonoBehaviour
2525
* - The animator must have a control boolean called 'on'. Otherwise the animator will not work.
2626
*/
2727
private Animator m_Animator;
28+
private bool m_IsOn;
29+
30+
public bool isOn {
31+
get {
32+
return m_IsOn;
33+
}
34+
private set {
35+
m_IsOn = value;
36+
}
37+
}
2838

2939
#region Unity Functions
3040
private void OnEnable() {
@@ -44,7 +54,10 @@ public void Animate(bool _on) {
4454
StartCoroutine("AwaitAnimation", _on);
4555
} else {
4656
if (!_on) {
57+
isOn = false;
4758
gameObject.SetActive(false);
59+
} else {
60+
isOn = true;
4861
}
4962
}
5063
}
@@ -66,7 +79,10 @@ private IEnumerator AwaitAnimation(bool _on) {
6679
Log("Page ["+type+"] finished transitioning to "+(_on ? "<color=#0f0>on</color>." : "<color=#f00>off</color>."));
6780

6881
if (!_on) {
82+
isOn = false;
6983
gameObject.SetActive(false);
84+
} else {
85+
isOn = true;
7086
}
7187
}
7288

Menu/PageController.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ private void Awake() {
3030
if (entryPage != PageType.None) {
3131
TurnPageOn(entryPage);
3232
}
33+
34+
DontDestroyOnLoad(gameObject);
35+
} else {
36+
Destroy(gameObject);
3337
}
3438
}
3539
#endregion
@@ -75,6 +79,15 @@ public void TurnPageOff(PageType _off, PageType _on=PageType.None, bool _waitFor
7579
TurnPageOn(_on);
7680
}
7781
}
82+
83+
public bool PageIsOn(PageType _type) {
84+
if (!PageExists(_type)) {
85+
LogWarning("You are trying to detect if a page is on ["+_type+"], but it has not been registered.");
86+
return false;
87+
}
88+
89+
return GetPage(_type).isOn;
90+
}
7891
#endregion
7992

8093
#region Private Functions

Scene/SceneController.cs

Lines changed: 161 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,173 @@
1-

1+
using System.Collections;
2+
using System.Threading.Tasks;
23
using UnityEngine;
4+
using UnityEngine.SceneManagement;
5+
using UnityCore.Menu;
36

47
namespace UnityCore {
58

69
namespace Scene {
710

811
public class SceneController : MonoBehaviour
912
{
13+
public delegate void SceneLoadDelegate(SceneType _scene);
14+
15+
public static SceneController instance;
16+
17+
public bool debug;
18+
19+
private PageController m_Menu;
20+
private SceneType m_TargetScene;
21+
private PageType m_LoadingPage;
22+
private SceneLoadDelegate m_SceneLoadDelegate;
23+
private bool m_SceneIsLoading;
24+
25+
// get menu with integrity
26+
private PageController menu {
27+
get {
28+
if (m_Menu == null) {
29+
m_Menu = PageController.instance;
30+
}
31+
if (m_Menu == null) {
32+
LogWarning("You are trying to access the PageController, but no instance was found.");
33+
}
34+
return m_Menu;
35+
}
36+
}
37+
38+
private string currentSceneName {
39+
get {
40+
return SceneManager.GetActiveScene().name;
41+
}
42+
}
1043

44+
#region Unity Functions
45+
private void Awake() {
46+
if (!instance) {
47+
Configure();
48+
DontDestroyOnLoad(gameObject);
49+
} else {
50+
Destroy(gameObject);
51+
}
52+
}
53+
54+
private void OnDisable() {
55+
Dispose();
56+
}
57+
#endregion
58+
59+
#region Public Functions
60+
public void Load(SceneType _scene, SceneLoadDelegate _sceneLoadDelegate=null, bool _reload=false, PageType _loadingPage=PageType.None) {
61+
if (_loadingPage != PageType.None && !menu) {
62+
return;
63+
}
64+
65+
if (!SceneCanBeLoaded(_scene, _reload)) {
66+
return;
67+
}
68+
69+
m_SceneIsLoading = true;
70+
m_TargetScene = _scene;
71+
m_LoadingPage = _loadingPage;
72+
m_SceneLoadDelegate = _sceneLoadDelegate;
73+
StartCoroutine("LoadScene");
74+
}
75+
#endregion
76+
77+
#region Private Functions
78+
private void Configure() {
79+
instance = this;
80+
SceneManager.sceneLoaded += OnSceneLoaded;
81+
}
82+
83+
private void Dispose() {
84+
SceneManager.sceneLoaded -= OnSceneLoaded;
85+
}
86+
87+
private async void OnSceneLoaded(UnityEngine.SceneManagement.Scene _scene, LoadSceneMode _mode) {
88+
if (m_TargetScene == SceneType.None) {
89+
return;
90+
}
91+
92+
SceneType _sceneType = StringToSceneType(_scene.name);
93+
if (m_TargetScene != _sceneType) {
94+
return;
95+
}
96+
97+
if (m_SceneLoadDelegate != null) {
98+
try {
99+
m_SceneLoadDelegate(_sceneType); // it is possible the delegate is no longer accessible
100+
} catch (System.Exception) {
101+
LogWarning("Unable to respond with sceneLoadDelegate after scene ["+_sceneType+"] loaded.");
102+
}
103+
}
104+
105+
if (m_LoadingPage != PageType.None) {
106+
await Task.Delay(1000);
107+
menu.TurnPageOff(m_LoadingPage);
108+
}
109+
110+
m_SceneIsLoading = false;
111+
}
112+
113+
private IEnumerator LoadScene() {
114+
if (m_LoadingPage != PageType.None) {
115+
menu.TurnPageOn(m_LoadingPage);
116+
while (!menu.PageIsOn(m_LoadingPage)) {
117+
yield return null;
118+
}
119+
}
120+
121+
string _targetSceneName = SceneTypeToString(m_TargetScene);
122+
SceneManager.LoadScene(_targetSceneName);
123+
}
124+
125+
private bool SceneCanBeLoaded(SceneType _scene, bool _reload) {
126+
string _targetSceneName = SceneTypeToString(_scene);
127+
if (currentSceneName == _targetSceneName && !_reload) {
128+
LogWarning("You are trying to load a scene ["+_scene+"] which is already active.");
129+
return false;
130+
} else if (_targetSceneName == string.Empty) {
131+
LogWarning("The scene you are trying to load ["+_scene+"] is not valid.");
132+
return false;
133+
} else if (m_SceneIsLoading) {
134+
LogWarning("Unable to load scene ["+_scene+"]. Another scene ["+m_TargetScene+"] is already loading.");
135+
return false;
136+
}
137+
138+
return true;
139+
}
140+
141+
private string SceneTypeToString(SceneType _scene) {
142+
switch (_scene) {
143+
case SceneType.Game: return "Game";
144+
case SceneType.Menu: return "Menu";
145+
default:
146+
LogWarning("Scene ["+_scene+"] does not contain a string for a valid scene.");
147+
return string.Empty;
148+
}
149+
}
150+
151+
private SceneType StringToSceneType(string _scene) {
152+
switch (_scene) {
153+
case "Game": return SceneType.Game;
154+
case "Menu": return SceneType.Menu;
155+
default:
156+
LogWarning("Scene ["+_scene+"] does not contain a type for a valid scene.");
157+
return SceneType.None;
158+
}
159+
}
160+
161+
private void Log(string _msg) {
162+
if (!debug) return;
163+
Debug.Log("[Scene Controller]: "+_msg);
164+
}
165+
166+
private void LogWarning(string _msg) {
167+
if (!debug) return;
168+
Debug.LogWarning("[Scene Controller]: "+_msg);
169+
}
170+
#endregion
11171
}
12172
}
13173
}

Scene/SceneType.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+

2+
namespace UnityCore {
3+
4+
namespace Scene {
5+
6+
public enum SceneType {
7+
None,
8+
Menu,
9+
Game,
10+
}
11+
}
12+
}

Scene/TestScene.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+

2+
using UnityEngine;
3+
using UnityCore.Menu;
4+
5+
namespace UnityCore {
6+
7+
namespace Scene {
8+
9+
public class TestScene : MonoBehaviour
10+
{
11+
12+
public SceneController sceneController;
13+
14+
#region Unity Functions
15+
#if UNITY_EDITOR
16+
private void Update() {
17+
if (Input.GetKeyUp(KeyCode.M)) {
18+
sceneController.Load(SceneType.Menu, (_scene) => {Debug.Log("Scene ["+_scene+"] loaded from test script!");}, false, PageType.Loading);
19+
}
20+
if (Input.GetKeyUp(KeyCode.G)) {
21+
sceneController.Load(SceneType.Game);
22+
}
23+
}
24+
#endif
25+
#endregion
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)