How to get OnCancel() or other UI callbacks called

Hello everybody,
can you please give me a hint on how to get OnSubmit() or OnCancel() called in my scripts with the new Input System during UI navigation?
Input System UI Input Module and EventSystem are apparently correctly configured as i can move through UI as intended and even trigger OnClick() in UI buttons, but no callbacks such as OnCancel() or OnMove() are actually called in my scripts.
During gameplay, other actions set in another action map inside my InputActionAsset are called without any problem after players have joined the Player Input Manager. I can’t understand why during menus I can navigate and “click” without joining but I can’t get the input system callbacks to be triggered.

The following code does not work:

using UnityEngine.EventSystems;
using UnityEngine.InputSystem;

[...]

        public void OnCancel(InputAction.CallbackContext context)
        {
            Debug.Log("This is never called!");
        }

Thanks!

55 reads and no answer? Was it such a bad question? :smile:
I will let you know if I can find a solution :slight_smile:

The submit and cancel callbacks in uGUI are received through the ISubmitHandler and ICancelHandler interfaces. This works like with the old input system.

If you have one or both of the interfaces on your MonoBehaviour and you implement the respective OnSubmit/OnCancel methods, you should see the method(s) getting called when your object is selected and a submit or cancel action is triggered.

How would the Input System know which GameObject to trigger it on then?

Since there still is no answer in this thread I’m sharing my workaround in case someone else gets here from Google like I did. Just like OP I found that attaching events to the UI/Cancel event on the PlayerInput component doesn’t work. The ICancelHandler also did nothing, I guess because that only works on active UI elements? I was able to get around it by defining my own action under Player instead of under UI. Using the “Cancel [Any]” binding I was able to capture the back button/gesture on Android which was my use case.

I really think that something needs to be clarified in the documentation about this. Are the actions under the UI category only available to the Unity’s UI? It would also be good to know what the official approach to handling the back button/gesture on Android in the new input system since my approach feels a bit hacky.

2 Likes

Hi. The following works for me.

First I have a singleton which holds the InputSystemUIInputModule component because, you know, why waste the memory.

public class UIInputModuleBridge : MonoBehaviour {

    public static UIInputModuleBridge instance ;

    public InputSystemUIInputModule module ;

    private void Awake () => instance = this ;
}

Then wherever I need back button functionality I add the following component and set events as needed.

public class BackButtonInput : MonoBehaviour {
 
    [ SerializeField ] private UnityEvent onTriggered ;

    private void OnEnable () => UIInputModuleBridge.instance.module.cancel.action.performed += Trigger ;

    private void OnDisable () => UIInputModuleBridge.instance.module.cancel.action.performed -= Trigger ;

    private void Trigger ( InputAction.CallbackContext context ) => onTriggered.Invoke () ;
}

Voila! Enjoy! :smile:

1 Like

I used the above approach but instead of instantiating the InputSystemUIInputModule I get it from the current event system like this:

module = EventSystem.current.GetComponent<InputSystemUIInputModule>();

1 Like

I know this thread is super old but it’s high on Google and helped make sense of how it’s set up for me.

Navigation-type input controls the current selection based on motion read from the move action. Additionally, input from submit will trigger ISubmitHandler on the currently selected object and cancel will trigger ICancelHandler on it.

This isn’t the most clear, but it’s saying that ICancelHandler only fires on the currently selected gameobject in the eventsystem. Someone else in this thread pointed this out already and made the astute observation “how else would it know what to fire on?”. ie: you may have several UI elements that use ICancelHandler, it only makes sense for it to fire on the object that is currently the selected object in the EventSytem.

In 2025 I think the simplest solution is to just start listening to your InputActions.Cancel event when applicable and unsubscribe from it when its not.