Skip to content

Commit 3242b1d

Browse files
committed
Streamlined the code so the developer doesn't have to add a SceneType and then add the cases in two other places for that SceneType enumeration.
The only rule is to name the enumeration just like the file name of the scene in Unity.
1 parent 7954c47 commit 3242b1d

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
*.meta
2-
2+
.idea

Scene/SceneController.cs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,14 @@ private IEnumerator LoadScene() {
118118
}
119119
}
120120

121-
string _targetSceneName = SceneTypeToString(m_TargetScene);
121+
string _targetSceneName = m_TargetScene;
122+
// string _targetSceneName = SceneTypeToString(m_TargetScene);
122123
SceneManager.LoadScene(_targetSceneName);
123124
}
124125

125126
private bool SceneCanBeLoaded(SceneType _scene, bool _reload) {
126-
string _targetSceneName = SceneTypeToString(_scene);
127+
string _targetSceneName = _scene;
128+
// string _targetSceneName = SceneTypeToString(_scene);
127129
if (currentSceneName == _targetSceneName && !_reload) {
128130
LogWarning("You are trying to load a scene ["+_scene+"] which is already active.");
129131
return false;
@@ -138,24 +140,32 @@ private bool SceneCanBeLoaded(SceneType _scene, bool _reload) {
138140
return true;
139141
}
140142

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-
}
143+
// No longer needed if scene type enums are named like the scene files in Unity
144+
// private string SceneTypeToString(SceneType _scene) {
145+
// switch (_scene) {
146+
// case SceneType.Game: return "Game";
147+
// case SceneType.Menu: return "Menu";
148+
// default:
149+
// LogWarning("Scene ["+_scene+"] does not contain a string for a valid scene.");
150+
// return string.Empty;
151+
// }
152+
// }
150153

151154
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;
155+
// Dynamic enumeration so you wouldn't have to manually add case for each individual scene you create in game (just add the type in SceneType)
156+
foreach (string name in Enum.GetNames(typeof(SceneType))) {
157+
if (name == _scene) {
158+
return (SceneType) Enum.Parse(typeof(SceneType), name);
159+
}
158160
}
161+
return SceneType.None
162+
// switch (_scene) {
163+
// case "Game": return SceneType.Game;
164+
// case "Menu": return SceneType.Menu;
165+
// default:
166+
// LogWarning("Scene ["+_scene+"] does not contain a type for a valid scene.");
167+
// return SceneType.None;
168+
// }
159169
}
160170

161171
private void Log(string _msg) {

Scene/SceneType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ namespace UnityCore {
33

44
namespace Scene {
55

6+
// Note: Name your SceneType enumerations just like the scene files in your Unity Scenes folder
7+
// i.e. MainMenuScene, Level01Scene, etc
68
public enum SceneType {
79
None,
810
Menu,

0 commit comments

Comments
 (0)