Neg_Utils/Editor/ToolsWindowBase.cs
2025-03-03 19:17:05 +01:00

123 lines
4.7 KiB
C#

using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace NegUtils.Editor
{
[InitializeOnLoad]
public class ToolsWindowBase : EditorWindow
{
private const int UnitySceneExtensionLength = 6;
static ToolsWindowBase()
{
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}
protected virtual void OnGUI()
{
if (GUILayout.Button("Select Scene"))
ShowScenesList(GUILayoutUtility.GetLastRect());
bool startFromSceneIndex0 = EditorPrefs.GetBool("StartFromSceneIndex0");
bool newVal = GUILayout.Toggle(startFromSceneIndex0, "Start from scene with index 0 on start");
if (newVal != startFromSceneIndex0) EditorPrefs.SetBool("StartFromSceneIndex0", newVal);
if (!startFromSceneIndex0)
return;
bool goToCurrentScene = EditorPrefs.GetBool("GoToCurrentSceneAfterPlay");
newVal = GUILayout.Toggle(goToCurrentScene, "Go to current scene after play");
if (newVal != goToCurrentScene) EditorPrefs.SetBool("GoToCurrentSceneAfterPlay", newVal);
bool goToFirstScene = EditorPrefs.GetBool("GoToFirstSceneAfterPlay");
newVal = GUILayout.Toggle(goToFirstScene, "Go to scene with index 1 after play");
if (newVal != goToFirstScene) EditorPrefs.SetBool("GoToFirstSceneAfterPlay", newVal);
}
[MenuItem("Tools/Show Tools Window")]
private static void ShowWindow()
{
var window = GetWindow<ToolsWindowBase>();
window.Show();
}
private static void ShowScenesList(Rect position)
{
var menu = new GenericMenu();
string path = Application.dataPath + "/Scenes";
AddFiles(path, path, menu);
menu.DropDown(position);
}
private static void AddFiles(string path, string basePath, GenericMenu menu)
{
string[] fileInfo = Directory.GetFiles(path, "*.unity");
for (int i = 0; i < fileInfo.Length; i++)
{
string s = fileInfo[i];
menu.AddItem(
new GUIContent(s.Remove(0, basePath.Length + 1)
.Remove(s.Length - basePath.Length - UnitySceneExtensionLength - 1, UnitySceneExtensionLength)
.Replace('\\', '/')), false, () =>
{
LoadScene(s);
});
if (i == fileInfo.Length) continue;
menu.AddSeparator("");
}
string[] dirInfo = Directory.GetDirectories(path);
foreach (string dir in dirInfo) AddFiles(dir, basePath, menu);
}
private static void LoadScene(string path)
{
EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
EditorSceneManager.OpenScene(path, OpenSceneMode.Single);
}
private static void OnPlayModeStateChanged(PlayModeStateChange state)
{
switch (state)
{
case PlayModeStateChange.ExitingEditMode:
{
if (!EditorPrefs.GetBool("StartFromSceneIndex0"))
return;
EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
EditorPrefs.SetString("LastOpenedScenePath", EditorSceneManager.GetSceneManagerSetup()[0].path);
EditorSceneManager.OpenScene(EditorBuildSettings.scenes[0].path);
}
break;
case PlayModeStateChange.EnteredPlayMode:
{
if (!EditorPrefs.GetBool("StartFromSceneIndex0"))
return;
if (EditorPrefs.GetBool("GoToCurrentSceneAfterPlay"))
{
EditorSceneManager.LoadSceneInPlayMode(EditorPrefs.GetString("LastOpenedScenePath"),
new LoadSceneParameters(LoadSceneMode.Single));
}
else if (EditorPrefs.GetBool("GoToFirstSceneAfterPlay"))
SceneManager.LoadScene(1);
}
break;
case PlayModeStateChange.EnteredEditMode:
{
if (!EditorPrefs.GetBool("StartFromSceneIndex0"))
return;
EditorSceneManager.OpenScene(EditorPrefs.GetString("LastOpenedScenePath"));
}
break;
}
}
}
}