SettingsProvider.OnActivate

Unity · Версия 2020.3 LTS · Scripting · SettingsProvider

SettingsProvider.OnActivate

Объявление

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

Параметры

searchContext Контекст поиска в поле поиска в окне настроек.
rootElement Корень дерева UIElements. Если вы добавите к этому корню, SettingsProvider использует UIElements вместо вызова SettingsProvider.OnGUI для создания пользовательского интерфейса. Если вы не добавляете к этому VisualElement, вы должны использовать IMGUI для создания пользовательского интерфейса..

Описание

Используйте эту функцию для реализации обработчика, когда пользователь нажимает кнопку "Настройки" в окне "Настройки". Вы можете получить актив настроек или настроить пользовательский интерфейс UIElements из этой функции.

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(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();
    }
}

В этом примере показано, как создать SettingsProvider на основе UIElements: вам нужно добавить любые дочерние элементы в rootElement, переданный в OnActivate.

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(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.
    }
}

Обновлено 17 ноября 2026 г.