Files
2026-06-17 20:44:53 +03:00

85 lines
2.6 KiB
C#

#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
[InitializeOnLoad]
public static class SceneBootstrapLoader
{
private const string EnabledKey = "SceneBootstrapLoader.Enabled";
private const string BootstrapScenePathKey = "SceneBootstrapLoader.BootstrapScenePath";
private const string RequestedScenePathKey = "SceneBootstrapLoader.RequestedScenePath";
public static bool Enabled
{
get => EditorPrefs.GetBool(EnabledKey, false);
set => EditorPrefs.SetBool(EnabledKey, value);
}
public static string BootstrapScenePath
{
get => EditorPrefs.GetString(BootstrapScenePathKey, string.Empty);
set => EditorPrefs.SetString(BootstrapScenePathKey, value ?? string.Empty);
}
public static string RequestedScenePath
{
get => EditorPrefs.GetString(RequestedScenePathKey, string.Empty);
set => EditorPrefs.SetString(RequestedScenePathKey, value ?? string.Empty);
}
static SceneBootstrapLoader()
{
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}
private static void OnPlayModeStateChanged(PlayModeStateChange state)
{
if (!Enabled)
{
EditorSceneManager.playModeStartScene = null;
return;
}
switch (state)
{
case PlayModeStateChange.ExitingEditMode:
PreparePlayModeStartScene();
break;
case PlayModeStateChange.EnteredEditMode:
CleanupAfterPlayMode();
break;
}
}
private static void PreparePlayModeStartScene()
{
string bootstrapPath = BootstrapScenePath;
if (string.IsNullOrWhiteSpace(bootstrapPath))
{
Debug.LogWarning("SceneBootstrapLoader: Bootstrap scene is not set.");
EditorSceneManager.playModeStartScene = null;
return;
}
var bootstrapScene = AssetDatabase.LoadAssetAtPath<SceneAsset>(bootstrapPath);
if (bootstrapScene == null)
{
Debug.LogWarning($"SceneBootstrapLoader: Bootstrap scene not found at path: {bootstrapPath}");
EditorSceneManager.playModeStartScene = null;
return;
}
string currentScenePath = UnityEngine.SceneManagement.SceneManager.GetActiveScene().path;
RequestedScenePath = currentScenePath;
EditorSceneManager.playModeStartScene = bootstrapScene;
}
private static void CleanupAfterPlayMode()
{
EditorSceneManager.playModeStartScene = null;
}
}
#endif