InputProxy.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using UnityEngine;
  2. #if ENABLE_INPUT_SYSTEM
  3. using UnityEngine.InputSystem;
  4. using UnityEngine.InputSystem.Controls;
  5. using EnhancedTouch = UnityEngine.InputSystem.EnhancedTouch;
  6. #endif
  7. namespace HighlightPlus {
  8. public static class InputProxy {
  9. #if ENABLE_INPUT_SYSTEM
  10. static Vector3 lastPointerPosition;
  11. public static void Init() {
  12. if (!EnhancedTouch.EnhancedTouchSupport.enabled) {
  13. EnhancedTouch.EnhancedTouchSupport.Enable();
  14. }
  15. }
  16. public static Vector3 mousePosition {
  17. get {
  18. if (touchCount > 0) {
  19. lastPointerPosition = EnhancedTouch.Touch.activeTouches[0].screenPosition;
  20. } else {
  21. Mouse m = Mouse.current;
  22. if (m != null) {
  23. lastPointerPosition = m.position.ReadValue();
  24. }
  25. }
  26. return lastPointerPosition;
  27. }
  28. }
  29. public static bool GetMouseButtonDown(int buttonIndex) {
  30. if (touchCount > 0) {
  31. return EnhancedTouch.Touch.activeTouches[0].phase == UnityEngine.InputSystem.TouchPhase.Began;
  32. } else {
  33. Mouse m = Mouse.current;
  34. if (m == null) return false;
  35. switch (buttonIndex) {
  36. case 1: return m.rightButton.wasPressedThisFrame;
  37. case 2: return m.middleButton.wasPressedThisFrame;
  38. default: return m.leftButton.wasPressedThisFrame;
  39. }
  40. }
  41. }
  42. public static int touchCount { get { return EnhancedTouch.Touch.activeTouches.Count; } }
  43. public static int GetFingerIdFromTouch(int touchIndex) {
  44. EnhancedTouch.Touch touch = EnhancedTouch.Touch.activeTouches[touchIndex];
  45. return touch.finger.index;
  46. }
  47. public static bool GetKeyDown(string name) {
  48. return ((KeyControl)Keyboard.current[name]).wasPressedThisFrame;
  49. }
  50. #else
  51. public static void Init() {}
  52. public static Vector3 mousePosition {
  53. get {
  54. return Input.mousePosition;
  55. }
  56. }
  57. public static bool GetMouseButtonDown(int buttonIndex) {
  58. return Input.GetMouseButtonDown(buttonIndex);
  59. }
  60. public static int touchCount {
  61. get { return Input.touchCount; }
  62. }
  63. public static int GetFingerIdFromTouch(int touchIndex) {
  64. return Input.GetTouch(touchIndex).fingerId;
  65. }
  66. public static bool GetKeyDown(string name) {
  67. return Input.GetKeyDown(name);
  68. }
  69. #endif
  70. }
  71. }