Created
February 11, 2021 14:17
-
-
Save karljj1/7d114f6c87e367299e6cca4f50db86a9 to your computer and use it in GitHub Desktop.
Initializes the localization settings before entering into a scene in play mode
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 UnityEditor; | |
using UnityEditor.SceneManagement; | |
using UnityEngine.Localization.Settings; | |
using UnityEngine.SceneManagement; | |
[InitializeOnLoad] | |
public static class EditorBootScene | |
{ | |
static EditorBootScene() | |
{ | |
EditorApplication.playModeStateChanged += EditorApplication_playModeStateChanged; | |
} | |
/// <summary> | |
/// The scene needs to be in the build settings so if its not we will add it and remove it again later on. | |
/// </summary> | |
/// <param name="scene"></param> | |
/// <returns></returns> | |
static bool AddSceneToBuildSettings(Scene scene) | |
{ | |
var scenes = EditorBuildSettings.scenes; | |
for (int i = 0; i < scenes.Length; ++i) | |
{ | |
var s = EditorBuildSettings.scenes[i]; | |
if (s.path == scene.path) | |
return false; | |
} | |
ArrayUtility.Add(ref scenes, new EditorBuildSettingsScene(scene.path, true)); | |
EditorBuildSettings.scenes = scenes; | |
return true; | |
} | |
/// <summary> | |
/// Remove the scene from the build settings | |
/// </summary> | |
/// <param name="path"></param> | |
static void RemoveScene(string path) | |
{ | |
var scenes = EditorBuildSettings.scenes; | |
for (int i = 0; i < scenes.Length; ++i) | |
{ | |
var s = EditorBuildSettings.scenes[i]; | |
if (s.path == path) | |
{ | |
ArrayUtility.RemoveAt(ref scenes, i); | |
EditorBuildSettings.scenes = scenes; | |
return; | |
} | |
} | |
} | |
static void EditorApplication_playModeStateChanged(PlayModeStateChange obj) | |
{ | |
// Entering play mode so load the boot scene | |
if (obj == PlayModeStateChange.ExitingEditMode) | |
{ | |
if (!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) | |
return; | |
// Record the current scene so we can go back to it once we have booted. | |
var scene = EditorSceneManager.GetActiveScene(); | |
SessionState.SetString("current-scene", scene.path); | |
SessionState.SetBool("remove-scene", AddSceneToBuildSettings(scene)); | |
// Create an empty scene to initialize in | |
var newScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); | |
} | |
else if (obj == PlayModeStateChange.EnteredPlayMode) | |
{ | |
// Wait for initialization and then switch to the scene | |
if (LocalizationSettings.InitializationOperation.IsDone) | |
LoadScene(); | |
else | |
LocalizationSettings.InitializationOperation.Completed += (_) => LoadScene(); | |
} | |
else if (obj == PlayModeStateChange.EnteredEditMode) | |
{ | |
// When entering edit mode revert back to how things were previously. | |
var path = SessionState.GetString("current-scene", null); | |
EditorSceneManager.OpenScene(path); | |
if (SessionState.GetBool("remove-scene", false)) | |
RemoveScene(path); | |
} | |
} | |
static void LoadScene() | |
{ | |
var path = SessionState.GetString("current-scene", null); | |
EditorSceneManager.LoadScene(path); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment