版本:Unity 6(6000.0)
语言English
  • C#

SettingsProvider.OnActivate

建议更改

成功!

感谢帮助我们提升 Unity 文档的质量。虽然我们无法接受所有投稿,但我们确实会阅读用户提出的每一项建议更改,并在适用情况下进行更新。

关闭

提交失败

由于某些原因,无法提交您建议的更改。请在几分钟后重新尝试。感谢您花时间帮助我们提升 Unity 文档的质量。

关闭

取消

声明

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

参数

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

说明

使用此函数来实现处理程序,以便当用户点击设置窗口中的设置时,便可执行该处理程序。你可以从此函数来获取设置资源或设置 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. } }