Unity 手柄/键盘映射

using UnityEngine;
using System.Collections;

public class InputHelper
{
    public static bool IsJoystick = Screen.width > 1024 ? true : false;
    public static string Joystick_left = "left";
    public static string Joystick_right = "right";
    public static string Joystick_up = "up";
    public static string Joystick_down = "down";
    public static string Joystick_A = "joystick 1 button 0";//i
    public static string Joystick_B = "joystick 1 button 1";//k
    public static string Joystick_X = "joystick 1 button 2";//l
    public static string Joystick_Y = "joystick 1 button 3";//j
    public static string Joystick_Start = "joystick 1 button 10";
    public static string Joystick_Select = "joystick 1 button 11";
    public static string Joystick_LeftFlipper = "joystick 1 button 4";
    public static string Joystick_RightFlipper = "joystick 1 button 5";

    public static bool GetKeyDown(string keyValue)
    {
        return Input.GetKeyDown(keyValue);
    }
    public static bool GetKeyDown(KeyCode key)
    {
        bool go = false;
#if JOYSTICK_ONLY
#else
        go = Input.GetKeyDown(key);
#endif
        if (go)
            return go;
        switch (key)
        {
            case KeyCode.A:
                return GetKeyDown(Joystick_left);
            case KeyCode.W:
                return GetKeyDown(Joystick_up);
            case KeyCode.S:
                return GetKeyDown(Joystick_down);
            case KeyCode.D:
                return GetKeyDown(Joystick_right);
            case KeyCode.I:
                return GetKeyDown(Joystick_A);
            case KeyCode.K:
                return GetKeyDown(Joystick_B);
            case KeyCode.L:
                return GetKeyDown(Joystick_X) || Input.GetKeyDown(KeyCode.Escape);
            case KeyCode.J:
                return GetKeyDown(Joystick_Y);
        }
        return go;
    }
    public static bool GetKeyUp(string keyValue)
    {
        return Input.GetKeyUp(keyValue);
    }
    public static bool GetKeyUp(KeyCode key)
    {
        bool go = false;
#if JOYSTICK_ONLY
#else
        go = Input.GetKeyUp(key);
#endif
        if (go)
            return go;
        switch (key)
        {
            case KeyCode.A:
                return GetKeyUp(Joystick_left);
            case KeyCode.W:
                return GetKeyUp(Joystick_up);
            case KeyCode.S:
                return GetKeyUp(Joystick_down);
            case KeyCode.D:
                return GetKeyUp(Joystick_right);
            case KeyCode.I:
                return GetKeyUp(Joystick_A);
            case KeyCode.K:
                return GetKeyUp(Joystick_B);
            case KeyCode.L:
                return GetKeyUp(Joystick_X) || Input.GetKeyUp(KeyCode.Escape);
            case KeyCode.J:
                return GetKeyUp(Joystick_Y);
        }
        return go;
    }
    public static bool GetKey(string keyValue)
    {
        return Input.GetKey(keyValue);
    }
    public static bool GetKey(KeyCode key)
    {
        bool go = false;
#if JOYSTICK_ONLY
#else
        go = Input.GetKey(key);
#endif
        if (go)
            return go;
        switch (key)
        {
            case KeyCode.A:
                return GetKey(Joystick_left);
            case KeyCode.W:
                return GetKey(Joystick_up);
            case KeyCode.S:
                return GetKey(Joystick_down);
            case KeyCode.D:
                return GetKey(Joystick_right);
            case KeyCode.I:
                return GetKey(Joystick_A);
            case KeyCode.K:
                return GetKey(Joystick_B);
            case KeyCode.L:
                return GetKey(Joystick_X) || Input.GetKey(KeyCode.Escape);
            case KeyCode.J:
                return GetKey(Joystick_Y);
        }
        return go;
    }
}
手柄上的方向键就对应的是键盘上的方向键,所以不用映射。如:KeyCode.LeftArrow同时对应了键盘和手柄上的左键。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值