Advertisement

We're Getting Mutants in the MCU - The Loop

01:36

AC supports "mouse and keyboard" and "keyboard/controller" input schemes through the Settings Manager's "Input method" field.  However, this field remains fixed at runtime, so must be chosen beforehand.

This script allows the field to be changed dynamically upon recieving input, switching to "Keyboard Or Controller" when a gamepad button is pressed, and back to "Mouse And Keyboard" when any other button is pressed.  It also makes changes to any other Manager field necessary for that new mode to work correctly.  By default, this is only the Menu Manager's "Direct control Menus" checkboxes - but comments in the script show where additional changes can be made.

To change a Manager field's value through script, right-click the field's label to copy an API reference to it.

To use the script, place in a new C# file named SwitchInputs, and attach to a GameObject in the scene.

SwitchInputs.cs:

using UnityEngine;
using AC;

public class SwitchInputs : MonoBehaviour
{

	[SerializeField] private InputMethod defaultInputMethod;

	private void Start ()
	{
		SetInputMethod (defaultInputMethod, true);
	}

	private void Update ()
	{
		if (Input.anyKeyDown)
		{
			if (Input.GetButtonDown ("JoystickButton"))
			{
				SetInputMethod (InputMethod.KeyboardOrController);
			}
			else
			{
				SetInputMethod (InputMethod.MouseAndKeyboard);
			}
		}
	}

	private void SetInputMethod (InputMethod inputMethod, bool force = false)
	{
		if (KickStarter.settingsManager.inputMethod != inputMethod || force)
		{
			KickStarter.settingsManager.inputMethod = inputMethod;

			switch (inputMethod)
			{
				case InputMethod.MouseAndKeyboard:
					KickStarter.menuManager.keyboardControlWhenCutscene = KickStarter.menuManager.keyboardControlWhenPaused = KickStarter.menuManager.keyboardControlWhenDialogOptions = false;
                    // Set any mouse/keyboard-specific settings here
					break;

				case InputMethod.KeyboardOrController:
					KickStarter.menuManager.keyboardControlWhenCutscene = KickStarter.menuManager.keyboardControlWhenPaused = KickStarter.menuManager.keyboardControlWhenDialogOptions = true;
					KickStarter.playerMenus.FindFirstSelectedElement ();
                    // Set any keyboard/controller-specific settings here
					break;

				default:
					break;
			}
		}
	}

}
Community content is available under CC-BY-SA unless otherwise noted.