Search Unity

InspectorElement refreshing and label binding

Discussion in 'UI Toolkit' started by cemleme, May 8, 2019.

  1. cemleme

    cemleme

    Joined:
    Mar 24, 2017
    Posts:
    30
    Hello,

    I am trying to build a dialogue system using the UIElements. please bare with me because I didn't use the old system much and I am not very familiar with the serializedProperties as well.

    I have created a list by using rows of VisualElements. it is a very simple ui.
    Screen Shot 2019-05-08 at 18.50.09.png

    I have a Dialogue scriptableobject which simply keeps the sentence and next dialogues

    Code (CSharp):
    1. public class Dialogue : ScriptableObject
    2. {
    3.     public bool waitForChoice;
    4.  
    5.     [TextArea(3, 10)]
    6.     public string sentence;
    7.  
    8.     public List<Dialogue> answers;
    9.     public Dialogue nextDialogue;
    10. }
    then I included the events for MouseDownEvent on these visual elements. so I can select a row
    I keep the selected dialogue and the label (dialogue sentece text on the rows)

    Code (CSharp):
    1.  
    2.     Dialogue selectedDialogue;
    3.     Label selectedDialogueLabel;
    Code (CSharp):
    1.    void  OnSelectRow()
    2.     {
    3.             selectedDialogue = dialogue;
    4.             selectedDialogueLabel = dialogueText;
    5.  
    6.             row.AddToClassList("selected-row");
    7.  
    8.             ShowSelectedDialogueInspector();
    9.     }

    then I update the dialogue InspectorElement on selection

    Code (CSharp):
    1.     void ShowSelectedDialogueInspector()
    2.     {
    3.         if (selectedDialogue != null)
    4.         {
    5.             if(root.Contains(selectedDialogueInspector))
    6.                 root.Remove(selectedDialogueInspector);
    7.  
    8.             selectedDialogueInspector = new InspectorElement();
    9.             var so = new SerializedObject(selectedDialogue);
    10.             selectedDialogueInspector.Bind(so);
    11.             selectedDialogueLabel.Bind(so);
    12.  
    13.             root.Add(selectedDialogueInspector);
    14.         }
    15.  
    16.     }

    so here are my questions:

    1) as you can see I remove the previous InspectorElement and create a new one for the new selectedDialogue; because I couldn't update inspectorElement when I instantiated it on OnEnable or CreateInspectorGUI . Is there a way to update the same InspectorElement with changing the serializedObject on each new selection?
    If so what am I doing completely wrong?

    2) although the above code is terrible, it shows the correct inspectorElement as it recreates. Afterwards I want to update the selected row's label when I change the text on inspector element.

    selectedDialogueLabel keeps the reference for that Label element.

    So I tried binding the serializedObject to it but couldnt succeed.

    Can someone show me the way?

    Thanks
     
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    1) You can bind the new SerializedObject to the old inspector. (also for future reference, you can easily remove an element with VisualElement.RemoveFromHierarchy )
    2) Unfortunately, Label is not a bindable element for now. Coming in 19.3, you'll be able to set the bindable path of the label and it's text value will be automatically updated when bound to a SerializedObject. For now, you'll have to do:
     selectedDialogueLabel.text = selectedDialogue.sentence;