How to correctly listen to changes in a IMGUIContainer with UIToolkit

Everything work as intended when I am drawing my scriptable object inside an EditorWindow with a VisualTreeAsset, but I don’t know how to listen to changes inside an IMGUIContainer. Or maybe another approach is better, anything will help thank you :slight_smile:


		{
			_levelInspector = new InspectorElement(_selectedLevel)
			{
				dataSourceType = typeof(Level)
			};
			
			rootVisualElement.Add(_levelInspector);
			
			// ...
			
			_levelInspector.Bind(new SerializedObject(_selectedLevel));
			
			_imguiLevelContainer = _levelInspector.Q<IMGUIContainer>();
			
			_imguiLevelContainer.onGUIHandler = () =>
			{
				EditorGUI.BeginChangeCheck();
				{
					// how can I draw the inspector of imguiLevelContainer to listen to changes
				}

				if (EditorGUI.EndChangeCheck())
				{
					_levelListView.Rebuild();
					Debug.Log("Did change");
				}
			};
	}

Edit : You can listen to changes with TrackSerializedObjectValue so any changes in the IMGUIContainer will trigger a change event :

SerializedObject so = new SerializedObject(_myObject);
			
_imguiContainer.Bind(so);
			
_imguiContainer.TrackSerializedObjectValue(so, (serializedObject) =>
{
	Debug.Log("On changed");
});

It also work in you do it in the InspectorElement that contain the IMGUIContainer:

_inspectorElement.TrackSerializedObjectValue(so, (serializedObject) =>
{
	Debug.Log("On changed");
});
1 Like