Search Unity

Dispose instance of IInputActionCollection generated from Input Action asset

Discussion in 'Input System' started by huulong, Feb 24, 2020.

  1. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
    I noticed that the Input Action asset generate a C# class that implements both IInputActionCollection and IDisposable, but the examples never call Dispose(), which means the asset created from JSON is never destroyed.

    Code (CSharp):
    1. public class @InputActionsCharacter : IInputActionCollection, IDisposable
    2. {
    3.     public InputActionAsset asset { get; }
    4.     public @InputActionsCharacter()
    5.     {
    6.         asset = InputActionAsset.FromJson(@"{...}");
    7.     }
    8. ...
    9.     public void Dispose()
    10.     {
    11.         UnityEngine.Object.Destroy(asset);
    12.     }
    13. ...
    14. }
    15.  
    Should I call it on destroy in my control script?

    Code (CSharp):
    1. public class MyPlayerScript : MonoBehaviour
    2. {
    3.     private InputActionsCharacter m_InputActionsCharacter;
    4.  
    5.     private void Awake()
    6.     {
    7.         m_InputActionsCharacter = new InputActionsCharacter();
    8.     }
    9.  
    10.     private void OnDestroy()
    11.     {
    12.         m_InputActionsCharacter.Dispose();
    13.     }
    14. }
    15.  
     
  2. JTAU

    JTAU

    Joined:
    May 12, 2019
    Posts:
    24
    I've had some issues with this as well. Should InputAction be disposing itself or should we be manually doing the above?
     
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619
    I would also like to know whether I have to call
    Dispose
    on the "InputActions". I couldn't find any hints in the documentation.
     
  4. gareth_untether

    gareth_untether

    Joined:
    Jan 5, 2018
    Posts:
    69
    @Peter77 did you find out if InputActions should be disposed?
     
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619
    This is what I ended up using. Unfortunately I have no clue whether this is what Unity Technologies wants us to do:
    Code (CSharp):
    1. public class HeroInput : MonoBehaviour
    2. {
    3.     InputActions m_InputActions;
    4.  
    5.     void Awake()
    6.     {
    7.         m_InputActions = new InputActions();
    8.         m_InputActions.Enable();
    9.     }
    10.  
    11.     void OnDestroy()
    12.     {
    13.         m_InputActions.Disable();
    14.         SafeDispose(m_InputActions);
    15.     }
    16.  
    17.     static void SafeDispose(InputActions self)
    18.     {
    19.         if (self == null || self.asset == null)
    20.             return;
    21.  
    22.         self.Disable();
    23.  
    24.         // If we call Dispose during OnDisable/OnDestroy while the editor exits playmode,
    25.         // Unity outputs an error that you must use DestroyImmediate instead.
    26.         // So this is our workaround here.
    27.         if (Application.isPlaying)
    28.             self.Dispose();
    29.     }