// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik // using UnityEngine; #if UNITY_UGUI using UnityEngine.EventSystems; #endif #if UNITY_INPUT_SYSTEM using UnityEngine.InputSystem; #endif namespace Animancer.Samples { /// /// A standard wrapper for receiving input from the /// Input System package or the /// Legacy Input Manager. /// /// Documentation: /// /// Input /// /// https://kybernetik.com.au/animancer/api/Animancer.Samples/SampleInput /// [AnimancerHelpUrl(typeof(SampleInput))] public static class SampleInput { /************************************************************************************************************************/ /// The current screen position of the mouse pointer. public static Vector2 MousePosition #if UNITY_INPUT_SYSTEM => Mouse.current.position.ReadValue(); #else => Input.mousePosition; #endif /// The amount that the mouse has moved since last frame. public static Vector2 MousePositionDelta #if UNITY_INPUT_SYSTEM => Mouse.current.delta.ReadValue(); #else => new(Input.GetAxisRaw("Mouse X") * 20, Input.GetAxisRaw("Mouse Y") * 20); #endif /// The amount that the mouse scroll value has changed since last frame. public static Vector2 MouseScrollDelta #if UNITY_INPUT_SYSTEM => Mouse.current.scroll.ReadValue() * 0.01f; #else => Input.mouseScrollDelta; #endif /************************************************************************************************************************/ /// Is the mouse pointer currently over a UI object? public static bool IsPointerOverUI #if UNITY_UGUI => EventSystem.current != null && EventSystem.current.IsPointerOverGameObject(); #else => false; #endif /************************************************************************************************************************/ /// Was the left mouse button pressed this frame? public static bool LeftMouseDown #if UNITY_INPUT_SYSTEM => !IsPointerOverUI && Mouse.current.leftButton.wasPressedThisFrame; #else => Input.GetMouseButtonDown(0); #endif /// Is the left mouse button currently being held down? public static bool LeftMouseHold #if UNITY_INPUT_SYSTEM => Mouse.current.leftButton.isPressed; #else => Input.GetMouseButton(0); #endif /// Was the left mouse button released this frame? public static bool LeftMouseUp #if UNITY_INPUT_SYSTEM => !IsPointerOverUI && Mouse.current.leftButton.wasReleasedThisFrame; #else => !IsPointerOverUI && Input.GetMouseButtonUp(0); #endif /************************************************************************************************************************/ /// Was the right mouse button pressed this frame? public static bool RightMouseDown #if UNITY_INPUT_SYSTEM => !IsPointerOverUI && Mouse.current.rightButton.wasPressedThisFrame; #else => !IsPointerOverUI && Input.GetMouseButtonDown(1); #endif /// Is the right mouse button currently being held down? public static bool RightMouseHold #if UNITY_INPUT_SYSTEM => Mouse.current.rightButton.isPressed; #else => Input.GetMouseButton(1); #endif /************************************************************************************************************************/ /// Was pressed this frame? public static bool SpaceDown #if UNITY_INPUT_SYSTEM => Keyboard.current.spaceKey.wasPressedThisFrame; #else => Input.GetKeyDown(KeyCode.Space); #endif /// Is currently being held down? public static bool SpaceHold #if UNITY_INPUT_SYSTEM => Keyboard.current.spaceKey.isPressed; #else => Input.GetKey(KeyCode.Space); #endif /// Was released this frame? public static bool SpaceUp #if UNITY_INPUT_SYSTEM => Keyboard.current.spaceKey.wasReleasedThisFrame; #else => Input.GetKeyUp(KeyCode.Space); #endif /************************************************************************************************************************/ /// Is currently being held down? public static bool LeftShiftHold #if UNITY_INPUT_SYSTEM => Keyboard.current.leftShiftKey.isPressed; #else => Input.GetKey(KeyCode.LeftShift); #endif /************************************************************************************************************************/ /// Was released this frame? public static bool Number1Up #if UNITY_INPUT_SYSTEM => Keyboard.current.digit1Key.wasReleasedThisFrame; #else => Input.GetKeyUp(KeyCode.Alpha1); #endif /// Was released this frame? public static bool Number2Up #if UNITY_INPUT_SYSTEM => Keyboard.current.digit2Key.wasReleasedThisFrame; #else => Input.GetKeyUp(KeyCode.Alpha2); #endif /************************************************************************************************************************/ #if UNITY_INPUT_SYSTEM private static InputAction _WasdAction; #endif /// WASD Controls. public static Vector2 WASD #if UNITY_INPUT_SYSTEM { get { if (_WasdAction == null) { _WasdAction = new(nameof(WASD), InputActionType.Value); _WasdAction.AddCompositeBinding("2DVector") .With("Up", "/w") .With("Down", "/s") .With("Left", "/a") .With("Right", "/d"); } _WasdAction.Enable(); return _WasdAction.ReadValue(); } } #else => new(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); #endif /************************************************************************************************************************/ } }