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

首先介绍一下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

以上如有误可评论区给予批评,谢谢留言

  • 14
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值