|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +#if UNITY_EDITOR |
| 4 | +using UnityEditor; |
| 5 | +using UnityEditor.SceneManagement; |
| 6 | +#endif |
| 7 | +using UnityEngine; |
| 8 | +using UnityEngine.SceneManagement; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Allows setting a scene as a root scene and setting its child scenes. To use this, drag this component on any object in a scene to make that scene a root scene. In the background, ChildSceneLoader will automatically manage this. |
| 12 | +/// </summary> |
| 13 | +public class EditorChildSceneLoader : MonoBehaviour |
| 14 | +{ |
| 15 | +#if UNITY_EDITOR |
| 16 | + [SerializeField] |
| 17 | + public List<SceneAsset> ChildScenesToLoadConfig; |
| 18 | + |
| 19 | + void Update() |
| 20 | + { |
| 21 | + // DO NOT DELETE keep this so we can enable/disable this script... (used in ChildSceneLoader) |
| 22 | + } |
| 23 | + |
| 24 | + public void SaveSceneSetup() |
| 25 | + { |
| 26 | + ChildScenesToLoadConfig ??= new(); |
| 27 | + ChildScenesToLoadConfig.Clear(); |
| 28 | + foreach (var sceneSetup in EditorSceneManager.GetSceneManagerSetup()) |
| 29 | + { |
| 30 | + ChildScenesToLoadConfig.Add(AssetDatabase.LoadAssetAtPath<SceneAsset>(sceneSetup.path)); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public void ResetSceneSetupToConfig() |
| 35 | + { |
| 36 | + var sceneAssetsToLoad = ChildScenesToLoadConfig; |
| 37 | + |
| 38 | + List<SceneSetup> sceneSetupToLoad = new(); |
| 39 | + foreach (var sceneAsset in sceneAssetsToLoad) |
| 40 | + { |
| 41 | + sceneSetupToLoad.Add(new SceneSetup() { path = AssetDatabase.GetAssetPath(sceneAsset), isActive = false, isLoaded = true }); |
| 42 | + } |
| 43 | + |
| 44 | + sceneSetupToLoad[0].isActive = true; |
| 45 | + EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo(); |
| 46 | + EditorSceneManager.RestoreSceneManagerSetup(sceneSetupToLoad.ToArray()); |
| 47 | + } |
| 48 | +#endif |
| 49 | +} |
| 50 | + |
| 51 | +#if UNITY_EDITOR |
| 52 | +[CustomEditor(typeof(EditorChildSceneLoader))] |
| 53 | +public class ChildSceneLoaderInspectorGUI : Editor |
| 54 | +{ |
| 55 | + public override void OnInspectorGUI() |
| 56 | + { |
| 57 | + base.OnInspectorGUI(); |
| 58 | + |
| 59 | + var currentInspectorObject = (EditorChildSceneLoader)target; |
| 60 | + |
| 61 | + if (GUILayout.Button("Save scene setup to config")) |
| 62 | + { |
| 63 | + currentInspectorObject.SaveSceneSetup(); |
| 64 | + } |
| 65 | + |
| 66 | + if (GUILayout.Button("Reset scene setup from config...")) |
| 67 | + { |
| 68 | + currentInspectorObject.ResetSceneSetupToConfig(); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +[InitializeOnLoad] |
| 74 | +public class ChildSceneLoader |
| 75 | +{ |
| 76 | + static ChildSceneLoader() |
| 77 | + { |
| 78 | + EditorSceneManager.sceneOpened += OnSceneLoaded; |
| 79 | + } |
| 80 | + |
| 81 | + static void OnSceneLoaded(Scene _, OpenSceneMode mode) |
| 82 | + { |
| 83 | + if (mode != OpenSceneMode.Single || BuildPipeline.isBuildingPlayer) return; // try to load child scenes only for root scenes or if not building |
| 84 | + |
| 85 | + var scenesToLoadObjects = GameObject.FindObjectsOfType<EditorChildSceneLoader>(); |
| 86 | + if (scenesToLoadObjects.Length > 1) |
| 87 | + { |
| 88 | + throw new Exception("Should only have one root scene at once loaded"); |
| 89 | + } |
| 90 | + |
| 91 | + if (scenesToLoadObjects.Length == 0 || !scenesToLoadObjects[0].enabled) // only when we have a config and when that config is enabled |
| 92 | + { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + scenesToLoadObjects[0].ResetSceneSetupToConfig(); |
| 97 | + |
| 98 | + Debug.Log("Setup done for root scene and child scenes"); |
| 99 | + } |
| 100 | +} |
| 101 | +#endif |
0 commit comments