unity基础开发----unity获取外部设备(方向盘)按键
在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 initialization
- void Start () {
- //Device joystickDevice;
- //JoystickState state;
- infoEx = new JOYINFOEX();
- infoEx.dwSize = Marshal.SizeOf(typeof(JOYINFOEX));
- infoEx.dwFlags=0x00000080;
- }
- // Update is called once per frame
- void 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上可以显示按键。
unity基础开发----unity获取外部设备(方向盘)按键相关推荐
- unity基础开发----Unity获取PC,Ios系统的mac地址等信息
在软件开发中可以会用到mac地址作为,设备的唯一标示,我们也可以通过unity获取,经测试pc,ios都可以但是安卓没有获取到. 代码如下: using UnityEngine; using Syst ...
- Unity基础开发之事件函数的执行顺序
Unity基础开发之事件函数的执行顺序 文章目录 前言 一.Initializtion阶段 Awake() OnEnable() 二.Editor阶段 Reset() OnValidate() 三.第 ...
- Unity 基础开发-WebGL发布无法运行问题
项目需要发布webGL项目,之前测试过本地可以在火狐浏览器上运行,Unity也支持发布,发布效率比较慢,支持的内容比较少.在做测试的时候还是遇到了问题. Unity版本 20182.4f 火狐浏览器版 ...
- 【Unity 框架】QFramework v1.0 使用指南 介绍篇:01. 简介 | Unity 游戏框架 | Unity 游戏开发 | Unity 独立游戏
01. 简介 大家好,我是 QFramework 的作者 凉鞋,QFramework 从第一次代码提交到现在快 7 年了(2015 年 12 月 ~ 2022 年 10 月)了,而经过了 7 年时间的 ...
- 1.4 从0开始学Unity游戏开发--Unity编辑器初见
安装 没啥好说的,要先安装一个Unity Hub,然后在Unity Hub里面安装编辑器,但是有两点要注意,Unity的收费模式跟随你使用的场景而不同,这里我都默认使用个人开发者版本,这个是免费的,如 ...
- Unity游戏开发——unity接入多渠道sdk
最近游戏进入最后测试和接入sdk阶段,对接了多个sdk简单记录和分享一下在接入各个渠道sdk中遇到的问题和坑,在之后或其他人看到之后可以绕过,提高效率仅此而已. 我们使用的unity开发,在选择sdk ...
- 【Unity 框架】QFramework v1.0 使用指南 工具篇:06. UIKit 界面管理快速开发解决方案 | Unity 游戏框架 | Unity 游戏开发 | Unity 独立游戏
UI Kit 简介 UI Kit 是一套界面管理&快速开发解决方案 UI Kit 的特性如下: 界面管理 层级管理 代码生成及组件自动绑定(底层用的 ViewController) UI Ki ...
- unity基础知识----unity界面菜单翻译
untiy界面菜单翻译对照
- Unity基础知识汇总
2.Unity相关知识 2.1 Unity介绍 Unity成为一款可以轻松创建游戏和三维互动的开发工具,是一个专业跨平台游戏引擎 Unity操作快捷键 Ctrl N New Scene 新建场景 Ct ...
最新文章
- webpack打包优化_前端性能优化:webpack性能调优与Gzip原理
- URAL 1993 This cheeseburger you don't need 模拟题
- ArcGIS Pro发布三维场景服务
- 阿里云面试官:如果是MySQL引起的CPU消耗过大,你会如何优化?
- 8K 星!这可能是最适合你的 TensorFlow 教程
- vs2010 利用DMP文件、pdb文件查找release下的异常行号的方法
- 数组分成两组差值最小 python_数组中的数分为两组,让给出一个算法,使得两个组的和的差的绝对值最小,数组中的数的取值范围是0x100,元素个数也是大于0, 小于100 。...
- jparepository查询所有_jparepository 怎样自定义查询
- Oracle中ascii为0的陷阱
- C# using 使用方法
- linux卸载文件系统什么意思,Linux文件系统的安装和卸载
- 日常投票评分:大多都是拉票
- sonar打包出现的问题The forked VM terminated without saying properly goodbye. VM crash or System.exit called
- 公众号搜题题库接口使用方法
- 计算机使用的一些技巧
- 什么是短视频电商,短视频电商的商业模式标题】
- 如何用 VS Code remote 插件编辑远程 linux 服务器上的文件
- 台式计算机内存是什么问题,如何解决台式机上内存不足的问题
- 如何以厘米为单位精确设置Excel表格的行高列宽?
- ata驱动框架及scsi请求处理流程
热门文章
- 【CF768G】The Winds of Winter(启发式合并)(二分答案)
- 测试人明年跳槽,我劝你千万别忽略这些“细节”不然……
- RXJS 中 Observable 和 Subject
- 倍福TwinCAT(贝福Beckhoff)应用教程12.2 TwinCAT控制松下伺服 NC初步
- 行业解决方案|智慧检务:聚焦检察院工作数字化
- 2018.8.22日阿里巴巴java实习生面试答案整理
- 深入剖析搜索引擎:了解搜索技术背后的神秘工作原理!
- java实现后台直接生成二维码图片并直接上传到七牛云上面
- 2面之后说等HR通知,等不到怎么办
- 14.深入浅出:信号的运算——参考《模拟电子技术基础》清华大学华成英主讲