From 1b61f92c97486802fe7355a22583522f5b1313f5 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Mon, 1 Aug 2022 21:27:38 -0400 Subject: [PATCH 1/3] Removing config object from builds and scene hierarchy. They are now only editable through menu. (still saved in scene, but invisible). This way there's no more garbage in game scenes and this config is still saved per scene. Adding automatic expand of child scenes when loading root scene. --- Assets/Scenes/BossRoom.unity | 4 +- .../Utilities/EditorChildSceneLoader.cs | 104 +++++++++++++++--- 2 files changed, 93 insertions(+), 15 deletions(-) diff --git a/Assets/Scenes/BossRoom.unity b/Assets/Scenes/BossRoom.unity index b725b54a6..4214d3ea1 100644 --- a/Assets/Scenes/BossRoom.unity +++ b/Assets/Scenes/BossRoom.unity @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e3f00d58c6349986e4c2ca3da5c3d980eeda05bd854d63e23d1f3ab33f034b71 -size 509756 +oid sha256:17e307041ee666cac5009c66b5422807916f1f54025c1118d2e5a2ee9721c081 +size 509770 diff --git a/Packages/com.unity.multiplayer.samples.coop/Utilities/EditorChildSceneLoader.cs b/Packages/com.unity.multiplayer.samples.coop/Utilities/EditorChildSceneLoader.cs index 6cf14c9d3..6e11e8d19 100644 --- a/Packages/com.unity.multiplayer.samples.coop/Utilities/EditorChildSceneLoader.cs +++ b/Packages/com.unity.multiplayer.samples.coop/Utilities/EditorChildSceneLoader.cs @@ -6,6 +6,7 @@ #endif using UnityEngine; using UnityEngine.SceneManagement; +using Object = UnityEngine.Object; /// /// 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. @@ -44,32 +45,109 @@ public void ResetSceneSetupToConfig() sceneSetupToLoad[0].isActive = true; EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo(); EditorSceneManager.RestoreSceneManagerSetup(sceneSetupToLoad.ToArray()); + + for (int i = 0; i < SceneManager.sceneCount; i++) + { + var scene = SceneManager.GetSceneAt(i); + + // hacky way to "expand" a scene. If someone found a cleaner way to do this, let me know + var selection = new List(); + selection.AddRange(Selection.objects); + selection.Add(scene.GetRootGameObjects()[0]); + Selection.objects = selection.ToArray(); + } } -#endif -} -#if UNITY_EDITOR -[CustomEditor(typeof(EditorChildSceneLoader))] -public class ChildSceneLoaderInspectorGUI : Editor -{ - public override void OnInspectorGUI() + const string k_MenuBase = "Boss Room/Child Scene Loader"; + + [MenuItem(k_MenuBase + "/Save Scene Setup To Config")] + static void DoSaveSceneSetupMenu() { - base.OnInspectorGUI(); + var activeScene = EditorSceneManager.GetActiveScene(); + var wasDirty = activeScene.isDirty; + var foundLoaders = GameObject.FindObjectsOfType(); + EditorChildSceneLoader loader; + if (foundLoaders.Length == 0) + { + // create loader in scene + var supportingGameObject = new GameObject("YOU SHOULD NOT SEE THIS"); + supportingGameObject.hideFlags = HideFlags.DontSaveInBuild | HideFlags.HideInHierarchy; + loader = supportingGameObject.AddComponent(); + SceneManager.MoveGameObjectToScene(supportingGameObject, activeScene); + } + else + { + loader = foundLoaders[0]; + } + + loader.SaveSceneSetup(); + EditorSceneManager.MarkSceneDirty(loader.gameObject.scene); + TrySave(wasDirty, activeScene); + ReadConfig(); + } + + static void TrySave(bool wasDirty, Scene activeScene) + { + if (!wasDirty) + { + EditorSceneManager.SaveScene(activeScene); + } + else + { + EditorSceneManager.SaveModifiedScenesIfUserWantsTo(new[] { activeScene }); + } + } - var currentInspectorObject = (EditorChildSceneLoader)target; + static EditorChildSceneLoader TryFind() + { + var foundLoaders = GameObject.FindObjectsOfType(); + if (foundLoaders.Length > 1) + { + throw new Exception("not normal, should only have one loaded child scene loader"); + } - if (GUILayout.Button("Save scene setup to config")) + if (foundLoaders.Length == 0) { - currentInspectorObject.SaveSceneSetup(); + throw new Exception("couldn't find any child scene loader, please use Save Scene setup"); } - if (GUILayout.Button("Reset scene setup from config...")) + return foundLoaders[0]; + } + + [MenuItem(k_MenuBase + "/Remove Config")] + static void RemoveConfig() + { + var foundObj = TryFind().gameObject; + var parentScene = foundObj.scene; + var wasDirty = parentScene.isDirty; + DestroyImmediate(foundObj); + EditorSceneManager.MarkSceneDirty(parentScene); + TrySave(wasDirty, parentScene); + } + + [MenuItem(k_MenuBase + "/Load Scene Setup from Config")] + static void DoResetSceneToConfig() + { + TryFind().ResetSceneSetupToConfig(); + } + + [MenuItem(k_MenuBase + "/Read Current Config")] + static void ReadConfig() + { + var foundLoader = TryFind(); + string toPrint = $"To Load ({foundLoader.ChildScenesToLoadConfig.Count}): "; + foreach (var config in foundLoader.ChildScenesToLoadConfig) { - currentInspectorObject.ResetSceneSetupToConfig(); + toPrint += $"{config.name}, "; } + + Debug.Log(toPrint); } +#endif } +#if UNITY_EDITOR + [InitializeOnLoad] public class ChildSceneLoader { From c914be49e351c1d82c8f17d12fd76af982420fb6 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Mon, 1 Aug 2022 21:36:37 -0400 Subject: [PATCH 2/3] cleanup --- .../Utilities/EditorChildSceneLoader.cs | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Packages/com.unity.multiplayer.samples.coop/Utilities/EditorChildSceneLoader.cs b/Packages/com.unity.multiplayer.samples.coop/Utilities/EditorChildSceneLoader.cs index 6e11e8d19..38190f987 100644 --- a/Packages/com.unity.multiplayer.samples.coop/Utilities/EditorChildSceneLoader.cs +++ b/Packages/com.unity.multiplayer.samples.coop/Utilities/EditorChildSceneLoader.cs @@ -17,12 +17,14 @@ public class EditorChildSceneLoader : MonoBehaviour [SerializeField] public List ChildScenesToLoadConfig; + const string k_MenuBase = "Boss Room/Child Scene Loader"; + void Update() { // DO NOT DELETE keep this so we can enable/disable this script... (used in ChildSceneLoader) } - public void SaveSceneSetup() + void SaveSceneSetup() { ChildScenesToLoadConfig ??= new List(); ChildScenesToLoadConfig.Clear(); @@ -58,8 +60,6 @@ public void ResetSceneSetupToConfig() } } - const string k_MenuBase = "Boss Room/Child Scene Loader"; - [MenuItem(k_MenuBase + "/Save Scene Setup To Config")] static void DoSaveSceneSetupMenu() { @@ -71,7 +71,7 @@ static void DoSaveSceneSetupMenu() { // create loader in scene var supportingGameObject = new GameObject("YOU SHOULD NOT SEE THIS"); - supportingGameObject.hideFlags = HideFlags.DontSaveInBuild | HideFlags.HideInHierarchy; + supportingGameObject.hideFlags = HideFlags.DontSaveInBuild | HideFlags.HideInHierarchy; // object only findable through scripts loader = supportingGameObject.AddComponent(); SceneManager.MoveGameObjectToScene(supportingGameObject, activeScene); } @@ -82,11 +82,12 @@ static void DoSaveSceneSetupMenu() loader.SaveSceneSetup(); EditorSceneManager.MarkSceneDirty(loader.gameObject.scene); - TrySave(wasDirty, activeScene); - ReadConfig(); + TrySaveScene(wasDirty, activeScene); + PrintConfig(); } - static void TrySave(bool wasDirty, Scene activeScene) + // wasDirty: was the scene dirty before modifying it? if not, will try to save it directly without asking the user + static void TrySaveScene(bool wasDirty, Scene activeScene) { if (!wasDirty) { @@ -98,7 +99,7 @@ static void TrySave(bool wasDirty, Scene activeScene) } } - static EditorChildSceneLoader TryFind() + static EditorChildSceneLoader TryFindLoader() { var foundLoaders = GameObject.FindObjectsOfType(); if (foundLoaders.Length > 1) @@ -117,24 +118,24 @@ static EditorChildSceneLoader TryFind() [MenuItem(k_MenuBase + "/Remove Config")] static void RemoveConfig() { - var foundObj = TryFind().gameObject; + var foundObj = TryFindLoader().gameObject; var parentScene = foundObj.scene; var wasDirty = parentScene.isDirty; DestroyImmediate(foundObj); EditorSceneManager.MarkSceneDirty(parentScene); - TrySave(wasDirty, parentScene); + TrySaveScene(wasDirty, parentScene); } [MenuItem(k_MenuBase + "/Load Scene Setup from Config")] static void DoResetSceneToConfig() { - TryFind().ResetSceneSetupToConfig(); + TryFindLoader().ResetSceneSetupToConfig(); } - [MenuItem(k_MenuBase + "/Read Current Config")] - static void ReadConfig() + [MenuItem(k_MenuBase + "/Print Current Config")] + static void PrintConfig() { - var foundLoader = TryFind(); + var foundLoader = TryFindLoader(); string toPrint = $"To Load ({foundLoader.ChildScenesToLoadConfig.Count}): "; foreach (var config in foundLoader.ChildScenesToLoadConfig) { From 99be6c7d6f9222202bc2f8cdd46f5713269004c7 Mon Sep 17 00:00:00 2001 From: Samuel Bellomo Date: Mon, 1 Aug 2022 21:40:49 -0400 Subject: [PATCH 3/3] Adding changelog --- Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md b/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md index d04f13166..4576f8434 100644 --- a/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md +++ b/Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md @@ -5,7 +5,7 @@ ### Added * ### Changed -* +* Cleaning up child scene loader to remove garbage gameObject and adding automatic expand of child scenes (#704) ### Removed * ### Fixed