Unity【XBox One】- 手柄输入的配置与使用

本文涉及的产品
传统型负载均衡 CLB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
应用型负载均衡 ALB,每月750个小时 15LCU
简介: Unity【XBox One】- 手柄输入的配置与使用

在Unity中使用XBox手柄进行输入的控制管理,首先需要在Project Settings / Input Manager中进行按键的配置,下图是Unity默认的Input Manager配置:

image.gif

XBox的按键在Unity中的对应关系如下:

usingUnityEngine;
namespaceSK.Framework{
/// <summary>/// XBox按键/// </summary>publicclassXBox    {
/// <summary>/// 左侧摇杆水平轴/// X axis/// </summary>publicconststringLeftStickHorizontal="LeftStickHorizontal";
/// <summary>/// 左侧摇杆垂直轴/// Y axis/// </summary>publicconststringLeftStickVertical="LeftStickVertical";
/// <summary>/// 右侧摇杆水平轴/// 4th axis/// </summary>publicconststringRightStickHorizontal="RightStickHorizontal";
/// <summary>/// 右侧摇杆垂直轴/// 5th axis/// </summary>publicconststringRightStickVertical="RightStickVertical";
/// <summary>/// 十字方向盘水平轴/// 6th axis/// </summary>publicconststringDPadHorizontal="DPadHorizontal";
/// <summary>/// 十字方向盘垂直轴/// 7th axis/// </summary>publicconststringDPadVertical="DPadVertical";
/// <summary>/// LT/// 9th axis/// </summary>publicconststringLT="LT";
/// <summary>/// RT/// 10th axis/// </summary>publicconststringRT="RT";
/// <summary>/// 左侧摇杆按键/// joystick button 8/// </summary>publicconstKeyCodeLeftStick=KeyCode.JoystickButton8;
/// <summary>/// 右侧摇杆按键/// joystick button 9/// </summary>publicconstKeyCodeRightStick=KeyCode.JoystickButton9;
/// <summary>/// A键/// joystick button 0/// </summary>publicconstKeyCodeA=KeyCode.JoystickButton0;
/// <summary>/// B键/// joystick button 1/// </summary>publicconstKeyCodeB=KeyCode.JoystickButton1;
/// <summary>/// X键/// joystick button 2/// </summary>publicconstKeyCodeX=KeyCode.JoystickButton2;
/// <summary>/// Y键/// joystick button 3/// </summary>publicconstKeyCodeY=KeyCode.JoystickButton3;
/// <summary>/// LB键/// joystick button 4/// </summary>publicconstKeyCodeLB=KeyCode.JoystickButton4;
/// <summary>/// RB键/// joystick button 5/// </summary>publicconstKeyCodeRB=KeyCode.JoystickButton5;
/// <summary>/// View视图键/// joystick button 6/// </summary>publicconstKeyCodeView=KeyCode.JoystickButton6;
/// <summary>/// Menu菜单键/// joystick button 7/// </summary>publicconstKeyCodeMenu=KeyCode.JoystickButton7;
    }
}

image.gif

根据上面的对应关系,在Input Manager中间配置,例如Left Stick Horizontal,即手柄左侧摇杆的水平轴,对应的Axis为X axis,那么它在Input Manager中的配置如下:

image.gif

右侧摇杆的垂直轴对应的Axis为5th axis,那么它在Input Manager中的配置如下:

image.gif

同理进行其他按键的配置:

image.gif

可直接编辑项目根目录下ProjectSettings文件夹中的InputManager.asset内容,使用以下内容覆盖:

%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!13 &1
InputManager:
  m_ObjectHideFlags: 0
  serializedVersion: 2
  m_Axes:
  - serializedVersion: 3
    m_Name: LeftStickHorizontal
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 0
    type: 2
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: LeftStickVertical
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 1
    type: 2
    axis: 1
    joyNum: 0
  - serializedVersion: 3
    m_Name: A
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: B
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 1
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: X
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 2
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: Y
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 3
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: LB
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 4
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: RB
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 5
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: LeftStick
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 8
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: RightStick
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 9
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: View
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 6
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: Menu
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: joystick button 7
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.001
    sensitivity: 1
    snap: 0
    invert: 0
    type: 0
    axis: 0
    joyNum: 0
  - serializedVersion: 3
    m_Name: LT
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 0
    type: 2
    axis: 8
    joyNum: 0
  - serializedVersion: 3
    m_Name: RT
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 0
    type: 2
    axis: 9
    joyNum: 0
  - serializedVersion: 3
    m_Name: DPadHorizontal
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 0
    type: 2
    axis: 5
    joyNum: 0
  - serializedVersion: 3
    m_Name: DPadVertical
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 1
    type: 2
    axis: 6
    joyNum: 0
  - serializedVersion: 3
    m_Name: RightStickHorizontal
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 1
    type: 2
    axis: 3
    joyNum: 0
  - serializedVersion: 3
    m_Name: RightStickVertical
    descriptiveName: 
    descriptiveNegativeName: 
    negativeButton: 
    positiveButton: 
    altNegativeButton: 
    altPositiveButton: 
    gravity: 0
    dead: 0.19
    sensitivity: 1
    snap: 0
    invert: 1
    type: 2
    axis: 4
    joyNum: 0

image.gif

附上XBox的按键说明:

image.gif

测试脚本:

usingUnityEngine;
usingSK.Framework;
publicclassExample : MonoBehaviour{
privatevoidUpdate()
    {
if (Input.GetKeyDown(XBox.A)) Debug.Log("A"); 
if (Input.GetKeyDown(XBox.B)) Debug.Log("B");
if (Input.GetKeyDown(XBox.X)) Debug.Log("X");
if (Input.GetKeyDown(XBox.Y)) Debug.Log("Y");
if (Input.GetKeyDown(XBox.LB)) Debug.Log("LB");
if (Input.GetKeyDown(XBox.RB)) Debug.Log("RB");
if (Input.GetKeyDown(XBox.View)) Debug.Log("View");
if (Input.GetKeyDown(XBox.Menu)) Debug.Log("Menu");
floatlh=Input.GetAxis(XBox.LeftStickHorizontal);
floatlv=Input.GetAxis(XBox.LeftStickVertical);
floatrh=Input.GetAxis(XBox.RightStickHorizontal);
floatrv=Input.GetAxis(XBox.RightStickVertical);
floatlt=Input.GetAxis(XBox.LT);
floatrt=Input.GetAxis(XBox.RT);
floatdPadH=Input.GetAxis(XBox.DPadHorizontal);
floatdPadV=Input.GetAxis(XBox.DPadVertical);
if (lh!=0) Debug.Log($"LH:{lh}");
if (lv!=0) Debug.Log($"LV:{lv}");
if (rh!=0) Debug.Log($"RH:{rh}");
if (rv!=0) Debug.Log($"RV:{rv}");
if (lt!=0) Debug.Log($"LT:{lt}");
if (rt!=0) Debug.Log($"RT:{rt}");
if (dPadH!=0) Debug.Log($"DPadH:{dPadH}");
if (dPadV!=0) Debug.Log($"DPadV:{dPadV}");
    }
}

image.gif

相关实践学习
SLB负载均衡实践
本场景通过使用阿里云负载均衡 SLB 以及对负载均衡 SLB 后端服务器 ECS 的权重进行修改,快速解决服务器响应速度慢的问题
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
2月前
|
API 开发工具 vr&ar
PicoVR Unity SDK⭐️一、SDK下载、项目设置与程序初始配置
PicoVR Unity SDK⭐️一、SDK下载、项目设置与程序初始配置
|
1月前
|
vr&ar 图形学 API
Unity与VR控制器交互全解:从基础配置到力反馈应用,多角度提升虚拟现实游戏的真实感与沉浸体验大揭秘
【8月更文挑战第31天】虚拟现实(VR)技术迅猛发展,Unity作为主流游戏开发引擎,支持多种VR硬件并提供丰富的API,尤其在VR控制器交互设计上具备高度灵活性。本文详细介绍了如何在Unity中配置VR支持、设置控制器、实现按钮交互及力反馈,结合碰撞检测和物理引擎提升真实感,助力开发者创造沉浸式体验。
71 0
|
4月前
|
图形学
【unity小技巧】unity读excel配置表操作,excel转txt文本,并读取txt文本内容,实例说明
【unity小技巧】unity读excel配置表操作,excel转txt文本,并读取txt文本内容,实例说明
79 0
|
XML 存储 JSON
Unity 使用ScriptableObject创作问答系统数据配置
Unity 使用ScriptableObject创作问答系统数据配置
157 1
Unity 使用ScriptableObject创作问答系统数据配置
|
传感器 编解码 5G
Unity配置Android开发环境与第一个Demo
ARFoundation之路-环境配置(Android)
517 0
Unity配置Android开发环境与第一个Demo
|
Java 开发工具 Android开发
2018年Unity结合Android SDK下载安装及配置教程
原文:2018年Unity结合Android SDK下载安装及配置教程 首先声明: Unity版本2017.1f3        最近试着在Unity中利用网易做AR开发时,发布项目文件需要发布到Android平台,遇到一些问题,看了网上的一些资料,踩了一些坑,现在总结出来,希望有相同的开发者遇到时可以规避。
4016 0
|
开发工具 Android开发 图形学