Version: 2023.1
public void OnActivate (string searchContext, UIElements.VisualElement rootElement);

参数

searchContext Settings 窗口的搜索框中的搜索上下文。
rootElement UIElements 树的根。如果添加到此根,则 SettingsProvider 使用 UIElements 而不是调用 SettingsProvider.OnGUI 来构建 UI。如果不添加到此 VisualElement,则必须使用 IMGUI 来构建 UI。

描述

用户单击 Settings 窗口中的 Settings 时,使用此函数来实现处理程序。可以从此函数获取设置资源或设置 UIElements UI。

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

class SimpleIMGUISettingsProvider : SettingsProvider { SerializedObject m_Settings; const string k_MyCustomSettingsPath = "Assets/Editor/MyCustomSettings.asset"; public SimpleIMGUISettingsProvider(string path, SettingsScope scope = SettingsScope.User) : base(path, scope) {}

public override void OnActivate(string searchContext, VisualElement rootElement) { // Called when the user clicks on the MyCustom element in the Settings window m_Settings = new SerializedObject(AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(k_MyCustomSettingsPath)); }

public override void OnDeactivate() { // User selected another setting or closed the Settings window m_Settings = null; }

public override void OnGUI(string searchContext) { // Use IMGUI to display UI: EditorGUILayout.PropertyField(m_Settings.FindProperty("m_Number"), new GUIContent("My Number")); EditorGUILayout.PropertyField(m_Settings.FindProperty("m_SomeString"), new GUIContent("Some string")); m_Settings.ApplyModifiedPropertiesWithoutUndo(); } }

该示例显示如何基于 UIElements 来构建 SettingsProvider:需要将任何子项添加到已传递到 OnActivate 的 rootElement。

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;

class SimpleUIElementsSettingsProvider : SettingsProvider { SerializedObject m_Settings; const string k_MyCustomSettingsPath = "Assets/Editor/MyCustomSettings.asset"; public SimpleUIElementsSettingsProvider(string path, SettingsScope scope = SettingsScope.User) : base(path, scope) {}

public override void OnActivate(string searchContext, VisualElement rootElement) { // Called when the user clicks on the MyCustom element in the Settings window m_Settings = new SerializedObject(AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(k_MyCustomSettingsPath));

// rootElement is a VisualElement. If you add any children to it, you are using UIElements to build the SettingsProvider 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);

rootElement.Add(new PropertyField(m_Settings.FindProperty("m_SomeString"))); rootElement.Add(new PropertyField(m_Settings.FindProperty("m_Number")));

rootElement.Bind(m_Settings); }

public override void OnGUI(string searchContext) { // This function is never called since UIElements is drawing the UI. } }