|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using UnityEngine; |
4 | 4 | using UnityEngine.SceneManagement; |
5 | | - |
6 | | -[System.Serializable] |
7 | | -public class AdditiveSceneRef { |
8 | | - public string scene_name; |
9 | | - public Object scene_ref; |
10 | | -} |
| 5 | +using UnityEngine.Events; |
11 | 6 |
|
12 | 7 | public class AdditiveSceneManager : MonoBehaviour |
13 | 8 | { |
14 | 9 |
|
15 | 10 | // Global instance |
16 | 11 | public static AdditiveSceneManager Instance; |
| 12 | + |
| 13 | + [Header("=== Scene Lists ===")] |
| 14 | + [Tooltip("List of additive scenes. These scenes must be added to \"Build Settings\" too.")] |
| 15 | + public Object[] scenes; |
| 16 | + public Dictionary<string, Object> sceneDict; |
| 17 | + private List<string> activeScenes = new List<string>(); |
17 | 18 |
|
18 | | - // List of scenes |
19 | | - public AdditiveSceneRef[] scenes; |
20 | | - |
21 | | - // Adictionary of scenes based on `scenes`; generated |
22 | | - |
| 19 | + [Header("=== Callbacks ===")] |
| 20 | + public UnityEvent onSceneLoadedCallback; |
| 21 | + public UnityEvent onSceneUnloadedCallback; |
23 | 22 |
|
24 | 23 | // Awake |
25 | 24 | private void Awake() { |
26 | 25 | Instance = this; |
| 26 | + InitializeScenes(); |
| 27 | + } |
| 28 | + |
| 29 | + private void InitializeScenes() { |
| 30 | + // Load listeners |
| 31 | + SceneManager.sceneLoaded += OnSceneLoaded; |
| 32 | + SceneManager.sceneUnloaded += OnSceneUnloaded; |
| 33 | + |
| 34 | + // Initialize arrays and dictionary |
| 35 | + activeScenes = new List<string>(); |
| 36 | + sceneDict = new Dictionary<string, Object>(); |
| 37 | + foreach(Object s in scenes) { |
| 38 | + sceneDict.Add(s.name, s); |
| 39 | + } |
27 | 40 | } |
28 | 41 |
|
29 | 42 | // Add Scene from `scenes` to scene |
30 | | - public void AddScene(string quuery) { |
| 43 | + public void LoadScene(string query, LoadSceneMode mode = LoadSceneMode.Additive) { |
| 44 | + if(!sceneDict.ContainsKey(query)) { |
| 45 | + Debug.LogError($"Query scene \"{query}\" doesn't exist in this scene manager!"); |
| 46 | + return; |
| 47 | + } |
| 48 | + if (activeScenes.Contains(query)) { |
| 49 | + Debug.LogError($"Query scene \"{query}\" is already loaded additively. Will not add scene again."); |
| 50 | + return; |
| 51 | + } |
| 52 | + SceneManager.LoadScene(sceneDict[query].name, mode); |
| 53 | + } |
| 54 | + |
| 55 | + public void UnloadScene(string query) { |
| 56 | + if (!sceneDict.ContainsKey(query)) { |
| 57 | + Debug.LogError($"Query scene \"{query}\" doesn't exist in this scene manager!"); |
| 58 | + return; |
| 59 | + } |
| 60 | + if (!activeScenes.Contains(query)) { |
| 61 | + Debug.LogError($"Query scene \"{query}\" is not loaded."); |
| 62 | + return; |
| 63 | + } |
| 64 | + SceneManager.UnloadSceneAsync(sceneDict[query].name); |
| 65 | + } |
| 66 | + |
| 67 | + public void OnSceneLoaded(Scene scene, LoadSceneMode mode) { |
| 68 | + SceneManager.SetActiveScene(scene); |
| 69 | + if (!activeScenes.Contains(scene.name)) activeScenes.Add(scene.name); |
| 70 | + Debug.Log($"Scene \"{scene.name}\" loaded!"); |
| 71 | + onSceneLoadedCallback?.Invoke(); |
| 72 | + } |
| 73 | + |
| 74 | + public void OnSceneUnloaded(Scene scene) { |
| 75 | + if (activeScenes.Contains(scene.name)) activeScenes.Remove(scene.name); |
| 76 | + Debug.Log($"Scene \"scene.name\" unloaded!"); |
| 77 | + onSceneUnloadedCallback?.Invoke(); |
| 78 | + } |
| 79 | + |
| 80 | + public bool QuerySceneLoaded(string query) { |
| 81 | + return activeScenes.Contains(query); |
| 82 | + } |
| 83 | + |
| 84 | + void OnDestroy() { |
| 85 | + SceneManager.sceneLoaded -= OnSceneLoaded; |
| 86 | + SceneManager.sceneUnloaded -= OnSceneUnloaded; |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + |
31 | 91 |
|
32 | | - } |
33 | 92 |
|
34 | 93 |
|
35 | 94 | } |
0 commit comments