Skip to content

Commit ca19d82

Browse files
feat: scene parenting: editor time child scene loader for composed scenes (#653)
adding this to the boss room main scene
1 parent 5044c7f commit ca19d82

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

Assets/BossRoom/Scenes/BossRoom.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:cd0792f12a7e463be8d0d45b8ff8f1c2c53933b1d93c5cc17e70ee045756c7a4
3-
size 503413
2+
oid sha256:3de6367e1590522862f4e215fee25c7625e74596ab09067345a67c895080139c
3+
size 508257

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fix: remove initial ugs popup [MTT-3563] (#650) --> Users who do not use UGS wil
2121
chore: bump NGO to pre.9 (#643)
2222
chore: bump boss room to 2021 [MTT-3022] (#620)
2323
fix: folders and assemblies refactor MTT-2623, MTT-2615 (#628)
24+
feat: updated boss room's root scene to automatically load child scenes at editor time (#653)
2425

2526
### Removed
2627
chore: remove UNET [MTT-3435] (#638) --> removed deprecated UNET transport from Boss Room

Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Added
66
feat: other players loading progress in loading screen [MTT-2239] (#580)
7+
feat: adding editor child scene loader for composed scenes (#653)
78

89
### Changed
910

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Packages/com.unity.multiplayer.samples.coop/Utilities/EditorChildSceneLoader.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)