2020-10-09 00:07:39 +08:00
|
|
|
|
/// Credit SimonDarksideJ
|
|
|
|
|
/// Sourced from: https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/348/menu-manager-does-not-work-with-the-new
|
|
|
|
|
|
2020-10-13 06:27:57 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-10-14 23:21:26 +08:00
|
|
|
|
|
|
|
|
|
#if !ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-13 06:27:57 +08:00
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
using UnityEngine.InputSystem.Controls;
|
2020-10-14 23:21:26 +08:00
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class UIExtensionsInputManager
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if !ENABLE_LEGACY_INPUT_MANAGER
|
|
|
|
|
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
|
|
|
|
|
|
2020-10-09 00:07:39 +08:00
|
|
|
|
public static bool GetMouseButton(int button)
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-09 00:07:39 +08:00
|
|
|
|
return Input.GetMouseButton(button);
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#else
|
|
|
|
|
if (Mouse.current == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Mouse.current.leftButton.isPressed;
|
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool GetMouseButtonDown(int button)
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-09 00:07:39 +08:00
|
|
|
|
return Input.GetMouseButtonDown(button);
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#else
|
|
|
|
|
if (Mouse.current == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Mouse.current.leftButton.isPressed)
|
|
|
|
|
{
|
|
|
|
|
if (!mouseButtons[button])
|
|
|
|
|
{
|
|
|
|
|
mouseButtons[button] = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool GetMouseButtonUp(int button)
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-09 00:07:39 +08:00
|
|
|
|
return Input.GetMouseButtonUp(button);
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#else
|
|
|
|
|
if (Mouse.current == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mouseButtons[button] && !Mouse.current.leftButton.isPressed)
|
|
|
|
|
{
|
|
|
|
|
mouseButtons[button] = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool GetButton(string input)
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-09 00:07:39 +08:00
|
|
|
|
return Input.GetButton(input);
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#else
|
|
|
|
|
ButtonControl buttonPressed = GetButtonControlFromString(input);
|
|
|
|
|
|
|
|
|
|
if (!buttons.ContainsKey(input))
|
|
|
|
|
{
|
|
|
|
|
buttons.Add(input, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buttonPressed != null ? buttonPressed.isPressed : false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 23:21:26 +08:00
|
|
|
|
#if !ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-13 06:27:57 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2020-10-09 00:07:39 +08:00
|
|
|
|
}
|
2020-10-14 23:21:26 +08:00
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
|
|
|
|
|
public static bool GetButtonDown(string input)
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-09 00:07:39 +08:00
|
|
|
|
return Input.GetButtonDown(input);
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#else
|
|
|
|
|
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;
|
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool GetButtonUp(string input)
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-09 00:07:39 +08:00
|
|
|
|
return Input.GetButtonUp(input);
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#else
|
|
|
|
|
ButtonControl buttonPressed = GetButtonControlFromString(input);
|
|
|
|
|
|
|
|
|
|
if (buttons[input] && !buttonPressed.isPressed)
|
|
|
|
|
{
|
|
|
|
|
buttons[input] = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool GetKey(KeyCode key)
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-09 00:07:39 +08:00
|
|
|
|
return Input.GetKey(key);
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#else
|
|
|
|
|
KeyControl keyPressed = GetKeyControlFromKeyCode(key);
|
|
|
|
|
if (!keys.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
keys.Add(key, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return keyPressed != null ? keyPressed.isPressed : false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 23:21:26 +08:00
|
|
|
|
#if !ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-13 06:27:57 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2020-10-09 00:07:39 +08:00
|
|
|
|
}
|
2020-10-14 23:21:26 +08:00
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
|
|
|
|
|
public static bool GetKeyDown(KeyCode key)
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-09 00:07:39 +08:00
|
|
|
|
return Input.GetKeyDown(key);
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#else
|
|
|
|
|
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;
|
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool GetKeyUp(KeyCode key)
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-09 00:07:39 +08:00
|
|
|
|
return Input.GetKeyUp(key);
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#else
|
|
|
|
|
KeyControl keyPressed = GetKeyControlFromKeyCode(key);
|
|
|
|
|
if (keys[key] && !keyPressed.isPressed)
|
|
|
|
|
{
|
|
|
|
|
keys[key] = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float GetAxisRaw(string axis)
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-09 00:07:39 +08:00
|
|
|
|
return Input.GetAxisRaw(axis);
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#else
|
|
|
|
|
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;
|
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector3 MousePosition
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
2020-10-09 00:07:39 +08:00
|
|
|
|
return Input.mousePosition;
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#else
|
|
|
|
|
return Mouse.current.position.ReadValue();
|
2020-10-15 01:03:49 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector3 MouseScrollDelta
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
|
|
|
return Input.mouseScrollDelta;
|
|
|
|
|
#else
|
|
|
|
|
return Mouse.current.position.ReadValue();
|
2020-10-13 06:27:57 +08:00
|
|
|
|
#endif
|
2020-10-09 00:07:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|