Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Updating Binding of InspectorElement doesn't get repainted in Editor

Discussion in 'UI Toolkit' started by lijinglue, Dec 30, 2019.

  1. lijinglue

    lijinglue

    Joined:
    Mar 9, 2016
    Posts:
    1
    I'm trying to go through a list of scriptable objects by updating the object that the Inspector Element bound to. However, the inspector doesn't get updated or repainted in the editor after I invoke Inspector.Bind. Does it suppose to work this way? am I missing sth here?

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.UIElements;
    4. using UnityEditor.UIElements;
    5. using VolatileBytes.eol.Entity;
    6.  
    7.  
    8. public class CharacterCreatorWindow : EditorWindow
    9. {
    10.     private CharacterDatabase _characterDatabase;
    11.     private AssetReference _assetReference;
    12.     private CharacterEntity _current => _characterDatabase.entities[_currentIndex];
    13.     private int _currentIndex = 0;
    14.     private InspectorElement _entityInspector;
    15.  
    16.  
    17.     [MenuItem("Window/UIElements/CharacterCreatorWindow")]
    18.     public static void ShowExample()
    19.     {
    20.         CharacterCreatorWindow wnd = GetWindow<CharacterCreatorWindow>();
    21.         wnd.titleContent = new GUIContent("CharacterCreatorWindow");
    22.     }
    23.  
    24.     public void OnEnable()
    25.     {
    26.         _assetReference = AssetDatabase.LoadAssetAtPath<AssetReference>
    27.             ("Assets/VolatileBytes/eol/Data/AssetReference.asset");
    28.         _characterDatabase = _assetReference.CharacterDatabase;
    29.         // Each editor window contains a root VisualElement object
    30.         VisualElement root = rootVisualElement;
    31.  
    32.         // Import UXML
    33.         var visualTree =
    34.             AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
    35.                 "Assets/VolatileBytes/eol/Editor/CharacterCreator/CharacterCreatorWindow.uxml");
    36.         var styleSheet =
    37.             AssetDatabase.LoadAssetAtPath<StyleSheet>(
    38.                 "Assets/VolatileBytes/eol/Editor/CharacterCreator/CharacterCreatorWindow.uss");
    39.         VisualElement view = visualTree.CloneTree();
    40.         view.styleSheets.Add(styleSheet);
    41.         var ve = view.Query<VisualElement>("character-entity-drawer").First();
    42.         _entityInspector = new InspectorElement(_current);
    43.         ve.Add(_entityInspector);
    44.         RegisterEventHandlers(view);
    45.         root.Add(view);
    46.     }
    47.  
    48.     public void RegisterEventHandlers(VisualElement view)
    49.     {
    50.         var btnNext = view.Query<Button>("btn-next").First();
    51.         var btnPrev = view.Query<Button>("btn-prev").First();
    52.         btnNext.clicked += NextEntity;
    53.         btnPrev.clicked += PrevEntity;
    54.     }
    55.  
    56.     private void NextEntity()
    57.     {
    58.         if (++_currentIndex >= _characterDatabase.entities.Count)
    59.         {
    60.             _currentIndex = 0;
    61.         }
    62.  
    63.         _entityInspector.Unbind();
    64.         _entityInspector.Bind(new SerializedObject(_current));
    65.         _entityInspector.MarkDirtyRepaint();
    66.         Debug.Log("Bind data:" + _current.name);
    67.     }
    68.  
    69.     private void PrevEntity()
    70.     {
    71.         if (--_currentIndex < 0)
    72.         {
    73.             _currentIndex = _characterDatabase.entities.Count - 1;
    74.         }
    75.  
    76.         _entityInspector.Unbind();
    77.         _entityInspector.Bind(new SerializedObject(_current));
    78.         _entityInspector.MarkDirtyRepaint();
    79.     }
    80. }
     
    a_p_u_r_o and Djayp like this.
  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Hi lijinglue,

    This looks like a bug, can you report it with the bug reporter so we can investigate? (Help -> Report a bug...)
     
  3. Djayp

    Djayp

    Joined:
    Feb 16, 2015
    Posts:
    114
  4. a_p_u_r_o

    a_p_u_r_o

    Joined:
    May 27, 2015
    Posts:
    20