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
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 (Input Action Assets | Package Manager UI website) 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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using UnityEngine;
public class Test : MonoBehaviour
{
private NewInputSystem _ActionMappings;
public PlayerInput _playerInput;
void Start()
{
// by default we are starting with "Player" as the action map
_ActionMappings = new NewInputSystem();
Debug.Log("Current Action Map: " + _playerInput.currentActionMap.ToString());
// I would expect this to return "UI" to the method, but its returning other jargon with it.
_playerInput.SwitchCurrentActionMap(_ActionMappings.UI.ToString());
Debug.Log("Current Action Map: " + _playerInput.currentActionMap.ToString());
}
}