| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- /// Credit SimonDarksideJ
- /// Sourced from: https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/348/menu-manager-does-not-work-with-the-new
- #if UNITY_2019_1_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
- #define NEW_INPUT_SYSTEM
- #endif
- using System;
- using System.Collections.Generic;
- #if NEW_INPUT_SYSTEM
- using UnityEngine.InputSystem;
- using UnityEngine.InputSystem.Controls;
- #endif
- namespace UnityEngine.UI.Extensions
- {
- public static class UIExtensionsInputManager
- {
- #if NEW_INPUT_SYSTEM
- private static bool[] mouseButtons = new bool[3] { false, false, false };
- private static Dictionary<KeyCode, bool> keys = new Dictionary<KeyCode, bool>();
- private static Dictionary<String, bool> buttons = new Dictionary<String, bool>();
- #endif
- public static bool GetMouseButton(int button)
- {
- #if NEW_INPUT_SYSTEM
- if (Mouse.current == null)
- {
- return false;
- }
- return Mouse.current.leftButton.isPressed;
- #else
- return Input.GetMouseButton(button);
- #endif
- }
- public static bool GetMouseButtonDown(int button)
- {
- #if NEW_INPUT_SYSTEM
- if (Mouse.current == null)
- {
- return false;
- }
- if (Mouse.current.leftButton.isPressed)
- {
- if (!mouseButtons[button])
- {
- mouseButtons[button] = true;
- return true;
- }
- }
- return false;
- #else
- return Input.GetMouseButtonDown(button);
- #endif
- }
- public static bool GetMouseButtonUp(int button)
- {
- #if NEW_INPUT_SYSTEM
- if (Mouse.current == null)
- {
- return false;
- }
- if (mouseButtons[button] && !Mouse.current.leftButton.isPressed)
- {
- mouseButtons[button] = false;
- return true;
- }
- return false;
- #else
- return Input.GetMouseButtonUp(button);
- #endif
- }
- public static bool GetButton(string input)
- {
- #if NEW_INPUT_SYSTEM
- ButtonControl buttonPressed = GetButtonControlFromString(input);
- if (!buttons.ContainsKey(input))
- {
- buttons.Add(input, false);
- }
- return buttonPressed != null ? buttonPressed.isPressed : false;
- #else
- return Input.GetButton(input);
- #endif
- }
- #if NEW_INPUT_SYSTEM
- private static ButtonControl GetButtonControlFromString(string input)
- {
- if (Gamepad.current == null)
- {
- return null;
- }
- switch (input)
- {
- case "Submit":
- return Gamepad.current.aButton;
- case "Cancel":
- return Gamepad.current.bButton;
- default:
- return null;
- }
- }
- #endif
- public static bool GetButtonDown(string input)
- {
- #if NEW_INPUT_SYSTEM
- ButtonControl buttonPressed = GetButtonControlFromString(input);
- if (buttonPressed.isPressed)
- {
- if (!buttons.ContainsKey(input))
- {
- buttons.Add(input, false);
- }
- if (!buttons[input])
- {
- buttons[input] = true;
- return true;
- }
- }
- else
- {
- buttons[input] = false;
- }
- return false;
- #else
- return Input.GetButtonDown(input);
- #endif
- }
- public static bool GetButtonUp(string input)
- {
- #if NEW_INPUT_SYSTEM
- ButtonControl buttonPressed = GetButtonControlFromString(input);
- if (buttons[input] && !buttonPressed.isPressed)
- {
- buttons[input] = false;
- return true;
- }
- return false;
- #else
- return Input.GetButtonUp(input);
- #endif
- }
- public static bool GetKey(KeyCode key)
- {
- #if NEW_INPUT_SYSTEM
- KeyControl keyPressed = GetKeyControlFromKeyCode(key);
- if (!keys.ContainsKey(key))
- {
- keys.Add(key, false);
- }
- return keyPressed != null ? keyPressed.isPressed : false;
- #else
- return Input.GetKey(key);
- #endif
- }
- #if NEW_INPUT_SYSTEM
- private static KeyControl GetKeyControlFromKeyCode(KeyCode key)
- {
- if (Keyboard.current == null)
- {
- return null;
- }
- switch (key)
- {
- case KeyCode.Escape:
- return Keyboard.current.escapeKey;
- case KeyCode.KeypadEnter:
- return Keyboard.current.numpadEnterKey;
- case KeyCode.UpArrow:
- return Keyboard.current.upArrowKey;
- case KeyCode.DownArrow:
- return Keyboard.current.downArrowKey;
- case KeyCode.RightArrow:
- return Keyboard.current.rightArrowKey;
- case KeyCode.LeftArrow:
- return Keyboard.current.leftArrowKey;
- case KeyCode.LeftShift:
- return Keyboard.current.leftShiftKey;
- case KeyCode.Tab:
- return Keyboard.current.tabKey;
- default:
- return null;
- }
- }
- #endif
- public static bool GetKeyDown(KeyCode key)
- {
- #if NEW_INPUT_SYSTEM
- KeyControl keyPressed = GetKeyControlFromKeyCode(key);
- if (keyPressed.isPressed)
- {
- if (!keys.ContainsKey(key))
- {
- keys.Add(key, false);
- }
- if (!keys[key])
- {
- keys[key] = true;
- return true;
- }
- }
- else
- {
- keys[key] = false;
- }
- return false;
- #else
- return Input.GetKeyDown(key);
- #endif
- }
- public static bool GetKeyUp(KeyCode key)
- {
- #if NEW_INPUT_SYSTEM
- KeyControl keyPressed = GetKeyControlFromKeyCode(key);
- if (keys[key] && !keyPressed.isPressed)
- {
- keys[key] = false;
- return true;
- }
- return false;
- #else
- return Input.GetKeyUp(key);
- #endif
- }
- public static float GetAxisRaw(string axis)
- {
- #if NEW_INPUT_SYSTEM
- if (Gamepad.current == null)
- {
- return 0f;
- }
- switch (axis)
- {
- case "Horizontal":
- return Gamepad.current.leftStick.x.ReadValue();
- case "Vertical":
- return Gamepad.current.leftStick.y.ReadValue();
- }
- return 0f;
- #else
- return Input.GetAxisRaw(axis);
- #endif
- }
- public static Vector3 MousePosition
- {
- get
- {
- #if NEW_INPUT_SYSTEM
- return Mouse.current.position.ReadValue();
- #else
- return Input.mousePosition;
- #endif
- }
- }
- public static Vector3 MouseScrollDelta
- {
- get
- {
- #if NEW_INPUT_SYSTEM
- return Mouse.current.position.ReadValue();
- #else
- return Input.mouseScrollDelta;
- #endif
- }
- }
- }
- }
|