Skip to content

feat: editor child scene loader for composed scenes #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 20, 2022
Merged
4 changes: 2 additions & 2 deletions Assets/BossRoom/Scenes/BossRoom.unity
Git LFS file not shown
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fix: remove initial ugs popup [MTT-3563] (#650) --> Users who do not use UGS wil
chore: bump NGO to pre.9 (#643)
chore: bump boss room to 2021 [MTT-3022] (#620)
fix: folders and assemblies refactor MTT-2623, MTT-2615 (#628)
feat: updated boss room's root scene to automatically load child scenes at editor time (#653)

### Removed
chore: remove UNET [MTT-3435] (#638) --> removed deprecated UNET transport from Boss Room
Expand Down
1 change: 1 addition & 0 deletions Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
#endif
using UnityEngine;
using UnityEngine.SceneManagement;

/// <summary>
/// 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.
/// </summary>
public class EditorChildSceneLoader : MonoBehaviour
{
#if UNITY_EDITOR
[SerializeField]
public List<SceneAsset> ChildScenesToLoadConfig;

void Update()
{
// DO NOT DELETE keep this so we can enable/disable this script... (used in ChildSceneLoader)
}

public void SaveSceneSetup()
{
ChildScenesToLoadConfig ??= new();
ChildScenesToLoadConfig.Clear();
foreach (var sceneSetup in EditorSceneManager.GetSceneManagerSetup())
{
ChildScenesToLoadConfig.Add(AssetDatabase.LoadAssetAtPath<SceneAsset>(sceneSetup.path));
}
}

public void ResetSceneSetupToConfig()
{
var sceneAssetsToLoad = ChildScenesToLoadConfig;

List<SceneSetup> sceneSetupToLoad = new();
foreach (var sceneAsset in sceneAssetsToLoad)
{
sceneSetupToLoad.Add(new SceneSetup() { path = AssetDatabase.GetAssetPath(sceneAsset), isActive = false, isLoaded = true });
}

sceneSetupToLoad[0].isActive = true;
EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
EditorSceneManager.RestoreSceneManagerSetup(sceneSetupToLoad.ToArray());
}
#endif
}

#if UNITY_EDITOR
[CustomEditor(typeof(EditorChildSceneLoader))]
public class ChildSceneLoaderInspectorGUI : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();

var currentInspectorObject = (EditorChildSceneLoader)target;

if (GUILayout.Button("Save scene setup to config"))
{
currentInspectorObject.SaveSceneSetup();
}

if (GUILayout.Button("Reset scene setup from config..."))
{
currentInspectorObject.ResetSceneSetupToConfig();
}
}
}

[InitializeOnLoad]
public class ChildSceneLoader
{
static ChildSceneLoader()
{
EditorSceneManager.sceneOpened += OnSceneLoaded;
}

static void OnSceneLoaded(Scene _, OpenSceneMode mode)
{
if (mode != OpenSceneMode.Single || BuildPipeline.isBuildingPlayer) return; // try to load child scenes only for root scenes or if not building

var scenesToLoadObjects = GameObject.FindObjectsOfType<EditorChildSceneLoader>();
if (scenesToLoadObjects.Length > 1)
{
throw new Exception("Should only have one root scene at once loaded");
}

if (scenesToLoadObjects.Length == 0 || !scenesToLoadObjects[0].enabled) // only when we have a config and when that config is enabled
{
return;
}

scenesToLoadObjects[0].ResetSceneSetupToConfig();

Debug.Log("Setup done for root scene and child scenes");
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.