在unity中可能会用到外部的设备,比如是游戏手柄,赛车的方向盘手柄,在unity中 input manger中最多可以获取到10个按键,但是就像赛车的游戏手柄可能在电脑pc上可以显示,但是在unity中就获取不到了。那我们只能用其他的方法来解决这个问题了。

[csharp] view plaincopy
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Runtime.InteropServices;
  4. public class JoystickController : MonoBehaviour {
  5. public struct JOYINFOEX
  6. {
  7. /// <summary>
  8. /// Size, in bytes, of this structure.
  9. /// </summary>
  10. public int dwSize;
  11. /// <summary>
  12. /// Flags indicating the valid information returned in this structure. Members that do not contain valid information are set to zero.
  13. /// </summary>
  14. public int dwFlags;
  15. /// <summary>
  16. /// Current X-coordinate.
  17. /// </summary>
  18. public int dwXpos;
  19. /// <summary>
  20. /// Current Y-coordinate.
  21. /// </summary>
  22. public int dwYpos;
  23. /// <summary>
  24. /// Current Z-coordinate.
  25. /// </summary>
  26. public int dwZpos;
  27. /// <summary>
  28. /// Current position of the rudder or fourth joystick axis.
  29. /// </summary>
  30. public int dwRpos;
  31. /// <summary>
  32. /// Current fifth axis position.
  33. /// </summary>
  34. public int dwUpos;
  35. /// <summary>
  36. /// Current sixth axis position.
  37. /// </summary>
  38. public int dwVpos;
  39. /// <summary>
  40. /// 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.
  41. /// </summary>
  42. public int dwButtons;
  43. /// <summary>
  44. /// Current button number that is pressed.
  45. /// </summary>
  46. public int dwButtonNumber;
  47. /// <summary>
  48. /// 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.
  49. /// </summary>
  50. public int dwPOV;
  51. /// <summary>
  52. /// Reserved; do not use.
  53. /// </summary>
  54. public int dwReserved1;
  55. /// <summary>
  56. /// Reserved; do not use.
  57. /// </summary>
  58. public int dwReserved2;
  59. };
  60. [DllImport("winmm")] public static extern int joyGetPosEx(int uJoyID, ref JOYINFOEX pji);
  61. private JOYINFOEX infoEx;
  62. private string currentButton;
  63. private string currentAxis;
  64. private float axisInput;
  65. // Use this for initialization
  66. void Start () {
  67. //Device joystickDevice;
  68. //JoystickState state;
  69. infoEx = new JOYINFOEX();
  70. infoEx.dwSize = Marshal.SizeOf(typeof(JOYINFOEX));
  71. infoEx.dwFlags=0x00000080;
  72. }
  73. // Update is called once per frame
  74. void Update ()
  75. {
  76. getButton();
  77. }
  78. /// <summary>
  79. /// get the button data of the joystick
  80. /// </summary>
  81. void getButton()
  82. {
  83. int e = joyGetPosEx(0,ref infoEx);
  84. if (e==0)
  85. {
  86. int mask=0x10;
  87. string str = string.Empty;
  88. for(int i=5;i<32;i++)
  89. {
  90. if ((infoEx.dwButtons & mask) > 0)
  91. {
  92. str = str + string.Format("button({0})",i);
  93. }
  94. mask = mask << 1;
  95. }
  96. Debug.Log(str);
  97. }
  98. }
  99. }

这样就可以全部获取到在pc上可以显示按键。

unity基础开发----unity获取外部设备(方向盘)按键相关推荐

  1. unity基础开发----Unity获取PC,Ios系统的mac地址等信息

    在软件开发中可以会用到mac地址作为,设备的唯一标示,我们也可以通过unity获取,经测试pc,ios都可以但是安卓没有获取到. 代码如下: using UnityEngine; using Syst ...

  2. Unity基础开发之事件函数的执行顺序

    Unity基础开发之事件函数的执行顺序 文章目录 前言 一.Initializtion阶段 Awake() OnEnable() 二.Editor阶段 Reset() OnValidate() 三.第 ...

  3. Unity 基础开发-WebGL发布无法运行问题

    项目需要发布webGL项目,之前测试过本地可以在火狐浏览器上运行,Unity也支持发布,发布效率比较慢,支持的内容比较少.在做测试的时候还是遇到了问题. Unity版本 20182.4f 火狐浏览器版 ...

  4. 【Unity 框架】QFramework v1.0 使用指南 介绍篇:01. 简介 | Unity 游戏框架 | Unity 游戏开发 | Unity 独立游戏

    01. 简介 大家好,我是 QFramework 的作者 凉鞋,QFramework 从第一次代码提交到现在快 7 年了(2015 年 12 月 ~ 2022 年 10 月)了,而经过了 7 年时间的 ...

  5. 1.4 从0开始学Unity游戏开发--Unity编辑器初见

    安装 没啥好说的,要先安装一个Unity Hub,然后在Unity Hub里面安装编辑器,但是有两点要注意,Unity的收费模式跟随你使用的场景而不同,这里我都默认使用个人开发者版本,这个是免费的,如 ...

  6. Unity游戏开发——unity接入多渠道sdk

    最近游戏进入最后测试和接入sdk阶段,对接了多个sdk简单记录和分享一下在接入各个渠道sdk中遇到的问题和坑,在之后或其他人看到之后可以绕过,提高效率仅此而已. 我们使用的unity开发,在选择sdk ...

  7. 【Unity 框架】QFramework v1.0 使用指南 工具篇:06. UIKit 界面管理快速开发解决方案 | Unity 游戏框架 | Unity 游戏开发 | Unity 独立游戏

    UI Kit 简介 UI Kit 是一套界面管理&快速开发解决方案 UI Kit 的特性如下: 界面管理 层级管理 代码生成及组件自动绑定(底层用的 ViewController) UI Ki ...

  8. unity基础知识----unity界面菜单翻译

    untiy界面菜单翻译对照

  9. Unity基础知识汇总

    2.Unity相关知识 2.1 Unity介绍 Unity成为一款可以轻松创建游戏和三维互动的开发工具,是一个专业跨平台游戏引擎 Unity操作快捷键 Ctrl N New Scene 新建场景 Ct ...

最新文章

  1. webpack打包优化_前端性能优化:webpack性能调优与Gzip原理
  2. URAL 1993 This cheeseburger you don't need 模拟题
  3. ArcGIS Pro发布三维场景服务
  4. 阿里云面试官:如果是MySQL引起的CPU消耗过大,你会如何优化?
  5. 8K 星!这可能是最适合你的 TensorFlow 教程
  6. vs2010 利用DMP文件、pdb文件查找release下的异常行号的方法
  7. 数组分成两组差值最小 python_数组中的数分为两组,让给出一个算法,使得两个组的和的差的绝对值最小,数组中的数的取值范围是0x100,元素个数也是大于0, 小于100 。...
  8. jparepository查询所有_jparepository 怎样自定义查询
  9. Oracle中ascii为0的陷阱
  10. C# using 使用方法
  11. linux卸载文件系统什么意思,Linux文件系统的安装和卸载
  12. 日常投票评分:大多都是拉票
  13. sonar打包出现的问题The forked VM terminated without saying properly goodbye. VM crash or System.exit called
  14. 公众号搜题题库接口使用方法
  15. 计算机使用的一些技巧
  16. 什么是短视频电商,短视频电商的商业模式标题】
  17. 如何用 VS Code remote 插件编辑远程 linux 服务器上的文件
  18. 台式计算机内存是什么问题,如何解决台式机上内存不足的问题
  19. 如何以厘米为单位精确设置Excel表格的行高列宽?
  20. ata驱动框架及scsi请求处理流程

热门文章

  1. 【CF768G】The Winds of Winter(启发式合并)(二分答案)
  2. 测试人明年跳槽,我劝你千万别忽略这些“细节”不然……
  3. RXJS 中 Observable 和 Subject
  4. 倍福TwinCAT(贝福Beckhoff)应用教程12.2 TwinCAT控制松下伺服 NC初步
  5. 行业解决方案|智慧检务:聚焦检察院工作数字化
  6. 2018.8.22日阿里巴巴java实习生面试答案整理
  7. 深入剖析搜索引擎:了解搜索技术背后的神秘工作原理!
  8. java实现后台直接生成二维码图片并直接上传到七牛云上面
  9. 2面之后说等HR通知,等不到怎么办
  10. 14.深入浅出:信号的运算——参考《模拟电子技术基础》清华大学华成英主讲