Unity 6.3
0 онлайн 75 гостей 3 в системе
Вход

SettingsProvider.OnActivate

Declaration

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

Параметры

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

Описание

Используйте эту функцию для реализации обработчика, когда пользователь нажимает на кнопку Настройки в окне Настройки. Вы можете получить asset 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(); } }

Этот пример показывает, как построить 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<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. } }