I’m interested in taking action (for example, playing a sound) programmatically when a button’s state changes - regardless of how it changed (mouse, keyboard, gamepad, etc.)
For example, I want to play a sound when a button is highlighted, pressed, released, etc.
It seems following a Selectable.currentSelectionState is the way to go, but I can’t hook Selectable.UpdateSelectionState(). Do I have to poll for states and look for a change?
I can do much of this work at a “higher level” by hooking OnSelect, OnPointerDown, etc. etc, but that’s a lot of redundant code for doing something that seems easy at a lower level.
Any thoughts here?
EDIT: Thinking on what I said I don’t think it’s what your looking for so here’s a revised answer:
You could subclass Button and override DoStateTransition, and then just call base.DoStateTransition… So maybe something like this:
public class StateHookableButton : Button
{
protected override void DoStateTransition(SelectionState state, bool instant)
{
base.DoStateTransition(state, instant);
// Do what you need here.
}
}
Note: The call to base.DoStateTransition is only needed if you have a non-none transition, if you want to handle the transition effects entirely yourself you don’t need the call.
I saw your previous post re: using the Animator and yeah, while I think that would work, it’s kind of like using a bulldozer to pound in a nail
But assuming everything calls DoStateTransition() to update the button’s internal state, I think your override idea is clever… embarrassed I didn’t think of it earlier.
I’ll let you know if this works. Only risk is that not all stage changes go through DoStateTransition() - we’ll soon find out!
Thanks!