Skip to content

Commit 80d1848

Browse files
Ryan KimRyan Kim
authored andcommitted
additive scene manager bugfix with scene names and objects. FrameCount now can render to a specific TextMeshpro textbox. Prefabs for framecount now position the frame counters relative to the screen
1 parent 27ae151 commit 80d1848

File tree

7 files changed

+634
-17
lines changed

7 files changed

+634
-17
lines changed

AdditiveSceneManager/AdditiveSceneManager.cs

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,97 @@
77
public class AdditiveSceneManager : MonoBehaviour
88
{
99

10+
[System.Serializable]
11+
public class Ref
12+
{
13+
public string key;
14+
public GameObject reference;
15+
}
16+
1017
// Global instance
1118
public static AdditiveSceneManager Instance;
1219

1320
[Header("=== Scene Lists ===")]
1421
[Tooltip("List of additive scenes. These scenes must be added to \"Build Settings\" too.")]
22+
#if UNITY_EDITOR
1523
public Object[] scenes;
16-
public Dictionary<string, Object> sceneDict;
24+
#endif
25+
[HideInInspector]
26+
public List<string> scene_names;
27+
private Dictionary<string, Object> sceneDict;
1728
private List<string> activeScenes = new List<string>();
1829

30+
[Header("=== Reference Management ===")]
31+
public List<Ref> references = new List<Ref>();
32+
private Dictionary<string, GameObject> refDict;
33+
1934
[Header("=== Callbacks ===")]
2035
public UnityEvent onSceneLoadedCallback;
2136
public UnityEvent onSceneUnloadedCallback;
2237

38+
#if UNITY_EDITOR
39+
private void OnValidate()
40+
{
41+
scene_names.Clear();
42+
foreach (Object s in scenes) {
43+
if (s != null) scene_names.Add(s.name);
44+
}
45+
}
46+
#endif
47+
2348
// Awake
2449
private void Awake() {
2550
Instance = this;
2651
InitializeScenes();
52+
InitializeRefs();
2753
}
2854

2955
private void InitializeScenes() {
3056
// Load listeners
3157
SceneManager.sceneLoaded += OnSceneLoaded;
3258
SceneManager.sceneUnloaded += OnSceneUnloaded;
33-
3459
// Initialize arrays and dictionary
3560
activeScenes = new List<string>();
36-
sceneDict = new Dictionary<string, Object>();
37-
foreach(Object s in scenes) {
38-
sceneDict.Add(s.name, s);
39-
}
4061
}
4162

42-
// Add Scene from `scenes` to scene
43-
public void LoadScene(string query, LoadSceneMode mode = LoadSceneMode.Additive) {
44-
if(!sceneDict.ContainsKey(query)) {
63+
private void InitializeRefs()
64+
{
65+
refDict = new Dictionary<string, GameObject>();
66+
foreach(Ref r in references) refDict.Add(r.key, r.reference);
67+
}
68+
69+
// Add Scene from `_scenes` to scene
70+
public void LoadScene(string query, LoadSceneMode mode) {
71+
if(!scene_names.Contains(query)) {
4572
Debug.LogError($"Query scene \"{query}\" doesn't exist in this scene manager!");
4673
return;
4774
}
4875
if (activeScenes.Contains(query)) {
4976
Debug.LogError($"Query scene \"{query}\" is already loaded additively. Will not add scene again.");
5077
return;
5178
}
52-
SceneManager.LoadScene(sceneDict[query].name, mode);
79+
SceneManager.LoadScene(query, mode);
80+
}
81+
public void LoadScene(string query) {
82+
LoadScene(query, LoadSceneMode.Additive);
5383
}
5484

5585
public void UnloadScene(string query) {
56-
if (!sceneDict.ContainsKey(query)) {
86+
if (!scene_names.Contains(query)) {
5787
Debug.LogError($"Query scene \"{query}\" doesn't exist in this scene manager!");
5888
return;
5989
}
6090
if (!activeScenes.Contains(query)) {
6191
Debug.LogError($"Query scene \"{query}\" is not loaded.");
6292
return;
6393
}
64-
SceneManager.UnloadSceneAsync(sceneDict[query].name);
94+
SceneManager.UnloadSceneAsync(query);
95+
}
96+
97+
public void ToggleScene(string query)
98+
{
99+
if (activeScenes.Contains(query)) UnloadScene(query);
100+
else LoadScene(query, LoadSceneMode.Additive);
65101
}
66102

67103
public void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
@@ -81,6 +117,13 @@ public bool QuerySceneLoaded(string query) {
81117
return activeScenes.Contains(query);
82118
}
83119

120+
public bool TryGetRef(string query, out GameObject g)
121+
{
122+
bool found = refDict.ContainsKey(query);
123+
g = found ? refDict[query] : null;
124+
return found;
125+
}
126+
84127
void OnDestroy() {
85128
SceneManager.sceneLoaded -= OnSceneLoaded;
86129
SceneManager.sceneUnloaded -= OnSceneUnloaded;

AdditiveSceneManager/Editor/AdditiveSceneManagerEditor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ public override void OnInspectorGUI() {
1818

1919
// Render controls for each scene
2020
EditorGUILayout.LabelField("=== Scene Controls ===", EditorStyles.boldLabel);
21-
foreach(Object s in manager.scenes) SceneButtons(s);
21+
foreach(string s in manager.scene_names) SceneButtons(s);
2222
}
2323

24-
public void SceneButtons(Object s) {
24+
public void SceneButtons(string s) {
2525

2626
// Query if scene is active
27-
bool sceneActive = manager.QuerySceneLoaded(s.name);
27+
bool sceneActive = manager.QuerySceneLoaded(s);
2828

2929
// Start a horizontal group for the buttons
3030
GUILayout.BeginHorizontal();
3131

3232
GUI.enabled = !sceneActive;
33-
if (GUILayout.Button($"Load \"{s.name}\"")) manager.LoadScene(s.name);
33+
if (GUILayout.Button($"Load \"{s}\"")) manager.LoadScene(s);
3434
GUI.enabled = sceneActive;
3535
GUILayout.Space(5);
36-
if (GUILayout.Button($"Unload \"{s.name}\"")) manager.UnloadScene(s.name);
36+
if (GUILayout.Button($"Unload \"{s}\"")) manager.UnloadScene(s);
3737
GUI.enabled = true;
3838

3939
// End the horizontal group

FrameCount/FrameCount.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
using System.Collections;
22
using System.Collections.Generic;
33
using UnityEngine;
4+
using TMPro;
45

56
public class FrameCount : MonoBehaviour
67
{
78

89
public static FrameCount Instance;
10+
11+
public enum TextboxWriteType { FrameCount, FPS, SmoothFPS }
12+
13+
[System.Serializable]
14+
public class Textbox
15+
{
16+
public TextMeshProUGUI textbox;
17+
public TextboxWriteType write_type;
18+
}
919

1020
[Header("=== Calculated (Read-Only) ===")]
1121
[Tooltip("The raw frame number of the current frame. Wil always increment from 0 at the start of the scene.")]
@@ -19,6 +29,8 @@ public class FrameCount : MonoBehaviour
1929
[Header("=== Settings ===")]
2030
[Range(0f,1f), Tooltip("The ratio for calculating the smoothed FPS. 1 = focus only on raw FPS on the current frame, 0 = focus only on the previous FPS of the previous frame.")]
2131
public float smoothed_ratio = 0.75f;
32+
[Tooltip("Which textboxes should this write to?")]
33+
public Textbox[] textboxes;
2234

2335
// Outputs readable to other external scripts
2436
public int frame_count => _frame_count;
@@ -34,6 +46,20 @@ private void Update() {
3446
_fps = 1f / Time.unscaledDeltaTime;
3547
_smoothed_fps = _prev_fps * (1f-smoothed_ratio) + _fps * smoothed_ratio;
3648
_prev_fps = _smoothed_fps;
49+
foreach(Textbox t in textboxes) {
50+
switch(t.write_type)
51+
{
52+
case TextboxWriteType.FrameCount:
53+
t.textbox.text = _frame_count.ToString();
54+
break;
55+
case TextboxWriteType.FPS:
56+
t.textbox.text = _fps.ToString();
57+
break;
58+
case TextboxWriteType.SmoothFPS:
59+
t.textbox.text = _smoothed_fps.ToString();
60+
break;
61+
}
62+
}
3763

3864
}
3965

0 commit comments

Comments
 (0)