Editor Scripts: Change Check

So when using EditorGUI.BeginChangeCheck() and EditorGUI.EndChangeCheck(), this will return true if any portion of the value changes.

For example, if I have a float field, and I start typing in the values:

3
36
365

A change flag rises for each digit typed.

What if I want to know about when the user types in the last digit? For instance if they exit the field, or hits enter. In WinForms for example this is the OnValidate event for a textfield.

Because lets say I have a FloatField that allows you to type in values, but I clamp the range to say 150 → 300. Thing is, the 215, though in range, will be clamped on the first digit 2!

I could track for even the ‘control’ is focused, and when it leaves focus, manually. Though this doesn’t always work as I expect it to, especially when using EditorGUI.MultiFloatField.

Anyone have any ideas?

bump?

There is an OnValidate method on MonoBehaviour you can use to clamp values and the like. But it’s not exactly what you are after.

Maybe something hacky with checking Event.current?

Event.current is always turning up used.

I might have to hook into some other event… ugh.

Not really what you’re looking for… but what about building up an event on a timer? Trigger it once 2-3 seconds after EndChangeCheck() returns true and if the number changes within that window, just reset the delay window and only trigger once in another 2-3 seconds.

I don’t entirely understand BeginChangeCheck and EndChangeCheck. Do you wrap your controls in these guys? In the update method maybe? The documentation is terrible…

How about checking GUI.GetNameOfFocusedControl during the repaint event? You could store a local variable with the name of the focused control. If the name changes then you know the user has selected a different element, and validation code can run.

The other option is an else on the GUI.EndChangeCheck. Maybe set a dirty flag inside of the EndChangeCheck, and run validation code if EndChangeCheck returns false and the dirty flag is true? Again you are probably looking to catch this in the repaint event.

Welcome to OnGUI. The system is event based and entirely code generated. It’s also full of Unity’s magic methods. It’s a completely different workflow to everything else you do in Unity.

I tried GUI.GetNameOfFocusedControl.

But you have to set the name before hand. And when I call MultiFloatField, it doesn’t set the name of either of the fields, and instead the label, so it only tracks if I click on or off the freaking label. UGH.

I’m probably just going to have to write my own version of MultiFloatField that tracks each individual field independently and return a flag for which is focused.