SampleInput.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using UnityEngine;
  3. #if UNITY_UGUI
  4. using UnityEngine.EventSystems;
  5. #endif
  6. #if UNITY_INPUT_SYSTEM
  7. using UnityEngine.InputSystem;
  8. #endif
  9. namespace Animancer.Samples
  10. {
  11. /// <summary>
  12. /// A standard wrapper for receiving input from the
  13. /// <see href="https://docs.unity3d.com/Packages/com.unity.inputsystem@latest">Input System</see> package or the
  14. /// <see href="https://docs.unity3d.com/Manual/class-InputManager.html">Legacy Input Manager</see>.
  15. /// <remarks>
  16. /// <strong>Documentation:</strong>
  17. /// <see href="https://kybernetik.com.au/animancer/docs/samples/basics/input">
  18. /// Input</see>
  19. /// </remarks>
  20. /// https://kybernetik.com.au/animancer/api/Animancer.Samples/SampleInput
  21. ///
  22. [AnimancerHelpUrl(typeof(SampleInput))]
  23. public static class SampleInput
  24. {
  25. /************************************************************************************************************************/
  26. /// <summary>The current screen position of the mouse pointer.</summary>
  27. public static Vector2 MousePosition
  28. #if UNITY_INPUT_SYSTEM
  29. => Mouse.current.position.ReadValue();
  30. #else
  31. => Input.mousePosition;
  32. #endif
  33. /// <summary>The amount that the mouse has moved since last frame.</summary>
  34. public static Vector2 MousePositionDelta
  35. #if UNITY_INPUT_SYSTEM
  36. => Mouse.current.delta.ReadValue();
  37. #else
  38. => new(Input.GetAxisRaw("Mouse X") * 20, Input.GetAxisRaw("Mouse Y") * 20);
  39. #endif
  40. /// <summary>The amount that the mouse scroll value has changed since last frame.</summary>
  41. public static Vector2 MouseScrollDelta
  42. #if UNITY_INPUT_SYSTEM
  43. => Mouse.current.scroll.ReadValue() * 0.01f;
  44. #else
  45. => Input.mouseScrollDelta;
  46. #endif
  47. /************************************************************************************************************************/
  48. /// <summary>Is the mouse pointer currently over a UI object?</summary>
  49. public static bool IsPointerOverUI
  50. #if UNITY_UGUI
  51. => EventSystem.current != null
  52. && EventSystem.current.IsPointerOverGameObject();
  53. #else
  54. => false;
  55. #endif
  56. /************************************************************************************************************************/
  57. /// <summary>Was the left mouse button pressed this frame?</summary>
  58. public static bool LeftMouseDown
  59. #if UNITY_INPUT_SYSTEM
  60. => !IsPointerOverUI
  61. && Mouse.current.leftButton.wasPressedThisFrame;
  62. #else
  63. => Input.GetMouseButtonDown(0);
  64. #endif
  65. /// <summary>Is the left mouse button currently being held down?</summary>
  66. public static bool LeftMouseHold
  67. #if UNITY_INPUT_SYSTEM
  68. => Mouse.current.leftButton.isPressed;
  69. #else
  70. => Input.GetMouseButton(0);
  71. #endif
  72. /// <summary>Was the left mouse button released this frame?</summary>
  73. public static bool LeftMouseUp
  74. #if UNITY_INPUT_SYSTEM
  75. => !IsPointerOverUI
  76. && Mouse.current.leftButton.wasReleasedThisFrame;
  77. #else
  78. => !IsPointerOverUI
  79. && Input.GetMouseButtonUp(0);
  80. #endif
  81. /************************************************************************************************************************/
  82. /// <summary>Was the right mouse button pressed this frame?</summary>
  83. public static bool RightMouseDown
  84. #if UNITY_INPUT_SYSTEM
  85. => !IsPointerOverUI
  86. && Mouse.current.rightButton.wasPressedThisFrame;
  87. #else
  88. => !IsPointerOverUI
  89. && Input.GetMouseButtonDown(1);
  90. #endif
  91. /// <summary>Is the right mouse button currently being held down?</summary>
  92. public static bool RightMouseHold
  93. #if UNITY_INPUT_SYSTEM
  94. => Mouse.current.rightButton.isPressed;
  95. #else
  96. => Input.GetMouseButton(1);
  97. #endif
  98. /************************************************************************************************************************/
  99. /// <summary>Was <see cref="KeyCode.Space"/> pressed this frame?</summary>
  100. public static bool SpaceDown
  101. #if UNITY_INPUT_SYSTEM
  102. => Keyboard.current.spaceKey.wasPressedThisFrame;
  103. #else
  104. => Input.GetKeyDown(KeyCode.Space);
  105. #endif
  106. /// <summary>Is <see cref="KeyCode.Space"/> currently being held down?</summary>
  107. public static bool SpaceHold
  108. #if UNITY_INPUT_SYSTEM
  109. => Keyboard.current.spaceKey.isPressed;
  110. #else
  111. => Input.GetKey(KeyCode.Space);
  112. #endif
  113. /// <summary>Was <see cref="KeyCode.Space"/> released this frame?</summary>
  114. public static bool SpaceUp
  115. #if UNITY_INPUT_SYSTEM
  116. => Keyboard.current.spaceKey.wasReleasedThisFrame;
  117. #else
  118. => Input.GetKeyUp(KeyCode.Space);
  119. #endif
  120. /************************************************************************************************************************/
  121. /// <summary>Is <see cref="KeyCode.LeftShift"/> currently being held down?</summary>
  122. public static bool LeftShiftHold
  123. #if UNITY_INPUT_SYSTEM
  124. => Keyboard.current.leftShiftKey.isPressed;
  125. #else
  126. => Input.GetKey(KeyCode.LeftShift);
  127. #endif
  128. /************************************************************************************************************************/
  129. /// <summary>Was <see cref="KeyCode.Alpha1"/> released this frame?</summary>
  130. public static bool Number1Up
  131. #if UNITY_INPUT_SYSTEM
  132. => Keyboard.current.digit1Key.wasReleasedThisFrame;
  133. #else
  134. => Input.GetKeyUp(KeyCode.Alpha1);
  135. #endif
  136. /// <summary>Was <see cref="KeyCode.Alpha2"/> released this frame?</summary>
  137. public static bool Number2Up
  138. #if UNITY_INPUT_SYSTEM
  139. => Keyboard.current.digit2Key.wasReleasedThisFrame;
  140. #else
  141. => Input.GetKeyUp(KeyCode.Alpha2);
  142. #endif
  143. /************************************************************************************************************************/
  144. #if UNITY_INPUT_SYSTEM
  145. private static InputAction _WasdAction;
  146. #endif
  147. /// <summary>WASD Controls.</summary>
  148. public static Vector2 WASD
  149. #if UNITY_INPUT_SYSTEM
  150. {
  151. get
  152. {
  153. if (_WasdAction == null)
  154. {
  155. _WasdAction = new(nameof(WASD), InputActionType.Value);
  156. _WasdAction.AddCompositeBinding("2DVector")
  157. .With("Up", "<Keyboard>/w")
  158. .With("Down", "<Keyboard>/s")
  159. .With("Left", "<Keyboard>/a")
  160. .With("Right", "<Keyboard>/d");
  161. }
  162. _WasdAction.Enable();
  163. return _WasdAction.ReadValue<Vector2>();
  164. }
  165. }
  166. #else
  167. => new(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  168. #endif
  169. /************************************************************************************************************************/
  170. }
  171. }