Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

Question Custom Property Drawer with Delete Command & Bound ListView (UI Elements & UI Toolkit)

Discussion in 'Editor & General Support' started by neroid, Nov 13, 2023.

  1. neroid


    Jul 24, 2015
    I have an editor script that has a listview im binding to my property (which is a list of a custom class).

    My CustomClass.cs in this example is a simple class extending nothing and has a Custom Property Drawer which also adds button that when pressed issues the serializedProperty.DeleteCommand().

    Now my problem is how do i make my ListView Rebuild when an element is either added or deleted?

    the event SerializedPropertyChangedEvent always fires once at the start so its not reliable for my use case since this is meant to only fire on Add or Delete, but neither are done from the editor and are all done externally.

    Let me know how i can approach this since its a weird problem to tackle

    Here is an example of the classes simplified:
    Code (CSharp):
    1. [CustomEditor(typeof(MyClass))]
    2. public class MyClassEditor: Editor
    3. {
    4.     public VisualTreeAsset UXML;
    5.     public override VisualElement CreateInspectorGUI()
    6.     {
    7.         var root = new VisualElement();
    8.         UXML.CloneTree(root);
    9.         var list = root.Q<ListView>("Property-ListView");
    10.         list.BindProperty(serializedObject.FindProperty("MyList"));
    11.         //?? How do we rebuild once the property is updated?
    12.         return root;
    13.     }
    15.     //This is the initial attempt i did
    16.     void UpdateListView(SerializedPropertyChangeEvent evt, ListView thisList)
    17.     {
    18.         thisList.Rebuild();
    19.     }
    20.     //Doesnt work cause it recursively rebuilds the list until unity crashes for some reason.
    21. }
    23. [CustomPropertyDrawer(typeof(MyCustomClass))]
    24. public class MyCustomClassDrawer: PropertyDrawer
    25. {
    26.     public override VisualElement CreatePropertyGUI(SerializedProperty property)
    27.     {
    28.         var root = new VisualElement();
    29.         var deleteButtonExample = new Button();
    30.         deleteButtonExample.userData = property;
    31.         deleteButtonExample.RegisterCallback<ClickEvent>(DeleteThis);
    32.         root.Add(deleteButtonExample);
    33.         return root;
    34.     }
    36.     void DeleteThis(ClickEvent evt)
    37.     {
    38.         SerializedProperty property = (SerializedProperty)((Button);
    39.         SerializedObject obj = property.serializedObject;
    40.         property.DeleteCommand();
    41.         obj.ApplyModifiedProperties();
    42.     }
    43. }
    Last edited: Nov 13, 2023
    chriseborn likes this.