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同时对应了键盘和手柄上的左键。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity2D/3D开发流程主要包括以下几个步骤: 1. 确定游戏类型和玩法:在开始开发前,需要确定游戏类型和玩法,包括游戏的主题、场景、角色、关卡、道具等,以及游戏的操作方式、规则、目标等。 2. 设计游戏关卡和场景:在确定游戏类型和玩法后,需要设计游戏的关卡和场景,包括地图、道具、障碍、敌人等。在设计时需要考虑游戏的难度、节奏、流畅度和趣味性等因素。 3. 创建游戏对象和元素:在设计游戏关卡和场景后,需要创建游戏对象和元素,包括角色、道具、敌人、场景元素等。可以使用Unity的编辑器来创建和编辑游戏对象和元素,也可以使用第三方工具来创建和导入。 4. 添加组件和脚本:在创建游戏对象和元素后,需要添加相应的组件和脚本来控制其行为和动作。可以使用Unity的组件和脚本来实现常见的游戏行为,也可以使用C#脚本来编写自定义的游戏逻辑。 5. 调试和测试:在添加组件和脚本后,需要进行调试和测试,以确保游戏的功能和表现符合设计要求。可以使用Unity的调试工具来检查游戏对象和脚本的状态和行为,也可以使用第三方工具来进行测试和调试。 6. 优化和打包发布:在完成测试和调试后,需要对游戏进行优化和打包发布。可以使用Unity的优化工具来优化游戏性能和资源占用,也可以使用Unity的打包工具来打包和发布游戏到不同的平台和设备。 以上是Unity2D/3D开发流程的主要步骤,每个步骤都需要认真考虑和实践,以确保游戏的质量和用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值