Skip to content

Instantly share code, notes, and snippets.

@stash
Created April 1, 2018 23:57
Show Gist options
  • Save stash/ddf301fe1be08957e71793926d047bcf to your computer and use it in GitHub Desktop.
Save stash/ddf301fe1be08957e71793926d047bcf to your computer and use it in GitHub Desktop.
AutoLoadGlobalScene - automatically and additively load a global scene in the Unity editor
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