How to remove and InputAction phase callback

Hello, the documentation is pretty clear on how to add a callback for an InputAction phase with :

var action = new InputAction();

action.started += ctx => /* Action was started */;
action.performed += ctx => /* Action was performed */;
action.canceled += ctx => /* Action was canceled */;

However, I didn’t find anything related to removing a callback. I tried with this

action.started -= ctx => /* Action was started */;

But it doesn’t seem to work. Any idea ? Thanks.

This is how C# events work.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events

So you need to do something like this:

private void doSomething(CallbackContext ctx)
{
    // do the thing
}

and then you can

// register
action.started += doSomething;
// unregsiter
action.started -= doSomething;
2 Likes

Absolute blessed post!

I ran into this same issue since all of the resources I was looking at was telling me to subscribe it how Shashimee posted above.

After reading the Microsoft API for subscribing events, it would appear the difference is between using an anonymous function (Shashimee’s post) which would require delegates to swap the callback reference and using a properly subscribed event. You can’t unsubscribe an anonymous function.

I wanted to post this in the event anyone else is running into the issue, definitely take Lurking-Ninja’s approach!

It’s as simple as:

action.started += doSomething;
action.canceled += doSomething;

action.started -= doSomething;
action.canceled -= doSomething;
private void DoSomething(InputAction.CallbackContext ctx)
{
    if (ctx.started)
    {
        // do the thing
    }

    else if (ctx.canceled)
    {
        // do the other thing
    }


}