Performance Issues and Input Callbacks with New Unity Input System

Message:

Hi,

I’m encountering a persistent issue in my Unity project related to the new Input System.

I am on Unity 2022.3.7f1 with Input System 1.6.3

In my platformer game project, we’ve adopted the new Unity Input System for handling player input. However, as the game progresses and scenes are reloaded frequently due to player retries, we’ve noticed a degradation in performance and unexpected behavior. After around 100 scene reloads, the game becomes increasingly buggy, and there are noticeable spikes in input response times.

Profiler :

I’ve used Unity’s Profiler to investigate the issue further, and I’ve identified that the performance degradation is occurring in the “PreUpdate.NewInputUpdate” section, specifically in the chain “NativeInputSystem.NotifyUpdate() → InputUpdate → InputActionCallback”. This leads me to believe that there might be some inefficiency or incorrect handling in our input callbacks. Moreover, the issue happens only when reloading / loading a scene !

Input Handling Code:

Here’s a portion of the input handling code that I suspect might be contributing to the issue:

    private void SetupControls()
    {
        controls.Player.Jump.performed += ctx => OnActionTriggered();
        controls.Player.Power.performed += ctx => OnActionTriggered();
        controls.Player.Move.started += ctx => OnActionTriggered();

        controls.Player.Jump.performed += ctx => OnJump();

        controls.Player.Move.started += ctx => OnMove(ctx);
        controls.Player.Move.canceled += ctx => OnMove(ctx);

        // Other enabling...
    
    }

public void OnEnable()
{
    if (controls == null)
        controls = new PlayerControls();

    InputManager.SyncWithBindingOverrides(controls);

    SetupControls();
    controls.Enable();
}

public void OnDisable()
{
    controls.Player.Disable();
}

Are there any best practices I should be following when setting up and managing input callbacks? Is there a way to ensure proper subscription and unsubscription of callbacks to prevent accumulation and potential memory leaks?

I do not frequently do forum posts so it might lacks information or clarity, please do not hesitate if you need any further information.

Thank you in advance for your time and assistance!

Best regards,

Arthur