From 5f65b80b8dc7d203ec428ca88199ca240f06274f Mon Sep 17 00:00:00 2001 From: Hubert Mattusch Date: Tue, 20 Dec 2022 03:13:04 +0100 Subject: [PATCH 1/2] Add tools --- AutoSceneChanger.cs | 13 +++ AutoSceneChanger.cs.meta | 11 ++ Editor/ComponentsAdditionalItems.meta | 8 ++ .../RectTransformSetBasedOnImage.cs | 22 ++++ .../RectTransformSetBasedOnImage.cs.meta | 11 ++ Editor/ToolsWindowBase.cs | 107 ++++++++++++++++++ Editor/ToolsWindowBase.cs.meta | 3 + 7 files changed, 175 insertions(+) create mode 100644 AutoSceneChanger.cs create mode 100644 AutoSceneChanger.cs.meta create mode 100644 Editor/ComponentsAdditionalItems.meta create mode 100644 Editor/ComponentsAdditionalItems/RectTransformSetBasedOnImage.cs create mode 100644 Editor/ComponentsAdditionalItems/RectTransformSetBasedOnImage.cs.meta create mode 100644 Editor/ToolsWindowBase.cs create mode 100644 Editor/ToolsWindowBase.cs.meta diff --git a/AutoSceneChanger.cs b/AutoSceneChanger.cs new file mode 100644 index 0000000..c7b60ea --- /dev/null +++ b/AutoSceneChanger.cs @@ -0,0 +1,13 @@ +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace NEG.Utils +{ + public class AutoSceneChanger : MonoBehaviour + { +#if !UNITY_EDITOR + private void Start() => SceneManager.LoadScene(1); +#endif + } +} + diff --git a/AutoSceneChanger.cs.meta b/AutoSceneChanger.cs.meta new file mode 100644 index 0000000..c7db448 --- /dev/null +++ b/AutoSceneChanger.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eac2f9a681087504998bbeb40cd8516a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/ComponentsAdditionalItems.meta b/Editor/ComponentsAdditionalItems.meta new file mode 100644 index 0000000..3129799 --- /dev/null +++ b/Editor/ComponentsAdditionalItems.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d683d64cc04efcf479b41c84b51cf7fe +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/ComponentsAdditionalItems/RectTransformSetBasedOnImage.cs b/Editor/ComponentsAdditionalItems/RectTransformSetBasedOnImage.cs new file mode 100644 index 0000000..f1f1691 --- /dev/null +++ b/Editor/ComponentsAdditionalItems/RectTransformSetBasedOnImage.cs @@ -0,0 +1,22 @@ +using UnityEditor; +using UnityEngine; +using UnityEngine.UI; + +namespace NEG.Utils.Editor.ComponentsAdditionalItems +{ + public static class RectTransformSetBasedOnImage + { + [MenuItem("CONTEXT/RectTransform/Set as inside of Image", false, 2000)] + public static void SetFillBasedOnImage(MenuCommand command) + { + var transform = (RectTransform)command.context; + if (!transform.TryGetComponent(out Image image)) + return; + + transform.anchorMin = Vector2.zero; + transform.anchorMax = Vector2.one; + transform.offsetMin = new Vector2(-image.sprite.border.x, -image.sprite.border.y); + transform.offsetMax = new Vector2(image.sprite.border.z, image.sprite.border.w); + } + } +} \ No newline at end of file diff --git a/Editor/ComponentsAdditionalItems/RectTransformSetBasedOnImage.cs.meta b/Editor/ComponentsAdditionalItems/RectTransformSetBasedOnImage.cs.meta new file mode 100644 index 0000000..b1a832b --- /dev/null +++ b/Editor/ComponentsAdditionalItems/RectTransformSetBasedOnImage.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 614a58a9665e7cf4182834f1fb3c0096 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/ToolsWindowBase.cs b/Editor/ToolsWindowBase.cs new file mode 100644 index 0000000..64c8bdf --- /dev/null +++ b/Editor/ToolsWindowBase.cs @@ -0,0 +1,107 @@ +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; + } + + } + } +} \ No newline at end of file diff --git a/Editor/ToolsWindowBase.cs.meta b/Editor/ToolsWindowBase.cs.meta new file mode 100644 index 0000000..407b91d --- /dev/null +++ b/Editor/ToolsWindowBase.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 76faf7fadf7c40d88dec1a8ee4da5cb6 +timeCreated: 1671500708 \ No newline at end of file From 9791cf70f71e497402efd8d5884ac88f8f96586a Mon Sep 17 00:00:00 2001 From: Hubert Mattusch Date: Tue, 20 Dec 2022 23:38:18 +0100 Subject: [PATCH 2/2] Add save when exiting scene --- Editor/ToolsWindowBase.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Editor/ToolsWindowBase.cs b/Editor/ToolsWindowBase.cs index 64c8bdf..d200732 100644 --- a/Editor/ToolsWindowBase.cs +++ b/Editor/ToolsWindowBase.cs @@ -77,6 +77,7 @@ namespace NegUtils.Editor { if(!EditorPrefs.GetBool("StartFromSceneIndex0")) return; + EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo(); EditorPrefs.SetString("LastOpenedScenePath", EditorSceneManager.GetSceneManagerSetup()[0].path); EditorSceneManager.OpenScene(EditorBuildSettings.scenes[0].path); }