140 lines
5.8 KiB
C#
140 lines
5.8 KiB
C#
using UnityEditor;
|
|
using UnityEditor.SceneManagement;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace Unity.BossRoom.Editor
|
|
{
|
|
/// <summary>
|
|
/// Class that permits auto-loading a bootstrap scene when the editor switches play state. This class is
|
|
/// initialized when Unity is opened and when scripts are recompiled. This is to be able to subscribe to
|
|
/// EditorApplication's playModeStateChanged event, which is when we wish to open a new scene.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A critical edge case scenario regarding NetworkManager is accounted for here.
|
|
/// A NetworkObject's GlobalObjectIdHash value is currently generated in OnValidate() which is invoked during a
|
|
/// build and when the asset is loaded/viewed in the editor.
|
|
/// If we were to manually open Bootstrap scene via EditorSceneManager.OpenScene(...) as the editor is exiting play
|
|
/// mode, Bootstrap scene would be entering play mode within the editor prior to having loaded any assets, meaning
|
|
/// NetworkManager itself has no entry within the AssetDatabase cache. As a result of this, any referenced Network
|
|
/// Prefabs wouldn't have any entry either.
|
|
/// To account for this necessary AssetDatabase step, whenever we're redirecting from a new scene, or a scene
|
|
/// existing in our EditorBuildSettings, we forcefully stop the editor, open Bootstrap scene, and re-enter play
|
|
/// mode. This provides the editor the chance to create AssetDatabase cache entries for the Network Prefabs assigned
|
|
/// to the NetworkManager.
|
|
/// If we are entering play mode directly from Bootstrap scene, no additional steps need to be taken and the scene
|
|
/// is loaded normally.
|
|
/// </remarks>
|
|
[InitializeOnLoad]
|
|
public class SceneBootstrapper
|
|
{
|
|
const string k_PreviousSceneKey = "PreviousScene";
|
|
const string k_ShouldLoadBootstrapSceneKey = "LoadMainMenuScene";
|
|
|
|
const string k_LoadBootstrapSceneOnPlay = "FirstSceneLoad/Load MainMenu Scene On Play";
|
|
const string k_DoNotLoadBootstrapSceneOnPlay = "FirstSceneLoad/Don't Load MainMenu Scene On Play";
|
|
|
|
const string k_TestRunnerSceneName = "InitTestScene";
|
|
|
|
static bool s_RestartingToSwitchScene;
|
|
|
|
static string BootstrapScene => EditorBuildSettings.scenes[0].path;
|
|
|
|
static string PreviousScene
|
|
{
|
|
get => EditorPrefs.GetString(k_PreviousSceneKey);
|
|
set => EditorPrefs.SetString(k_PreviousSceneKey, value);
|
|
}
|
|
|
|
static bool ShouldLoadBootstrapScene
|
|
{
|
|
get
|
|
{
|
|
if (!EditorPrefs.HasKey(k_ShouldLoadBootstrapSceneKey))
|
|
{
|
|
EditorPrefs.SetBool(k_ShouldLoadBootstrapSceneKey, true);
|
|
}
|
|
return EditorPrefs.GetBool(k_ShouldLoadBootstrapSceneKey, true);
|
|
}
|
|
set => EditorPrefs.SetBool(k_ShouldLoadBootstrapSceneKey, value);
|
|
}
|
|
|
|
static SceneBootstrapper()
|
|
{
|
|
EditorApplication.playModeStateChanged += EditorApplicationOnplayModeStateChanged;
|
|
}
|
|
|
|
[MenuItem(k_LoadBootstrapSceneOnPlay, true)]
|
|
static bool ShowLoadBootstrapSceneOnPlay() => !ShouldLoadBootstrapScene;
|
|
|
|
[MenuItem(k_LoadBootstrapSceneOnPlay)]
|
|
static void EnableLoadBootstrapSceneOnPlay() => ShouldLoadBootstrapScene = true;
|
|
|
|
[MenuItem(k_DoNotLoadBootstrapSceneOnPlay, true)]
|
|
static bool ShowDoNotLoadBootstrapSceneOnPlay() => ShouldLoadBootstrapScene;
|
|
|
|
[MenuItem(k_DoNotLoadBootstrapSceneOnPlay)]
|
|
static void DisableDoNotLoadBootstrapSceneOnPlay() => ShouldLoadBootstrapScene = false;
|
|
|
|
static void EditorApplicationOnplayModeStateChanged(PlayModeStateChange playModeStateChange)
|
|
{
|
|
if (IsTestRunnerActive() || !ShouldLoadBootstrapScene)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (s_RestartingToSwitchScene)
|
|
{
|
|
if (playModeStateChange == PlayModeStateChange.EnteredPlayMode)
|
|
{
|
|
s_RestartingToSwitchScene = false;
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (playModeStateChange == PlayModeStateChange.ExitingEditMode)
|
|
{
|
|
PreviousScene = EditorSceneManager.GetActiveScene().path;
|
|
|
|
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
|
|
{
|
|
if (!string.IsNullOrEmpty(BootstrapScene) &&
|
|
System.Array.Exists(EditorBuildSettings.scenes, scene => scene.path == BootstrapScene))
|
|
{
|
|
s_RestartingToSwitchScene = true;
|
|
EditorApplication.isPlaying = false;
|
|
|
|
// Đóng tất cả các scene đang mở
|
|
for (int i = SceneManager.sceneCount - 1; i >= 0; i--)
|
|
{
|
|
Scene scene = SceneManager.GetSceneAt(i);
|
|
if (scene.isLoaded)
|
|
{
|
|
EditorSceneManager.CloseScene(scene, true);
|
|
}
|
|
}
|
|
|
|
// Mở scene đầu tiên trong danh sách build
|
|
EditorSceneManager.OpenScene(BootstrapScene);
|
|
|
|
EditorApplication.isPlaying = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
EditorApplication.isPlaying = false;
|
|
}
|
|
}
|
|
else if (playModeStateChange == PlayModeStateChange.EnteredEditMode && !string.IsNullOrEmpty(PreviousScene))
|
|
{
|
|
EditorSceneManager.OpenScene(PreviousScene);
|
|
}
|
|
}
|
|
|
|
static bool IsTestRunnerActive()
|
|
{
|
|
return EditorSceneManager.GetActiveScene().name.StartsWith(k_TestRunnerSceneName);
|
|
}
|
|
}
|
|
}
|