Init
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
#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
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 14654515991486d4790469be18a9123b
|
||||
@@ -0,0 +1,47 @@
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
public class SceneBootstrapLoaderWindow : EditorWindow
|
||||
{
|
||||
[MenuItem("Tools/Scenes/Bootstrap Loader")]
|
||||
public static void ShowWindow()
|
||||
{
|
||||
var window = GetWindow<SceneBootstrapLoaderWindow>("Bootstrap Loader");
|
||||
window.minSize = new Vector2(420, 110);
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
GUILayout.Space(8);
|
||||
|
||||
SceneBootstrapLoader.Enabled = EditorGUILayout.Toggle(
|
||||
new GUIContent("Enabled", "Always start Play Mode from bootstrap scene"),
|
||||
SceneBootstrapLoader.Enabled
|
||||
);
|
||||
|
||||
var currentAsset = string.IsNullOrWhiteSpace(SceneBootstrapLoader.BootstrapScenePath)
|
||||
? null
|
||||
: AssetDatabase.LoadAssetAtPath<SceneAsset>(SceneBootstrapLoader.BootstrapScenePath);
|
||||
|
||||
var newAsset = (SceneAsset)EditorGUILayout.ObjectField(
|
||||
new GUIContent("Bootstrap Scene", "Scene used as playModeStartScene"),
|
||||
currentAsset,
|
||||
typeof(SceneAsset),
|
||||
false
|
||||
);
|
||||
|
||||
string newPath = newAsset != null ? AssetDatabase.GetAssetPath(newAsset) : string.Empty;
|
||||
if (newPath != SceneBootstrapLoader.BootstrapScenePath)
|
||||
SceneBootstrapLoader.BootstrapScenePath = newPath;
|
||||
|
||||
GUILayout.Space(8);
|
||||
|
||||
EditorGUILayout.HelpBox(
|
||||
"When enabled, Play Mode always starts from the selected bootstrap scene. " +
|
||||
"The path of the scene you launched from is saved and can be loaded by bootstrap at runtime.",
|
||||
MessageType.Info
|
||||
);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3525cdb25bffe9f44abd3dd1a7e44aec
|
||||
Reference in New Issue
Block a user