暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

WebGLGamepad.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #if UNITY_WEBGL || UNITY_EDITOR || PACKAGE_DOCS_GENERATION
  2. using System;
  3. using System.ComponentModel;
  4. using UnityEngine.InputSystem.Layouts;
  5. using UnityEngine.InputSystem.LowLevel;
  6. using UnityEngine.InputSystem.WebGL.LowLevel;
  7. using UnityEngine.InputSystem.Utilities;
  8. namespace UnityEngine.InputSystem.WebGL.LowLevel
  9. {
  10. internal unsafe struct WebGLGamepadState : IInputStateTypeInfo
  11. {
  12. public const int NumAxes = 4;
  13. public const int NumButtons = 16;
  14. private const int ButtonOffset = NumAxes * 4;
  15. // Stick default format is already two floats so all we need to do is move the sticks and
  16. // put inverts on Y.
  17. [InputControl(name = "leftStick", offset = 0)]
  18. [InputControl(name = "rightStick", offset = 8)]
  19. [InputControl(name = "leftStick/y", parameters = "invert")]
  20. [InputControl(name = "leftStick/up", parameters = "clamp=2,clampMin=0,clampMax=1,invert")]
  21. [InputControl(name = "leftStick/down", parameters = "clamp=2,clampMin=-1,clampMax=0,invert=false")]
  22. [InputControl(name = "rightStick/y", parameters = "invert")]
  23. [InputControl(name = "rightStick/up", parameters = "clamp=2,clampMin=0,clampMax=1,invert")]
  24. [InputControl(name = "rightStick/down", parameters = "clamp=2,clampMin=-1,clampMax=0,invert=false")]
  25. // All the buttons we need to bump from single bits to full floats and reset bit offsets.
  26. [InputControl(name = "buttonSouth", offset = ButtonOffset + 0 * 4, bit = 0, format = "FLT")]
  27. [InputControl(name = "buttonEast", offset = ButtonOffset + 1 * 4, bit = 0, format = "FLT")]
  28. [InputControl(name = "buttonWest", offset = ButtonOffset + 2 * 4, bit = 0, format = "FLT")]
  29. [InputControl(name = "buttonNorth", offset = ButtonOffset + 3 * 4, bit = 0, format = "FLT")]
  30. [InputControl(name = "leftShoulder", offset = ButtonOffset + 4 * 4, bit = 0, format = "FLT")]
  31. [InputControl(name = "rightShoulder", offset = ButtonOffset + 5 * 4, bit = 0, format = "FLT")]
  32. [InputControl(name = "leftTrigger", offset = ButtonOffset + 6 * 4, bit = 0, format = "FLT")]
  33. [InputControl(name = "rightTrigger", offset = ButtonOffset + 7 * 4, bit = 0, format = "FLT")]
  34. [InputControl(name = "select", offset = ButtonOffset + 8 * 4, bit = 0, format = "FLT")]
  35. [InputControl(name = "start", offset = ButtonOffset + 9 * 4, bit = 0, format = "FLT")]
  36. [InputControl(name = "leftStickPress", offset = ButtonOffset + 10 * 4, bit = 0, format = "FLT")]
  37. [InputControl(name = "rightStickPress", offset = ButtonOffset + 11 * 4, bit = 0, format = "FLT")]
  38. [InputControl(name = "dpad", offset = ButtonOffset + 12 * 4, bit = 0, sizeInBits = 4 * 4 * 8)]
  39. [InputControl(name = "dpad/up", offset = 0, bit = 0, format = "FLT")]
  40. [InputControl(name = "dpad/down", offset = 4, bit = 0, format = "FLT")]
  41. [InputControl(name = "dpad/left", offset = 8, bit = 0, format = "FLT")]
  42. [InputControl(name = "dpad/right", offset = 12, bit = 0, format = "FLT")]
  43. public fixed float values[NumButtons + NumAxes];
  44. public float leftTrigger
  45. {
  46. get => GetValue(NumAxes + 6);
  47. set => SetValue(NumAxes + 6, value);
  48. }
  49. public float rightTrigger
  50. {
  51. get => GetValue(NumAxes + 7);
  52. set => SetValue(NumAxes + 7, value);
  53. }
  54. public Vector2 leftStick
  55. {
  56. get => new Vector2(GetValue(0), GetValue(1));
  57. set
  58. {
  59. SetValue(0, value.x);
  60. SetValue(1, value.y);
  61. }
  62. }
  63. public Vector2 rightStick
  64. {
  65. get => new Vector2(GetValue(2), GetValue(3));
  66. set
  67. {
  68. SetValue(2, value.x);
  69. SetValue(3, value.y);
  70. }
  71. }
  72. public FourCC format
  73. {
  74. get { return new FourCC('H', 'T', 'M', 'L'); }
  75. }
  76. public WebGLGamepadState WithButton(GamepadButton button, float value = 1)
  77. {
  78. int index;
  79. switch (button)
  80. {
  81. case GamepadButton.South: index = 0; break;
  82. case GamepadButton.East: index = 1; break;
  83. case GamepadButton.West: index = 2; break;
  84. case GamepadButton.North: index = 3; break;
  85. case GamepadButton.LeftShoulder: index = 4; break;
  86. case GamepadButton.RightShoulder: index = 5; break;
  87. case GamepadButton.Select: index = 8; break;
  88. case GamepadButton.Start: index = 9; break;
  89. case GamepadButton.LeftStick: index = 10; break;
  90. case GamepadButton.RightStick: index = 11; break;
  91. case GamepadButton.DpadUp: index = 12; break;
  92. case GamepadButton.DpadDown: index = 13; break;
  93. case GamepadButton.DpadLeft: index = 14; break;
  94. case GamepadButton.DpadRight: index = 15; break;
  95. default:
  96. throw new InvalidEnumArgumentException(nameof(button), (int)button, typeof(GamepadButton));
  97. }
  98. SetValue(NumAxes + index, value);
  99. return this;
  100. }
  101. private float GetValue(int index)
  102. {
  103. fixed(float* valuePtr = values)
  104. return valuePtr[index];
  105. }
  106. private void SetValue(int index, float value)
  107. {
  108. fixed(float* valuePtr = values)
  109. valuePtr[index] = value;
  110. }
  111. }
  112. [Serializable]
  113. internal struct WebGLDeviceCapabilities
  114. {
  115. public int numAxes;
  116. public int numButtons;
  117. public string mapping;
  118. public string ToJson()
  119. {
  120. return JsonUtility.ToJson(this);
  121. }
  122. public static WebGLDeviceCapabilities FromJson(string json)
  123. {
  124. if (string.IsNullOrEmpty(json))
  125. throw new ArgumentNullException(nameof(json));
  126. return JsonUtility.FromJson<WebGLDeviceCapabilities>(json);
  127. }
  128. }
  129. }
  130. namespace UnityEngine.InputSystem.WebGL
  131. {
  132. /// <summary>
  133. /// Gamepad on WebGL that uses the "standard" mapping.
  134. /// </summary>
  135. /// <seealso href="https://w3c.github.io/gamepad/#remapping"/>
  136. [InputControlLayout(stateType = typeof(WebGLGamepadState), displayName = "WebGL Gamepad (\"standard\" mapping)")]
  137. public class WebGLGamepad : Gamepad
  138. {
  139. }
  140. }
  141. #endif // UNITY_WEBGL || UNITY_EDITOR