MazeInput.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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>Normalized WASD movement axis: (right/left, up/down).</summary>
  41. public static Vector2 ReadWasdAxis()
  42. {
  43. float x = 0f, y = 0f;
  44. #if ENABLE_INPUT_SYSTEM
  45. var kb = Keyboard.current;
  46. if (kb != null)
  47. {
  48. if (kb.aKey.isPressed) x -= 1f;
  49. if (kb.dKey.isPressed) x += 1f;
  50. if (kb.sKey.isPressed) y -= 1f;
  51. if (kb.wKey.isPressed) y += 1f;
  52. }
  53. #else
  54. if (Input.GetKey(KeyCode.A)) x -= 1f;
  55. if (Input.GetKey(KeyCode.D)) x += 1f;
  56. if (Input.GetKey(KeyCode.S)) y -= 1f;
  57. if (Input.GetKey(KeyCode.W)) y += 1f;
  58. #endif
  59. Vector2 v = new Vector2(x, y);
  60. return v.sqrMagnitude > 1f ? v.normalized : v;
  61. }
  62. }
  63. }