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

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

[csharp]  view plain copy
  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. ///   
  11. /// Size, in bytes, of this structure.  
  12. ///   
  13. public int dwSize;  
  14. ///   
  15. /// Flags indicating the valid information returned in this structure. Members that do not contain valid information are set to zero.  
  16. ///   
  17. public int dwFlags;  
  18. ///   
  19. /// Current X-coordinate.  
  20. ///   
  21. public int dwXpos;  
  22. ///   
  23. /// Current Y-coordinate.  
  24. ///   
  25. public int dwYpos;  
  26. ///   
  27. /// Current Z-coordinate.  
  28. ///   
  29. public int dwZpos;  
  30. ///   
  31. /// Current position of the rudder or fourth joystick axis.  
  32. ///   
  33. public int dwRpos;  
  34. ///   
  35. /// Current fifth axis position.  
  36. ///   
  37. public int dwUpos;  
  38. ///   
  39. /// Current sixth axis position.  
  40. ///   
  41. public int dwVpos;  
  42. ///   
  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. ///   
  45. public int dwButtons;  
  46. ///   
  47. /// Current button number that is pressed.  
  48. ///   
  49. public int dwButtonNumber;  
  50. ///   
  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. ///   
  53. public int dwPOV;  
  54. ///   
  55. /// Reserved; do not use.  
  56. ///   
  57. public int dwReserved1;  
  58. ///   
  59. /// Reserved; do not use.  
  60. ///   
  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.     ///   
  86.     /// get the button data of the joystick  
  87.     ///   
  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上可以显示按键。

你可能感兴趣的:(Unity3D)