MazeInput.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using UnityEngine;
  2. #if ENABLE_INPUT_SYSTEM
  3. using UnityEngine.InputSystem;
  4. #endif
  5. namespace MazeGen
  6. {
  7. /// <summary>
  8. /// Small input-backend-agnostic helpers. This project has the Input System package
  9. /// installed, and Active Input Handling may be set to "Input System Package (New)"
  10. /// only - which silently breaks legacy-only APIs like OnMouseDown/Input.GetKey. These
  11. /// helpers pick the right backend at compile time via the ENABLE_INPUT_SYSTEM symbol
  12. /// Unity defines automatically when the package is present.
  13. /// </summary>
  14. public static class MazeInput
  15. {
  16. public static bool WasLeftClickedThisFrame()
  17. {
  18. #if ENABLE_INPUT_SYSTEM
  19. return Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame;
  20. #else
  21. return Input.GetMouseButtonDown(0);
  22. #endif
  23. }
  24. public static bool IsMiddleMouseHeld()
  25. {
  26. #if ENABLE_INPUT_SYSTEM
  27. return Mouse.current != null && Mouse.current.middleButton.isPressed;
  28. #else
  29. return Input.GetMouseButton(2);
  30. #endif
  31. }
  32. public static Vector2 GetPointerScreenPosition()
  33. {
  34. #if ENABLE_INPUT_SYSTEM
  35. return Mouse.current != null ? Mouse.current.position.ReadValue() : Vector2.zero;
  36. #else
  37. return Input.mousePosition;
  38. #endif
  39. }
  40. /// <summary>Mouse wheel movement this frame, normalized to roughly "notches" on both input backends (positive = scroll up/away from the user).</summary>
  41. public static float GetScrollDelta()
  42. {
  43. #if ENABLE_INPUT_SYSTEM
  44. // The new Input System reports raw OS wheel delta (~120 per notch on Windows),
  45. // not pre-normalized like the legacy API - scale it down to match.
  46. return Mouse.current != null ? Mouse.current.scroll.ReadValue().y / 120f : 0f;
  47. #else
  48. return Input.mouseScrollDelta.y;
  49. #endif
  50. }
  51. /// <summary>Normalized WASD movement axis: (right/left, up/down).</summary>
  52. public static Vector2 ReadWasdAxis()
  53. {
  54. float x = 0f, y = 0f;
  55. #if ENABLE_INPUT_SYSTEM
  56. var kb = Keyboard.current;
  57. if (kb != null)
  58. {
  59. if (kb.aKey.isPressed) x -= 1f;
  60. if (kb.dKey.isPressed) x += 1f;
  61. if (kb.sKey.isPressed) y -= 1f;
  62. if (kb.wKey.isPressed) y += 1f;
  63. }
  64. #else
  65. if (Input.GetKey(KeyCode.A)) x -= 1f;
  66. if (Input.GetKey(KeyCode.D)) x += 1f;
  67. if (Input.GetKey(KeyCode.S)) y -= 1f;
  68. if (Input.GetKey(KeyCode.W)) y += 1f;
  69. #endif
  70. Vector2 v = new Vector2(x, y);
  71. return v.sqrMagnitude > 1f ? v.normalized : v;
  72. }
  73. }
  74. }