您的位置:首页 > 移动开发 > Unity3D

unity中连接Xbox手柄设置及按键检测

2017-01-06 09:21 302 查看
首先介绍一下unity输入设置参数所代表的含义

参数名作用描述
Name 名称轴的名称,用于游戏加载界面和脚本中。
Descriptive Name 描述游戏加载界面中,轴的正向按键的详细描述。
Descriptive Negative Name
反向描述
游戏加载界面中,轴的反向按键的详细描述。
Negative Button 反向按钮该按钮会给轴发送一个负值。
Alt Negative Button 备选反向按钮给轴发送负值的另一个按钮。
Alt Positive Button 备选正向按钮给轴发送正值的另一个按钮。
Gravity 重力输入复位的速度,仅用于类型为 键/鼠标 的按键。
Dead 阈任何小于该值的输入值(不论正负值)都会被视为0,用于摇杆。
Sensitivity灵敏度对于键盘输入,该值越大则响应时间越快,该值越小则越平滑。对于鼠标输入,设置该值会对鼠标的实际移动距离按比例缩放。
Snap 对齐如果启用该设置,当轴收到反向的输入信号时,轴的数值会立即置为0,仅用于键/鼠标 输入。
Invert 反转启用该参数可以让正向按钮发送负值,反向按钮发送正值。
Type 类型所有的按钮输入都应设置为 键/鼠标 (Key / Mouse) 类型,对于鼠标移动和滚轮应设为 鼠标移动(Mouse Movement)。摇杆设为摇杆轴 (Joystick Axis),用户移动窗口设为窗口移动 (Window Movement)。
Axis 轴设备的输入轴(摇杆,鼠标,手柄等)。
Joy Num 摇杆编号设置使用哪个摇杆。默认是接收所有摇杆的输入。仅用于输入轴和非按键。
手柄在unity输入设置示意图



左摇杆参数设置(8)



右摇杆参数设置(9)



十字键参数设置



LTRT键参数设置

这里的左右扳机(按左键返回正值,按右键返回负值)



设置好参数后,我们通过代码检测到按键信息

*以下是摇杆、十字键、和扳机键的检测设定。

using UnityEngine;
using System.Collections;

public class GetInput : MonoBehaviour {

void Update()
{
float hl = Input.GetAxis ("Horizontal_Left");
float vl = Input.GetAxis ("Vertical_Left");
float x = Input.GetAxis ("Xbox +X");
float y = Input.GetAxis ("Xbox +Y");
float hr = Input.GetAxis ("Horizontal_Right");
float vr = Input.GetAxis ("Vertical_Right");
float t = Input.GetAxis ("LRT");

if(Mathf.Abs(hl)>0.05f || Mathf.Abs(vl) > 0.05f)
{
print ("leftX:" + hl);
print ("leftY:" + vl);
}

if(Mathf.Abs(x)>0.05f || Mathf.Abs(y) > 0.05f)
{
print ("Xbox +X:" + x);
print ("Xbox +Y:" + y);
}

if(Mathf.Abs(hr)>0.05f || Mathf.Abs(vr) > 0.05f)
{
print ("RightX:" + hr);
print ("RightY:" + vr);
}

if(Mathf.Abs(t)>0.05f)
{
print ("LRT:" + t);
}
}
}


*以下是除去摇杆后的按键对应信息

AJoystickButton0/Joystick1Button0
BJoystickButton1/Joystick1Button1
XJoystickButton2/Joystick1Button2
YJoystickButton3/Joystick1Button3
LBJoystickButton4/Joystick1Button4
RBJoystickButton5/Joystick1Button5
BACKJoystickButton6/Joystick1Button6
STARTJoystickButton7/Joystick1Button7
左摇杆DOWNJoystickButton8/Joystick1Button8
右摇杆DOWNJoystickButton9/Joystick1Button9
以上如有误可评论区给予批评,谢谢留言
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: