unity基础开发----unity获取外部设备(方向盘)按键
来源:互联网 发布:八岐大蛇 mac 蓝牙 编辑:程序博客网 时间:2024/05/09 09:51
在unity中可能会用到外部的设备,比如是游戏手柄,赛车的方向盘手柄,在unity中 input manger中最多可以获取到10个按键,但是就像赛车的游戏手柄可能在电脑pc上可以显示,但是在unity中就获取不到了。那我们只能用其他的方法来解决这个问题了。
using UnityEngine;using System.Collections;using System.Runtime.InteropServices;public class JoystickController : MonoBehaviour {public struct JOYINFOEX{/// <summary>/// Size, in bytes, of this structure./// </summary>public int dwSize;/// <summary>/// Flags indicating the valid information returned in this structure. Members that do not contain valid information are set to zero./// </summary>public int dwFlags;/// <summary>/// Current X-coordinate./// </summary>public int dwXpos;/// <summary>/// Current Y-coordinate./// </summary>public int dwYpos;/// <summary>/// Current Z-coordinate./// </summary>public int dwZpos;/// <summary>/// Current position of the rudder or fourth joystick axis./// </summary>public int dwRpos;/// <summary>/// Current fifth axis position./// </summary>public int dwUpos;/// <summary>/// Current sixth axis position./// </summary>public int dwVpos;/// <summary>/// Current state of the 32 joystick buttons. The value of this member can be set to any combination of JOY_BUTTONn flags, where n is a value in the range of 1 through 32 corresponding to the button that is pressed./// </summary>public int dwButtons;/// <summary>/// Current button number that is pressed./// </summary>public int dwButtonNumber;/// <summary>/// Current position of the point-of-view control. Values for this member are in the range 0 through 35,900. These values represent the angle, in degrees, of each view multiplied by 100./// </summary>public int dwPOV;/// <summary>/// Reserved; do not use./// </summary>public int dwReserved1;/// <summary>/// Reserved; do not use./// </summary>public int dwReserved2;};[DllImport("winmm")] public static extern int joyGetPosEx(int uJoyID, ref JOYINFOEX pji);private JOYINFOEX infoEx;private string currentButton;private string currentAxis;private float axisInput;// Use this for initializationvoid Start () { //Device joystickDevice; //JoystickState state;infoEx = new JOYINFOEX();infoEx.dwSize = Marshal.SizeOf(typeof(JOYINFOEX));infoEx.dwFlags=0x00000080;}// Update is called once per framevoid Update () {getButton();}/// <summary>/// get the button data of the joystick/// </summary>void getButton(){int e = joyGetPosEx(0,ref infoEx);if (e==0){int mask=0x10;string str = string.Empty;for(int i=5;i<32;i++){if ((infoEx.dwButtons & mask) > 0){str = str + string.Format("button({0})",i);}mask = mask << 1;}Debug.Log(str);}}}
这样就可以全部获取到在pc上可以显示按键。
0 1
- unity基础开发----unity获取外部设备(方向盘)按键
- unity基础开发----unity获取外部设备(方向盘)按键
- 【Unity】Unity 获取当前按键
- unity 获取当前键盘按键
- Unity+FairyGUI)rpg方向控制的方向盘控制/摇杆
- 【Unity】Unity按任意键获取 按键名字
- Unity连接罗技G27方向盘
- unity实现车方向盘转动效果
- 【Unity开发】Unity获取设备屏幕分辨率
- 【Unity开发】获取Unity Stats面板
- unity基础开发----unity串口通信
- unity基础开发----Unity获取PC,Ios系统的mac地址等信息
- unity基础开发--shader基础知识
- unity基础开发-photon服务器
- unity基础开发--学习笔记
- 谷歌 CardBoard Unity开发基础
- unity C#脚本开发基础
- Unity编辑器Unity Editor基础(一)
- oracle case when的使用方法
- WordPress如何更换域名
- 第四章 网络层 4.5因特网的路由选择协议
- Java 6 JVM参数选项大全(中文版)
- 利用axis1.4根据wsdl文件生成对应的java代码
- unity基础开发----unity获取外部设备(方向盘)按键
- 函数getaddrinfo()
- GNU Make简介
- 关于备份
- 网页布局,关于外层div不能随内层div自动伸缩的问题
- get和post的区别
- JS判断手机浏览器
- PRD产品需求文档概要
- 当调用标准的malloc函数时,内核发生了什么