How to subscribe to button state change (from interactable to not-interactable)

Is there some way I can listen for when a Selectable changes state? In my case from “Normal” to “Disabled” and back. There is no event for this on the Selectable (why not?) and I cannot figure out a smart way to do it.

Basically when a button is set to interactable I want to fade the content of that button. This is true for all buttons in my game so I would like a generic system for it I can re-use.

I found the below DoStateTransition method but I cant figure out if its actually possible to work with it without making my own custom Selectable class.

public class MyButton : Button
{
     protected override void DoStateTransition(SelectionState state, bool instant)
     {
         if (state == SelectionState.Disabled)
         {
         }
         else if (state == SelectionState.Normal)
         {
            
         }
     }
1 Like

Oh yes, most yes! :slight_smile:

What you want is to implement ISelectHandler and IDeselectHandler!

using UnityEngine;
using UnityEngine.EventSystems;

// @kurtdekker
// put this on the UnityEngine.UI.Button's GameObject

public class ListenToSelectable : MonoBehaviour, ISelectHandler, IDeselectHandler
{
    public void OnSelect (BaseEventData eventData)
    {
        Debug.Log( GetType() + "-" + name + "-OnSelect();");
    }
    public void OnDeselect (BaseEventData eventData)
    {
        Debug.Log( GetType() + "-" + name + "-OnDeselect();");
    }
}

Thanks but I think you misunderstood the question :slight_smile:

I was probably unclear, I want to listen for when IsInteractable changes value.

I would just check it in Update() and see if it changes, then signal whatever events you want to hook up.

You could probably have hundreds of buttons in your UI before the overhead even showed up in the profiler.

Ouf, that was my plan B as well, not a fan but if its the only way then I might have to do that. Thanks.

1 Like

You’ll get over it. :slight_smile:

Even if there was something called IInteractableChanged, inside the engine there is still a compare-and-decide happening. Instead you’re doing it in your Update(). Potato, potahto…

If you absolutely cannot live with the idea of polling (which the entire computer is doing all the time anyway), just make your own wrapper script that goes on every object that you want to set interactible or not, and hook it there!

1 Like