SettingsProvider 是一个配置类,用于指定如何在 Settings 或 Preferences 窗口中显示项目设置或偏好设置。
若要添加新的项目设置或偏好设置页面,请定义 SettingsProvider。SettingsProvider 类提供挂钩来显示任何 UI(使用 IMGUI 或 UIElements 来绘制它)。它还提供一个 API,让您以两种方式指定在 Settings 窗口和 Preferences 窗口中使用的关键字:
1) 搜索栏会过滤掉没有匹配关键字的 SettingsProviders。
2) 用匹配的关键字突出显示属性标签。
此示例将演示创建和配置不同 SettingsProviders 的多种方式:
using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEngine; using UnityEngine.UIElements;
// Create a new type of Settings Asset. class MyCustomSettings : ScriptableObject { public const string k_MyCustomSettingsPath = "Assets/Editor/MyCustomSettings.asset";
[SerializeField] private int m_Number;
[SerializeField] private string m_SomeString;
internal static MyCustomSettings GetOrCreateSettings() { var settings = AssetDatabase.LoadAssetAtPath<MyCustomSettings>(k_MyCustomSettingsPath); if (settings == null) { settings = ScriptableObject.CreateInstance<MyCustomSettings>(); settings.m_Number = 42; settings.m_SomeString = "The answer to the universe"; AssetDatabase.CreateAsset(settings, k_MyCustomSettingsPath); AssetDatabase.SaveAssets(); } return settings; }
internal static SerializedObject GetSerializedSettings() { return new SerializedObject(GetOrCreateSettings()); } }
// Register a SettingsProvider using IMGUI for the drawing framework: static class MyCustomSettingsIMGUIRegister { [SettingsProvider] public static SettingsProvider CreateMyCustomSettingsProvider() { // First parameter is the path in the Settings window. // Second parameter is the scope of this setting: it only appears in the Project Settings window. var provider = new SettingsProvider("Project/MyCustomIMGUISettings", SettingsScope.Project) { // By default the last token of the path is used as display name if no label is provided. label = "Custom IMGUI", // Create the SettingsProvider and initialize its drawing (IMGUI) function in place: guiHandler = (searchContext) => { var settings = MyCustomSettings.GetSerializedSettings(); EditorGUILayout.PropertyField(settings.FindProperty("m_Number"), new GUIContent("My Number")); EditorGUILayout.PropertyField(settings.FindProperty("m_SomeString"), new GUIContent("My String")); },
// Populate the search keywords to enable smart search filtering and label highlighting: keywords = new HashSet<string>(new[] { "Number", "Some String" }) };
return provider; } }
// Register a SettingsProvider using UIElements for the drawing framework: static class MyCustomSettingsUIElementsRegister { [SettingsProvider] public static SettingsProvider CreateMyCustomSettingsProvider() { // First parameter is the path in the Settings window. // Second parameter is the scope of this setting: it only appears in the Settings window for the Project scope. var provider = new SettingsProvider("Project/MyCustomUIElementsSettings", SettingsScope.Project) { label = "Custom UI Elements", // activateHandler is called when the user clicks on the Settings item in the Settings window. activateHandler = (searchContext, rootElement) => { var settings = MyCustomSettings.GetSerializedSettings();
// rootElement is a VisualElement. If you add any children to it, the OnGUI function // isn't called because the SettingsProvider uses the UIElements drawing framework. var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Editor/settings_ui.uss"); rootElement.styleSheets.Add(styleSheet); var title = new Label() { text = "Custom UI Elements" }; title.AddToClassList("title"); rootElement.Add(title);
var properties = new VisualElement() { style = { flexDirection = FlexDirection.Column } }; properties.AddToClassList("property-list"); rootElement.Add(properties);
var tf = new TextField() { value = settings.FindProperty("m_SomeString").stringValue }; tf.AddToClassList("property-value"); properties.Add(tf); },
// Populate the search keywords to enable smart search filtering and label highlighting: keywords = new HashSet<string>(new[] { "Number", "Some String" }) };
return provider; } }
// Create MyCustomSettingsProvider by deriving from SettingsProvider: class MyCustomSettingsProvider : SettingsProvider { private SerializedObject m_CustomSettings;
class Styles { public static GUIContent number = new GUIContent("My Number"); public static GUIContent someString = new GUIContent("Some string"); }
const string k_MyCustomSettingsPath = "Assets/Editor/MyCustomSettings.asset"; public MyCustomSettingsProvider(string path, SettingsScope scope = SettingsScope.User) : base(path, scope) {}
public static bool IsSettingsAvailable() { return File.Exists(k_MyCustomSettingsPath); }
public override void OnActivate(string searchContext, VisualElement rootElement) { // This function is called when the user clicks on the MyCustom element in the Settings window. m_CustomSettings = MyCustomSettings.GetSerializedSettings(); }
public override void OnGUI(string searchContext) { // Use IMGUI to display UI: EditorGUILayout.PropertyField(m_CustomSettings.FindProperty("m_Number"), Styles.number); EditorGUILayout.PropertyField(m_CustomSettings.FindProperty("m_SomeString"), Styles.someString); }
// Register the SettingsProvider [SettingsProvider] public static SettingsProvider CreateMyCustomSettingsProvider() { if (IsSettingsAvailable()) { var provider = new MyCustomSettingsProvider("Project/MyCustomSettingsProvider", SettingsScope.Project);
// Automatically extract all keywords from the Styles. provider.keywords = GetSearchKeywordsFromGUIContentProperties<Styles>(); return provider; }
// Settings Asset doesn't exist yet; no need to display anything in the Settings window. return null; } }
activateHandler | 重载 SettingsProvider.OnActivate。 |
deactivateHandler | 重载 SettingsProvider.OnDeactivate。 |
footerBarGuiHandler | 重载 SettingsProvider.OnFooterBarGUI。 |
guiHandler | 重载 SettingsProvider.OnGUI。 |
hasSearchInterestHandler | 重载 SettingsProvider.HasSearchInterest。 |
inspectorUpdateHandler | 重写 SettingsProvider.OnInspectorUpdate。 |
keywords | 获取或设置关键字列表,以便将这些关键字与用户正在搜索的内容进行比较。当用户在 Settings 窗口的搜索框中输入值时,SettingsProvider.HasSearchInterest 会尝试将这些关键字与此列表进行匹配。 |
label | 获取或设置 SettingsProvider 在 Settings 窗口中的显示名称。如果未设置,Settings 窗口将改用 SettingsProvider.settingsPath 的最后一个标记。 |
scope | 获取 SettingsProvider 的范围。该范围确定 SettingsProvider 显示在 Preferences 窗口 (SettingsScope.User) 中还是显示在 Settings 窗口 (SettingsScope.Project) 中。 |
settingsPath | 获取用于将 SettingsProvider 放置在 Settings 窗口的树视图中的路径。该路径在所有其他设置路径中应该具有唯一性,并且应该将“/”用作分隔符。 |
titleBarGuiHandler | 重载 SettingsProvider.OnTitleBarGUI。 |
SettingsProvider | 创建新的 SettingsProvider。 |
HasSearchInterest | 检查用户在 Settings 窗口搜索框中键入内容时是否应显示 SettingsProvider。SettingsProvider 尝试将搜索词(甚至搜索词一部分)与任何 SettingsProvider.keywords 匹配。该搜索不区分大小写。 |
OnActivate | 用户单击 Settings 窗口中的 Settings 时,使用此函数来实现处理程序。可以从此函数获取设置资源或设置 UIElements UI。 |
OnDeactivate | 用户单击另一设置或者 Settings 窗口关闭时,使用此函数来实现处理程序。 |
OnFooterBarGUI | 使用此函数通过 IMGUI 来重载 SettingsProvider 的页脚绘制。 |
OnGUI | 使用此函数基于 IMGUI 来绘制 UI。此情况下假设您没有向传递给 OnActivate 函数的 rootElement 添加任何子项。 |
OnInspectorUpdate | OnInspectorUpdate 以每秒 10 帧的速度调用,以便检视面板有机会进行更新。有关更多详细信息,请参阅 EditorWindow.OnInspectorUpdate。 |
OnTitleBarGUI | 使用此函数通过 IMGUI 来重载 SettingsProvider 的标题绘制。因此您可以在标题旁边添加自定义 UI(例如工具栏按钮)。AssetSettingsProvider 使用此机制来显示“add to preset”和“help”按钮。 |
Repaint | 请求 SettingsWindow 以进行重绘。 |
GetSearchKeywordsFromGUIContentProperties | 从特定类型的所有公共静态成员中提取搜索关键字。 |
GetSearchKeywordsFromPath | 从特定路径中资源的序列化属性提取搜索关键字。 |
GetSearchKeywordsFromSerializedObject | 从 SerializedObject 的序列化属性提取搜索关键字。 |