From 8962e344a368ba0232a411157ab27bfe81ac8af2 Mon Sep 17 00:00:00 2001 From: Christophe SAUVEUR Date: Wed, 30 Jun 2021 16:48:16 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=BD=20Migrated=20PreferenceItem=20to?= =?UTF-8?q?=20SettingsProvider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/AutoScene.cs | 129 +++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 61 deletions(-) diff --git a/Editor/AutoScene.cs b/Editor/AutoScene.cs index b756bfe..437b5b3 100644 --- a/Editor/AutoScene.cs +++ b/Editor/AutoScene.cs @@ -1,6 +1,7 @@ using UnityEditor; using UnityEditor.SceneManagement; using System; +using System.Collections.Generic; namespace chsxf { @@ -58,77 +59,83 @@ private static bool CanEnableAutoScene() { return !settings.Enabled; } - [PreferenceItem("AutoScene")] - public static void AutoScenePreferences() { + [SettingsProvider] + public static SettingsProvider AutoSceneSettingsProvider() { + SettingsProvider provider = new SettingsProvider("AutoScene", SettingsScope.User) { + keywords = new HashSet(new[] { "scene", "autoscene", "play mode" }), - EditorGUILayout.LabelField("Version", AutoSceneSettings.VERSION, EditorStyles.boldLabel); - EditorGUILayout.Space(); + guiHandler = (searchContext) => { + EditorGUILayout.LabelField("Version", AutoSceneSettings.VERSION, EditorStyles.boldLabel); + EditorGUILayout.Space(); - // Build scene list - string[] sceneGuids = AssetDatabase.FindAssets("t:Scene"); - string[] scenePathes = new string[sceneGuids.Length]; - for (int i = 0; i < sceneGuids.Length; i++) { - scenePathes[i] = AssetDatabase.GUIDToAssetPath(sceneGuids[i]); - } - Array.Sort(scenePathes, string.Compare); + // Build scene list + string[] sceneGuids = AssetDatabase.FindAssets("t:Scene"); + string[] scenePathes = new string[sceneGuids.Length]; + for (int i = 0; i < sceneGuids.Length; i++) { + scenePathes[i] = AssetDatabase.GUIDToAssetPath(sceneGuids[i]); + } + Array.Sort(scenePathes, string.Compare); - // Finding selected index - string prefsValue = settings.LoadedScene; - int selectedIndex = 0; - if (prefsValue == "auto") { - selectedIndex = 1; - } - else { - int arrayIndex = Array.IndexOf(scenePathes, prefsValue); - if (arrayIndex >= 0) { - selectedIndex = arrayIndex + 2; - } - } + // Finding selected index + string prefsValue = settings.LoadedScene; + int selectedIndex = 0; + if (prefsValue == "auto") { + selectedIndex = 1; + } + else { + int arrayIndex = Array.IndexOf(scenePathes, prefsValue); + if (arrayIndex >= 0) { + selectedIndex = arrayIndex + 2; + } + } - string[] menuEntries = new string[scenePathes.Length + 2]; - menuEntries[0] = "None"; - menuEntries[1] = "Auto"; - Array.Copy(scenePathes, 0, menuEntries, 2, scenePathes.Length); + string[] menuEntries = new string[scenePathes.Length + 2]; + menuEntries[0] = "None"; + menuEntries[1] = "Auto"; + Array.Copy(scenePathes, 0, menuEntries, 2, scenePathes.Length); - EditorGUI.BeginChangeCheck(); - - bool enabled = settings.Enabled; - enabled = EditorGUILayout.Toggle("Enable AutoScene", enabled); - EditorGUILayout.Space(); + EditorGUI.BeginChangeCheck(); - selectedIndex = EditorGUILayout.Popup("Scene to load on Play", selectedIndex, menuEntries); + bool enabled = settings.Enabled; + enabled = EditorGUILayout.Toggle("Enable AutoScene", enabled); + EditorGUILayout.Space(); - if (EditorGUI.EndChangeCheck()) { - if (selectedIndex == 0) { - prefsValue = "none"; - } - else if (selectedIndex == 1) { - prefsValue = "auto"; - } - else { - prefsValue = menuEntries[selectedIndex]; - } + selectedIndex = EditorGUILayout.Popup("Scene to load on Play", selectedIndex, menuEntries); - settings.LoadedScene = prefsValue; - settings.Enabled = enabled; - UpdatePlayModeStartScene(); - } + if (EditorGUI.EndChangeCheck()) { + if (selectedIndex == 0) { + prefsValue = "none"; + } + else if (selectedIndex == 1) { + prefsValue = "auto"; + } + else { + prefsValue = menuEntries[selectedIndex]; + } - string helpBoxMessage; - if (selectedIndex == 0) { - helpBoxMessage = "The scenes currently loaded in the editor will be maintained when entering Play mode.\n\nThis is the default Unity behaviour."; - } - else if (selectedIndex == 1) { - helpBoxMessage = "The first enabled scene in the Build Settings box will be loaded when entering Play mode. If no such scene exists, falls back to 'None'."; - } - else { - helpBoxMessage = string.Format("The scene '{0}' will be loaded when entring Play mode. If the scene does not exist anymore, falls back to 'None'.", prefsValue); - } - EditorGUILayout.Space(); - EditorGUILayout.HelpBox(helpBoxMessage, MessageType.Info, true); + settings.LoadedScene = prefsValue; + settings.Enabled = enabled; + UpdatePlayModeStartScene(); + } + + string helpBoxMessage; + if (selectedIndex == 0) { + helpBoxMessage = "The scenes currently loaded in the editor will be maintained when entering Play mode.\n\nThis is the default Unity behaviour."; + } + else if (selectedIndex == 1) { + helpBoxMessage = "The first enabled scene in the Build Settings box will be loaded when entering Play mode. If no such scene exists, falls back to 'None'."; + } + else { + helpBoxMessage = string.Format("The scene '{0}' will be loaded when entring Play mode. If the scene does not exist anymore, falls back to 'None'.", prefsValue); + } + EditorGUILayout.Space(); + EditorGUILayout.HelpBox(helpBoxMessage, MessageType.Info, true); - EditorGUILayout.Space(); - EditorGUILayout.LabelField("Made with ❤️ by chsxf"); + EditorGUILayout.Space(); + EditorGUILayout.LabelField("Made with ❤️ by chsxf"); + } + }; + return provider; } } } \ No newline at end of file