ExampleInput.cs 6.4 KB

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