Search Unity

Question Get InputActionReference from InputAction

Discussion in 'Input System' started by unityenjoyeronlywhenitworks, May 6, 2023.

  1. unityenjoyeronlywhenitworks

    unityenjoyeronlywhenitworks

    Joined:
    May 6, 2023
    Posts:
    3
    Hello,

    I have my InputActionAsset stored in a variable :
    Code (CSharp):
    1. [SerializeField] private InputActionAsset inputActions;
    Looping over it, :
    Code (CSharp):
    1.         foreach (InputAction item in inputActions) { }
    How do I get the InputActionReference from one of the InputAction ?

    I want this something like this :
    Code (CSharp):
    1.         foreach (InputAction action in inputActions)
    2.         {
    3.             if (action == anotherAction)
    4.             {
    5.                 InputActionReference inputActionsRef = action;
    6.             }
    7.         }
    Thanks in advance !

    EDIT : OR another solution would work for me; can I retrieve all InputActionReference from my assets and store them into a list ? I just need a List<InputActionReference> of my actions so I can iterate through it ..
     
    Last edited: May 6, 2023
  2. unityenjoyeronlywhenitworks

    unityenjoyeronlywhenitworks

    Joined:
    May 6, 2023
    Posts:
    3
    Well, for my use case I am using this for now :

    Code (CSharp):
    1.        
    2.     [SerializeField] private List<InputActionReference> l_inputActionsRef = new List<InputActionReference>();
    3. (...)
    4. foreach (var action in inputActions)
    5.         {
    6.             InputActionReference inputActionReference = new InputActionReference();
    7.             inputActionReference.Set(action);
    8.             l_inputActionsRef.Add(inputActionReference);
    9.         }
    And I am able to iterate through this list "l_inputActionsRef" and even though I am doing "new InputActionReference();" when I use this reference, it refers to the correct action in my InputActionAsset. So yeah, I'm good for now, but if there is a cleaner way let me know !