ChangeEvent on InspectorElement not working?

I’m trying to get a callback when any control changed its value inside of the InspectorElement. It’s really nice and convenient to just call new InspectorElement(target) and have all properties automatically drawn, but I also want to know when any of these fields have changed, so I can do some validation and disable a button for example.

In IMGUI I used to call EditorGUI.Begin- and EndChangeCheck, but in UI Toolkit I believe the correct way would be the ChangeEvent. However, when I use inspector.RegisterCallback<ChangeEvent<string>>(evt => Debug.Log("Change: " + evt.newValue)); to listen for any TextField change, it never gets called.

I have same problem. Did you solve this problem?

ObjectField currentObjectField = new ObjectField("ChangeTalkDataAsset");
currentObjectField.objectType = typeof(TalkData);
currentObjectField.value = currentTalkCtrl.stateGroup[idexItem].assetTalkData;    currentObjectField.RegisterCallback<ChangeEvent<TalkData>>(evt = {currentTalkCtrl.stateGroup[idexItem].assetTalkData = evt.newValue;});

When I change this objectField value, it can’t change in inspector. How can I solve this problem?

Are you sure? On the left you have the 3213 state id, on the right, in the inspector you have the 323232 state id opened. I guess you aren’t looking at the same thing?

Sorry, there was an error in the image capture just now. But,When TextField use RegisterCallback<ChangeEvent>, it can work. ObjectField not work.Is it because I used a custom class? But,TalkData is inherited from ScriptableObject.

You can use the UI Toolkit Event Debugger (Window > UI Toolkit > Event Debugger) to see all the events that occurred in your particular panel, and see if you’re using the proper signature to catch them. Most of the time, missing ChangeEvents are actually events that are fired with a more abstract type than one would expect, a good thing to keep in mind.

For example, ChangeEvent<UnityEngine.Object> might be called instead of ChangeEvent despite the fact that the ObjectField targets only TalkData objects, since ObjectField is not a generic type in of itself, so it stays conservative in what events it sends.

I hope this helps!

1 Like

Thank you for your reply.When I change ChangeEvent to ChangeEvent<UnityEngine.Object>, it can work.
Do you know any way to draw UnityEvent in my particular panel? I did not find the corresponding UIElement in UIbuilder.

@uBenoitA What version do I need for the event debugger? I only see UI Builder, Debugger, and Samples under Window > UI Toolkit. The Dbeugger option opens “UI Toolkit Debugger” which I use to check layouts.

Unity 2021.1.1.7f1 with UI Builder and UI Toolkit preview 14 packages installed.