Updating Binding of InspectorElement doesn't get repainted in Editor

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?

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
using VolatileBytes.eol.Entity;


public class CharacterCreatorWindow : EditorWindow
{
    private CharacterDatabase _characterDatabase;
    private AssetReference _assetReference;
    private CharacterEntity _current => _characterDatabase.entities[_currentIndex];
    private int _currentIndex = 0;
    private InspectorElement _entityInspector;


    [MenuItem("Window/UIElements/CharacterCreatorWindow")]
    public static void ShowExample()
    {
        CharacterCreatorWindow wnd = GetWindow<CharacterCreatorWindow>();
        wnd.titleContent = new GUIContent("CharacterCreatorWindow");
    }

    public void OnEnable()
    {
        _assetReference = AssetDatabase.LoadAssetAtPath<AssetReference>
            ("Assets/VolatileBytes/eol/Data/AssetReference.asset");
        _characterDatabase = _assetReference.CharacterDatabase;
        // Each editor window contains a root VisualElement object
        VisualElement root = rootVisualElement;

        // Import UXML
        var visualTree =
            AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
                "Assets/VolatileBytes/eol/Editor/CharacterCreator/CharacterCreatorWindow.uxml");
        var styleSheet =
            AssetDatabase.LoadAssetAtPath<StyleSheet>(
                "Assets/VolatileBytes/eol/Editor/CharacterCreator/CharacterCreatorWindow.uss");
        VisualElement view = visualTree.CloneTree();
        view.styleSheets.Add(styleSheet);
        var ve = view.Query<VisualElement>("character-entity-drawer").First();
        _entityInspector = new InspectorElement(_current);
        ve.Add(_entityInspector);
        RegisterEventHandlers(view);
        root.Add(view);
    }

    public void RegisterEventHandlers(VisualElement view)
    {
        var btnNext = view.Query<Button>("btn-next").First();
        var btnPrev = view.Query<Button>("btn-prev").First();
        btnNext.clicked += NextEntity;
        btnPrev.clicked += PrevEntity;
    }

    private void NextEntity()
    {
        if (++_currentIndex >= _characterDatabase.entities.Count)
        {
            _currentIndex = 0;
        }

        _entityInspector.Unbind();
        _entityInspector.Bind(new SerializedObject(_current));
        _entityInspector.MarkDirtyRepaint();
        Debug.Log("Bind data:" + _current.name);
    }

    private void PrevEntity()
    {
        if (--_currentIndex < 0)
        {
            _currentIndex = _characterDatabase.entities.Count - 1;
        }

        _entityInspector.Unbind();
        _entityInspector.Bind(new SerializedObject(_current));
        _entityInspector.MarkDirtyRepaint();
    }
}
2 Likes

Hi lijinglue,

This looks like a bug, can you report it with the bug reporter so we can investigate? (Help → Report a bug…)

Bug Reported (Case 1299036)

Link to final result in the IssueTracker Unity Issue Tracker - InspectorElement does not get updated when using Bind()

I believe it’s still reproducible with Unity 2021.2.0b2.3053 (built-in UI Toolkit) .