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(); 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) { bool goToCurrentScene = EditorPrefs.GetBool("GoToCurrentSceneAfterPlay"); newVal = GUILayout.Toggle(goToCurrentScene, "Go to current scene after play"); if (newVal != goToCurrentScene) { EditorPrefs.SetBool("GoToCurrentSceneAfterPlay", 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; 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 SceneManager.LoadScene(1); } break; case PlayModeStateChange.EnteredEditMode: { if(!EditorPrefs.GetBool("StartFromSceneIndex0")) return; EditorSceneManager.OpenScene(EditorPrefs.GetString("LastOpenedScenePath")); } break; } } } }