I currently have my own data model (C# classes) which sends events for each property change which I use to sync data between the model and the UI toolkit view. I also register to the ChangeEvent of each UI toolkit element to apply UI changes back to the model. Finally, the data model sends “anything changed” so that the UI can refresh a preview panel. This works but requires a lot of boilerplate event code just to sync a dozen properties.
To reduce the boilerplate code I wanted to change to the SerializedObject binding system, however I ran into an issue. The data members are nicely synced with the UI controls, but I have no way of detecting “anything changed”. With IMGUI it was possible to use EditorGUI.BeginChangeCheck and EndChangeCheck in the OnGUI loop, but with UI Toolkit I would expect SerializedObject to have an event for the same thing, but apparently it doesn’t exist.
Is there a way to still do what I want with UI Toolkit? I would be ok with polling every x seconds to check if something has changed, but even that doesn’t seem possible, because the SerializedObject dirty state is internal and gets applied by the individual controls.
In theory I might be able to register a change event to the root VisualElement, but that would only be a hack, because I really want to refresh the preview when my model updates, not when the specific UI control is used, since I might add different methods of editing the model later (custom inspector or other wizards).