-
Notifications
You must be signed in to change notification settings - Fork 557
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
07c8bdb
Initial script that works.
SamuelBellomo df409a4
adding some useful buttons
SamuelBellomo b815a0a
null ref fix
SamuelBellomo edea06a
better button names
SamuelBellomo bddf7f7
adding changelog entry
SamuelBellomo 1052c93
Fixes for builds and better name for editor script
SamuelBellomo f78867b
more fixes for build
SamuelBellomo a60bd13
Update Packages/com.unity.multiplayer.samples.coop/Utilities/EditorCh…
SamuelBellomo 35a1bd6
Merge develop into sam/feat/child-scene-loader
netcode-ci-service 03d5525
Merge develop into sam/feat/child-scene-loader
netcode-ci-service File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Git LFS file not shown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
Packages/com.unity.multiplayer.samples.coop/Utilities/EditorChildSceneLoader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
SamuelBellomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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>(); | ||
SamuelBellomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 |
11 changes: 11 additions & 0 deletions
11
Packages/com.unity.multiplayer.samples.coop/Utilities/EditorChildSceneLoader.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.