AbstractInputSystem.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using UnityEngine;
  2. #if ENABLE_INPUT_SYSTEM
  3. using UnityEngine.InputSystem;
  4. #endif
  5. namespace TriLibCore.Samples
  6. {
  7. /// <summary>
  8. /// Represents a class to abstract input system actions.
  9. /// </summary>
  10. public class AbstractInputSystem : MonoBehaviour
  11. {
  12. private float _lastMultiTouchDistance;
  13. /// <summary>
  14. /// Helper method to get a mouse button using the legacy and new input systems.
  15. /// </summary>
  16. protected bool GetMouseButton(int index)
  17. {
  18. #if ENABLE_INPUT_SYSTEM
  19. if (Mouse.current != null)
  20. {
  21. switch (index)
  22. {
  23. case 0:
  24. return Mouse.current.leftButton.isPressed;
  25. case 1:
  26. return Mouse.current.rightButton.isPressed;
  27. case 2:
  28. return Mouse.current.middleButton.isPressed;
  29. }
  30. }
  31. return false;
  32. #else
  33. if (SystemInfo.deviceType == DeviceType.Handheld && Input.touchSupported && Input.touchCount > 1 && index == 2)
  34. {
  35. return true;
  36. }
  37. return Input.GetMouseButton(index);
  38. #endif
  39. }
  40. /// <summary>
  41. /// Helper method to get a mouse button down using the legacy and new input systems.
  42. /// </summary>
  43. protected bool GetMouseButtonDown(int index)
  44. {
  45. #if ENABLE_INPUT_SYSTEM
  46. if (Mouse.current != null)
  47. {
  48. switch (index)
  49. {
  50. case 0:
  51. return Mouse.current.leftButton.wasPressedThisFrame;
  52. case 1:
  53. return Mouse.current.middleButton.wasPressedThisFrame;
  54. case 2:
  55. return Mouse.current.rightButton.wasPressedThisFrame;
  56. }
  57. }
  58. return false;
  59. #else
  60. return Input.GetMouseButtonDown(index);
  61. #endif
  62. }
  63. /// <summary>
  64. /// Helper method to get an axis value using the legacy and new input systems.
  65. /// </summary>
  66. protected float GetAxis(string axisName)
  67. {
  68. #if ENABLE_INPUT_SYSTEM
  69. switch (axisName)
  70. {
  71. case "Mouse X":
  72. return Mouse.current != null ? Mouse.current.delta.x.ReadValue() : 0f;
  73. case "Mouse Y":
  74. return Mouse.current != null ? Mouse.current.delta.y.ReadValue() : 0f;
  75. case "Horizontal":
  76. if (Keyboard.current == null)
  77. {
  78. return 0f;
  79. }
  80. return Keyboard.current.leftArrowKey.isPressed ? -1f :
  81. Keyboard.current.rightArrowKey.isPressed ? 1f : 0f;
  82. case "Vertical":
  83. if (Keyboard.current == null)
  84. {
  85. return 0f;
  86. }
  87. return Keyboard.current.downArrowKey.isPressed ? -1f :
  88. Keyboard.current.upArrowKey.isPressed ? 1f : 0f;
  89. default:
  90. return 0f;
  91. }
  92. #else
  93. if (SystemInfo.deviceType == DeviceType.Handheld && Input.touchSupported)
  94. {
  95. if (Input.touchCount > 0)
  96. {
  97. if (axisName == "Mouse X")
  98. {
  99. return Input.touches[0].deltaPosition.x * 0.05f;
  100. }
  101. if (axisName == "Mouse Y")
  102. {
  103. return Input.touches[0].deltaPosition.y * 0.05f;
  104. }
  105. }
  106. return 0f;
  107. }
  108. return Input.GetAxis(axisName);
  109. #endif
  110. }
  111. /// <summary>
  112. /// Helper method to return a keyboard key using the legacy and new input systems.
  113. /// </summary>
  114. protected bool GetKey(KeyCode keyCode)
  115. {
  116. #if ENABLE_INPUT_SYSTEM
  117. if (Keyboard.current != null)
  118. {
  119. switch (keyCode)
  120. {
  121. case KeyCode.LeftAlt:
  122. return Keyboard.current.leftAltKey.isPressed;
  123. case KeyCode.RightAlt:
  124. return Keyboard.current.rightAltKey.isPressed;
  125. }
  126. }
  127. return false;
  128. #else
  129. return Input.GetKey(keyCode);
  130. #endif
  131. }
  132. /// <summary>
  133. /// Helper method to return the mouse scroll delta using the legacy and new input systems.
  134. /// </summary>
  135. protected Vector2 GetMouseScrollDelta()
  136. {
  137. #if ENABLE_INPUT_SYSTEM
  138. return Mouse.current != null ? Mouse.current.scroll.ReadValue() * 0.01f: default;
  139. #else
  140. if (SystemInfo.deviceType == DeviceType.Handheld && Input.touchSupported && Input.touchCount == 2)
  141. {
  142. var firstTouch = Input.touches[0];
  143. var secondTouch = Input.touches[1];
  144. if (firstTouch.phase == TouchPhase.Began || secondTouch.phase == TouchPhase.Began)
  145. {
  146. _lastMultiTouchDistance = Vector2.Distance(firstTouch.position, secondTouch.position);
  147. }
  148. if (firstTouch.phase != TouchPhase.Moved || secondTouch.phase != TouchPhase.Moved)
  149. {
  150. return Vector2.zero;
  151. }
  152. var newMultiTouchDistance = Vector2.Distance(firstTouch.position, secondTouch.position);
  153. var deltaMultiTouchDistance = newMultiTouchDistance - _lastMultiTouchDistance;
  154. _lastMultiTouchDistance = newMultiTouchDistance;
  155. return new Vector2(0f, deltaMultiTouchDistance * 0.05f);
  156. }
  157. return Input.mouseScrollDelta;
  158. #endif
  159. }
  160. }
  161. }