daemon3000 / InputManager

Custom InputManager for Unity
Other
587 stars 88 forks source link

How to disable Keyboard Input #24

Closed RanaInside closed 7 years ago

RanaInside commented 7 years ago

I'm sorry if this has already been answered - I did look through the wiki multiple times, but couldn't find any notes about this.

So, I have an Input Manager with KeyBoard, Windows+Mac+Linux gamepad; I also have an Input Adapter where I have set windows gamepad only for player1.

When unity starts, I get a log message that Keyboard+mouse is being used; now it does switch to joystick if I use it, and then jumps back to keyboard the moment I move the mouse.

So, question is: can I completely disable this switch and have it stick to joystick.

PS: I did try unchecking realtime device hoping that as I have joystick setup, it will stick to that; instead it sticks to keyboard instead.

Thanks in advance.

daemon3000 commented 7 years ago

If you don't want the input device to change don't use the InputAdapter script. Instead use a simple script like the following:

using UnityEngine;
using TeamUtility.IO;

public class SelectInputTypeOnStart : MonoBehaviour
{
    public enum InputType
    {
        KeyboardAndMouse,
        Joystick
    }

    [SerializeField]
    private InputType m_defaultInputType = InputType.Joystick;

    [SerializeField]
    private string m_keyboardConfiguration = "KeyboardAndMouse";

    [SerializeField]
    private string m_windowsJoystickConfiguration = "Windows_Gamepad";

    [SerializeField]
    private string m_osxJoystickConfiguration = "OSX_Gamepad";

    [SerializeField]
    private string m_linuxJoystickConfiguration = "Linux_Gamepad";

    private void Start()
    {
        SetInputType(m_defaultInputType, PlayerID.One);
    }

    public void SetInputType(InputType inputType, PlayerID playerID)
    {
        string inputConfigName = GetInputConfiguration(inputType);
        InputManager.SetInputConfiguration(inputConfigName, playerID);
    }

    private string GetInputConfiguration(InputType inputType)
    {
        if(inputType == InputType.KeyboardAndMouse)
        {
            return m_keyboardConfiguration;
        }
        else
        {
            switch(Application.platform)
            {
            case RuntimePlatform.WindowsPlayer:
            case RuntimePlatform.WindowsEditor:
                return m_windowsJoystickConfiguration;
            case RuntimePlatform.OSXPlayer:
            case RuntimePlatform.OSXEditor:
                return m_osxJoystickConfiguration;
            case RuntimePlatform.LinuxPlayer:
                return m_linuxJoystickConfiguration;
            default:
                Debug.LogWarning("Unsupported XBOX 360 Controller driver. The default Windows driver configuration has been chosen.");
                return m_windowsJoystickConfiguration;
            }
        }
    }
}

SelectInputTypeOnStart.SetInputType is public so if you have a menu where the player can choose the input device call this method from one of your UI scripts.