Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Switching Current Action Map from code

Discussion in 'Input System' started by mvinc006, Nov 7, 2019.

  1. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    I'm trying to figure out how I change the current ActionMap to another in script, i.e from Player to UI just as an example.

    I understand that the PlayerInput class has a method called
    Code (CSharp):
    1. SwitchCurrentActionMap(string mapNameOrId);
    .

    Instead of passing a hard coded string (for obvious reasons) I'm wanting to access the ActionMaps that are already defined in the InputActionAsset I've already made (through the right click > create Input Actions menu).

    The docs say this should be possible (https://docs.unity3d.com/Packages/c...l/ActionAssets.html#using-input-action-assets) though I cannot get it working.

    After trial and error for a few hours I couldn't get it to work. The closest I got was accessing the C# Class name of the InputActionAsset I'd created (in this case the default is NewInputSystem.cs) but there is no way to assign this in the inspector, and I'll get Null Reference errors because its not assigned.

    I'd then tried to access the Mappings Class directly from the PlayerInput class, but no luck there either.

    Below is some test code, it's the closest I've managed to get but I'm missing something here.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.InputSystem;
    4. using UnityEngine;
    5.  
    6. public class Test : MonoBehaviour
    7. {
    8.  
    9.     private NewInputSystem _ActionMappings;
    10.     public PlayerInput _playerInput;
    11.  
    12.     void Start()
    13.     {
    14.         // by default we are starting with "Player" as the action map
    15.         _ActionMappings = new NewInputSystem();
    16.         Debug.Log("Current Action Map: " + _playerInput.currentActionMap.ToString());
    17.      
    18.         // I would expect this to return "UI" to the method, but its returning other jargon with it.
    19.         _playerInput.SwitchCurrentActionMap(_ActionMappings.UI.ToString());
    20.  
    21.         Debug.Log("Current Action Map: " + _playerInput.currentActionMap.ToString());
    22.     }
    23.  
    24. }
     
    Last edited: Nov 7, 2019
    tigerleapgorge likes this.
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    There's no way currently to combine the C# classes generated for .inputactions assets and PlayerInput. An integration is planned for after 1.0.

    Using the generated class for lookups the way you have it in the code can be done. Replace "ToString()" with "name" and it should do what you intend.
     
    tigerleapgorge and mvinc006 like this.
  3. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    Thank you so easy I can’t believe I missed that.
     
    tigerleapgorge likes this.