using UnityEngine; #if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER using UnityEngine.InputSystem; #endif public enum InputSourceType { None, Mouse, Touch } public struct InputEvent { public bool IsPressed; // true = down, false = up public Vector3 Position; // pointer/touch position public InputSourceType Source; // Mouse or Touch } public static class CrossPlatformInput { /// /// Detects if a primary press just began this frame (mouse or touch). /// public static InputEvent GetPrimaryDown() { #if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER // New Input System if (Touchscreen.current != null && Touchscreen.current.primaryTouch.press.wasPressedThisFrame) return new InputEvent { IsPressed = true, Position = Touchscreen.current.primaryTouch.position.ReadValue(), Source = InputSourceType.Touch }; if (Mouse.current?.leftButton.wasPressedThisFrame == true) return new InputEvent { IsPressed = true, Position = Mouse.current.position.ReadValue(), Source = InputSourceType.Mouse }; return default; #else // Legacy Input Manager if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) return new InputEvent { IsPressed = true, Position = Input.GetTouch(0).position, Source = InputSourceType.Touch }; if (Input.GetMouseButtonDown(0)) return new InputEvent { IsPressed = true, Position = Input.mousePosition, Source = InputSourceType.Mouse }; return default; #endif } /// /// Detects if a primary press just ended this frame (mouse or touch). /// public static InputEvent GetPrimaryUp() { #if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER // New Input System if (Touchscreen.current != null && Touchscreen.current.primaryTouch.press.wasReleasedThisFrame) return new InputEvent { IsPressed = false, Position = Touchscreen.current.primaryTouch.position.ReadValue(), Source = InputSourceType.Touch }; if (Mouse.current?.leftButton.wasReleasedThisFrame == true) return new InputEvent { IsPressed = false, Position = Mouse.current.position.ReadValue(), Source = InputSourceType.Mouse }; return default; #else // Legacy Input Manager if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) return new InputEvent { IsPressed = false, Position = Input.GetTouch(0).position, Source = InputSourceType.Touch }; if (Input.GetMouseButtonUp(0)) return new InputEvent { IsPressed = false, Position = Input.mousePosition, Source = InputSourceType.Mouse }; return default; #endif } /// /// Gets current position of primary pointer (mouse or touch). /// Returns Vector3.zero if no input is active. /// public static Vector3 GetPrimaryPosition() { #if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER if (Touchscreen.current != null) return Touchscreen.current.primaryTouch.position.ReadValue(); if (Mouse.current != null) return Mouse.current.position.ReadValue(); return Vector3.zero; #else if (Input.touchCount > 0) return Input.GetTouch(0).position; return Input.mousePosition; #endif } }