UxmlSerializedDataPropertyDrawer
The base class for custom property drawers applied to UxmlSerializedData nested classes.
읽는 시간 4분최근 업데이트: 9일 전
UxmlSerializedDataPropertyDrawerUxmlSerializedDataUxmlAttributeUse instead of when creating a custom drawer for a type that's a or a . For plain types that aren't types, use directly.
UxmlSerializedDataPropertyDrawerPropertyDrawerUxmlObjectUxmlElement[Serializable]UxmlObjectPropertyDrawerApply a custom drawer
Apply the attribute to your drawer class and target the nested class of the custom control:
[CustomPropertyDrawer]UxmlSerializedDatausing Unity.UIToolkit.Editor;using UnityEditor;using UnityEngine.UIElements;[CustomPropertyDrawer(typeof(MyCustomControl.UxmlSerializedData))]public class MyCustomControlDrawer : UxmlSerializedDataPropertyDrawer{ // The base class generates the full inspector UI automatically.}
Override points
Override one or both of the following methods to customize the generated UI.
CreateChildPropertiesGUI
Override to control which properties are shown and in what order. The base implementation generates a field for every property that isn't marked with .
CreateChildPropertiesGUI[UxmlAttribute][HideInInspector]To show or hide attributes by name, call or instead of iterating the properties yourself. Both take UXML attribute names and keep the default order and property drawers for everything they create:
CreateChildPropertiesIncludingCreateChildPropertiesExcluding// Override CreateChildPropertiesGUI to display only specific properties.[CustomPropertyDrawer(typeof(SelectiveButton.UxmlSerializedData))]public class SelectiveButtonDrawer : UxmlSerializedDataPropertyDrawer{ protected override void CreateChildPropertiesGUI(VisualElement container, SerializedProperty property) { CreateChildPropertiesIncluding(container, property, "color", "text"); }}
CreateChildPropertyGUI
Override to customize how a single property is rendered. The base implementation calls this method for each property it encounters and creates a for it.
CreateChildPropertyGUI[UxmlAttribute]UxmlAttributeFieldUse this override when a property needs a layout that's more complex than a single field — for example, pairing a field with action buttons. Wrap any field in a to retain override and binding affordances. For properties that don't need custom rendering, call to fall back to the default .
IBindableUxmlAttributeFieldDecoratorbase.CreateChildPropertyGUIUxmlAttributeFieldThe following example targets an element with two string properties. The property uses a multiline with a Clear button, and the property uses a single-line with an Open button that launches the URL in the system browser:
InfoPaneltextTextFielddocumentationUrlTextField// Override CreateChildPropertyGUI to render specific properties with custom layouts.//// This example targets an InfoPanel element with two string [UxmlAttribute] properties:// - "text": a freeform description rendered as a multiline TextField with a Clear button.// - "documentationUrl": a URL rendered as a single-line TextField with an Open button.//// Both fields use a UxmlAttributeFieldDecorator so they retain override and binding affordances.// All other properties fall back to the default UxmlAttributeField rendering via the base class.[CustomPropertyDrawer(typeof(InfoPanel.UxmlSerializedData))]public class InfoPanelDrawer : UxmlSerializedDataPropertyDrawer{ protected override void CreateChildPropertyGUI(VisualElement container, SerializedProperty property, SerializedProperty childProperty) { switch (childProperty.name) { case "text": container.Add(BuildMultilineFieldWithClear(property, childProperty)); break; case "documentationUrl": container.Add(BuildUrlFieldWithOpen(property, childProperty)); break; default: base.CreateChildPropertyGUI(container, property, childProperty); break; } } // Builds a row with a multiline TextField inside a UxmlAttributeFieldDecorator and a Clear button. static VisualElement BuildMultilineFieldWithClear(SerializedProperty property, SerializedProperty childProperty) { // Cache the path and the object — both properties are references that may be recycled after // this method returns, so the button callback must not touch either one. string propertyPath = childProperty.propertyPath; SerializedObject serializedObject = property.serializedObject; VisualElement row = new VisualElement(); row.style.flexDirection = FlexDirection.Row; UxmlAttributeFieldDecorator decorator = new UxmlAttributeFieldDecorator(); TextField textField = new TextField(childProperty.displayName); textField.bindingPath = propertyPath; textField.multiline = true; textField.style.flexGrow = 1; decorator.Add(textField); row.Add(decorator); row.Add(new Button(() => { SerializedProperty serializedProperty = serializedObject.FindProperty(propertyPath); if (serializedProperty != null) { serializedProperty.stringValue = string.Empty; serializedObject.ApplyModifiedProperties(); } }) { text = "Clear" }); return row; } // Builds a row with a single-line TextField inside a UxmlAttributeFieldDecorator and an Open button. static VisualElement BuildUrlFieldWithOpen(SerializedProperty property, SerializedProperty childProperty) { string propertyPath = childProperty.propertyPath; SerializedObject serializedObject = property.serializedObject; VisualElement row = new VisualElement(); row.style.flexDirection = FlexDirection.Row; UxmlAttributeFieldDecorator decorator = new UxmlAttributeFieldDecorator(); TextField textField = new TextField(childProperty.displayName); textField.bindingPath = propertyPath; textField.style.flexGrow = 1; decorator.Add(textField); row.Add(decorator); row.Add(new Button(() => { SerializedProperty serializedProperty = serializedObject.FindProperty(propertyPath); if (serializedProperty != null && !string.IsNullOrEmpty(serializedProperty.stringValue)) Application.OpenURL(serializedProperty.stringValue); }) { text = "Open" }); return row; }}
For a complete working example, refer to Create a custom inventory property drawer.