Search Unity

Bug Problems with updating values in Inspector

Discussion in 'UI Toolkit' started by imaewyn, Aug 2, 2019.

  1. imaewyn

    imaewyn

    Joined:
    Apr 23, 2016
    Posts:
    211
    ui elements not update inspector

    1. What happened
    Changes in inspector do not update.

    2. How we can reproduce it using the example you attached
    Create scriptable object with array.
    Create C# script for customeditor
    Draw list from scriptable object
    Create button for update arraysize, or label for catch values from list.
    Input values in field
    For example:
    in list i have string field "Name" for naming of elements in array
    after I enter the text in the Name field, then Element 0 is replaced by this text.

    what expect:
    array size or label catch changes from field in inspector in realtime
    what's happening:
    changes do not occur until the file is rediscovered in the inspector

    Code (CSharp):
    1.  
    2.  
    3. [CustomEditor(typeof(List_SO))]
    4. public class SO_UI : Editor
    5. {
    6.     SerializedProperty _listProperty;
    7.     public override VisualElement CreateInspectorGUI()
    8.     {
    9.         serializedObject.Update();
    10.         _listProperty = serializedObject.FindProperty("firstLists");
    11.         var btn1 = new Button(() => Test());
    12.      
    13.         var root = new VisualElement();
    14.         root.Add(btn1);
    15.         for (int i = 0; i < _listProperty.arraySize; i++)
    16.         {
    17.             SerializedProperty property = _listProperty.GetArrayElementAtIndex(i);
    18.             root.Add(new PropertyField(property));
    19.         }
    20.         serializedObject.ApplyModifiedProperties();
    21.         return root;
    22.     }
    23.     public void Test()
    24.     {
    25.         Debug.Log("new void test");
    26.         _listProperty.arraySize += 1;
    27.     }
    28. }
    29.  
    30.  
     
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    When using UIElements to create your inspector, CreateInspectorGUI() is called once once to create it, then the created ui is bound to the serialized object. You can create your custom fields and assign their bindingPath value, or register callbacks perform custom logic when the value changes.

    In your case, the PropertyField already takes case of creating child elements then the size of the array changes, so you can bind to the listProperty directly. In 19.3, you'll also be able to bind and array property to a ListView.

    Also, when changing a property on a SerializedObject, you need to call ApplyModifiedProperties() to save them back to the actual scene object.

    Here is a modified version of your script:

    Code (CSharp):
    1. [CustomEditor(typeof(List_SO))]
    2. public class SO_UI : Editor
    3. {
    4.     SerializedProperty _listProperty;
    5.  
    6.     public override VisualElement CreateInspectorGUI()
    7.     {
    8.         _listProperty = serializedObject.FindProperty("firstLists");
    9.  
    10.         var root = new VisualElement();
    11.         var b = new Button(() => Test());
    12.         b.text = "Add";
    13.  
    14.         root.Add(b);
    15.         root.Add(new PropertyField(_listProperty));
    16.         return root;
    17.     }
    18.     public void Test()
    19.     {
    20.         _listProperty.arraySize += 1;
    21.         _listProperty.serializedObject.ApplyModifiedProperties();
    22.     }
    23. }
     
    Reekdeb likes this.
  3. imaewyn

    imaewyn

    Joined:
    Apr 23, 2016
    Posts:
    211
    Perhaps I did not accurately describe my problems. I think if I show this on the video, it will become clearer what exactly isn’t. By the way, adding a ApplyModifiedProperties has no effect. I tried your code variation, but it still doesn't help me. In the 19.3 version, all this works the same as my current implementation at IMGUI, even with my last variation, but at the moment I do not have the ability to transfer the project to this version and therefore I would like to find some temporary solution.

    On left side 19.3 version, on right side is my current 19.1


     
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Hello,

    Please submit a bug with the attached script so our QA team can track it.

    Thanks !
     
  5. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    One more question:
    So in UIElements, Only need invoke 'SerializedObject.ApplyModifiedProperties'. No need for 'SerializedObject.Update'