Created
April 1, 2018 23:57
-
-
Save stash/ddf301fe1be08957e71793926d047bcf to your computer and use it in GitHub Desktop.
AutoLoadGlobalScene - automatically and additively load a global scene in the Unity editor
This file contains 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
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
using UnityEditor; | |
using UnityEditor.SceneManagement; | |
using System; | |
// Automatically load additively a "global" scene when the user opens a scene in the Editor | |
// Just put this file in your project and EDIT the two settings below! | |
[InitializeOnLoad] | |
public class AutoLoadGlobalScene { | |
// EDIT these two strings to match your project: | |
// 1. The name of your global scene: | |
public const string globalSceneName = "_Global"; | |
// 2. The path to your global scene, relative to the project root: | |
public const string globalScenePath = "Assets/_Scenes/_Global.unity"; | |
static AutoLoadGlobalScene() { | |
EditorSceneManager.sceneOpened += LoadGlobalSceneIfNeeded; | |
} | |
static void LoadGlobalSceneIfNeeded(Scene scene, OpenSceneMode mode) { | |
if (mode == OpenSceneMode.Single && scene.name != globalSceneName) { | |
Scene global = EditorSceneManager.GetSceneByPath(globalScenePath); | |
if (!global.isLoaded) { | |
global = EditorSceneManager.OpenScene(globalScenePath, OpenSceneMode.Additive); | |
} | |
EditorSceneManager.SetActiveScene(scene); | |
EditorSceneManager.MoveSceneBefore(global, scene); // make top of Editor hierarchy | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment