Files
chameleon/Assets/Scripts/Editor/PlaySceneWindow.cs
T
2026-07-10 13:26:04 +02:00

224 lines
7.7 KiB
C#

using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Chameleon.Editor
{
[ExecuteAlways]
public class PlaySceneWindow : EditorWindow
{
[MenuItem("Tools/Play Scene")]
public static void Open()
=> _ = GetWindow<PlaySceneWindow>("Play Scene");
[SerializeField] bool enabled = true;
[SerializeField] string lastActiveScene;
[SerializeField] List<string> lastScenePaths = new();
[SerializeField] string scenePath;
[SerializeField] int sceneIndex;
static List<(string path, SceneCollection collection)> sceneCollections;
static string[] scenePaths;
static string[] sceneNames;
static string activeScenePath
=> SceneManager.GetActiveScene().path;
void OnEnable()
{
OnDisable();
RefreshSceneCollections();
scenePaths = EditorBuildSettings.scenes.Select(x => x.path).ToArray();
sceneNames = scenePaths.Select(Path.GetFileNameWithoutExtension).ToArray();
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}
void RefreshSceneCollections()
{
sceneCollections = AssetDatabase.FindAssets($"t:{nameof(SceneCollection)}")
.Select(AssetDatabase.GUIDToAssetPath)
.Select(AssetDatabase.LoadAssetAtPath<SceneCollection>)
.Where(x => x)
.Select(x => (
string.Join(";", x.sceneAssets.Select(AssetDatabase.GetAssetPath)),
x
))
.ToList();
}
void OnDisable()
{
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
}
void OnPlayModeStateChanged(PlayModeStateChange state)
{
if (!enabled)
return;
if (state != PlayModeStateChange.ExitingEditMode)
return;
if (!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
EditorApplication.ExitPlaymode();
lastActiveScene = SceneManager.GetActiveScene().path;
lastScenePaths = Enumerable
.Range(0, SceneManager.sceneCount)
.Select(i => SceneManager.GetSceneAt(i).path)
.ToList();
scenePath = scenePaths[sceneIndex];
EditorSceneManager.OpenScene(scenePath);
}
void Update()
{
if (!enabled)
return;
if (EditorApplication.isPlaying)
return;
if (lastScenePaths.Count == 0)
return;
if (scenePath is not null)
return;
var scene = lastScenePaths.First();
if (scene == string.Empty)
return;
EditorSceneManager.OpenScene(scene);
foreach (var scenePath in lastScenePaths.Skip(1))
EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
if (!string.IsNullOrEmpty(lastActiveScene))
{
var activeScene = SceneManager.GetSceneByPath(lastActiveScene);
if (activeScene.IsValid())
SceneManager.SetActiveScene(activeScene);
lastActiveScene = string.Empty;
}
lastScenePaths.Clear();
}
static GUIStyle blockStyle1;
static GUIStyle blockStyle2;
static GUIStyle buttonStyle;
void OnGUI()
{
blockStyle1 ??= new() { padding = new(6, 6, 6, 3) };
blockStyle2 ??= new() { padding = new(6, 6, 3, 6) };
buttonStyle ??= new(GUI.skin.button) { padding = new(3, 3, 1, 1) };
System.Action action = null;
var isPlaying = EditorApplication.isPlaying;
GUI.FocusControl(null);
GUILayout.BeginHorizontal(blockStyle1);
{
GUI.enabled = !isPlaying;
var path = string.Join(";", Enumerable
.Range(0, SceneManager.sceneCount)
.Select(i => SceneManager.GetSceneAt(i).path)
);
var options = sceneCollections.Select(x => x.collection.name);
var index = sceneCollections.FindIndex(x => x.path == path);
if (index == -1)
{
index = 0;
options = options.Prepend("(New Scene Collection)");
var newIndex = EditorGUILayout.Popup(index, options.ToArray());
if (newIndex != index)
sceneCollections[newIndex - 1].collection.Open();
if (GUILayout.Button("Create", buttonStyle, GUILayout.ExpandWidth(false)))
{
var sceneCollection = CreateInstance<SceneCollection>();
sceneCollection.sceneAssets.AddRange(
Enumerable
.Range(0, SceneManager.sceneCount)
.Select(i => SceneManager.GetSceneAt(i).path)
.Select(AssetDatabase.LoadAssetAtPath<SceneAsset>)
);
var assetPath = AssetDatabase.GenerateUniqueAssetPath($"Assets/Untitled Scene Collection.asset");
AssetDatabase.CreateAsset(sceneCollection, assetPath);
AssetDatabase.Refresh();
RefreshSceneCollections();
}
}
else
{
var newIndex = EditorGUILayout.Popup(index, options.ToArray());
if (newIndex != index)
sceneCollections[newIndex].collection.Open();
}
if (GUILayout.Button(EditorGUIUtility.IconContent("d_Refresh"), buttonStyle, GUILayout.ExpandWidth(false)))
{
action = OnEnable;
}
}
GUILayout.EndHorizontal();
if (enabled)
{
if (isPlaying)
{
GUI.color = new(.5f, 1, .5f);
GUI.enabled = false;
if (scenePath is not null && scenePath == activeScenePath)
scenePath = null;
}
else
{
if (enabled)
GUI.color = new(.75f, 1, .75f);
if (scenePath == activeScenePath)
EditorApplication.EnterPlaymode();
}
}
GUILayout.BeginHorizontal(blockStyle2);
{
enabled = EditorGUILayout.Toggle(enabled, GUILayout.Width(16));
GUI.enabled = enabled && !isPlaying;
sceneIndex = EditorGUILayout.Popup(sceneIndex, sceneNames);
GUI.enabled = !isPlaying;
var disabled = !EditorSettings.enterPlayModeOptions.HasFlag(EnterPlayModeOptions.DisableDomainReload);
GUI.color = disabled ? new(1, .75f, .5f) : new(.825f, .825f, .825f);
disabled = EditorGUILayout.Toggle(disabled, GUILayout.Width(14));
EditorGUILayout.LabelField("Reload Domain", GUILayout.Width(86));
EditorSettings.enterPlayModeOptionsEnabled = !disabled;
EditorSettings.enterPlayModeOptions = disabled
? EnterPlayModeOptions.None
: (EnterPlayModeOptions.DisableDomainReload | EnterPlayModeOptions.DisableSceneReload);
}
GUILayout.EndHorizontal();
action?.Invoke();
}
}
}