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

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

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

这样就可以全部获取到在pc上可以显示按键。
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值