Neg_Utils/Editor/ToolsWindowBase.cs

115 lines
4.2 KiB
C#

using System;
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace NegUtils.Editor
{
[InitializeOnLoad]
public class ToolsWindowBase : EditorWindow
{
static ToolsWindowBase()
{
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}
[MenuItem("Tools/Show Tools Window")]
private static void ShowWindow()
{
var window = GetWindow<ToolsWindowBase>();
window.Show();
}
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);
}
}
private static void ShowScenesList(Rect position)
{
var menu = new GenericMenu();
string path = Application.dataPath + "/Scenes/Production";
string[] fileInfo = Directory.GetFiles(path, "*.unity");
foreach (string item in fileInfo)
{
string s = item;
menu.AddItem(new GUIContent(s.Remove(0, path.Length + 1).Remove(s.Length - path.Length - 7 ,6)), false, () => {
LoadScene(s);
});
menu.AddSeparator("");
}
menu.DropDown(position);
}
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;
}
}
}
}