959e80cf72
assets upload description.
298 lines
11 KiB
C#
298 lines
11 KiB
C#
/// <summary>
|
|
/// Created by SWAN DEV
|
|
/// </summary>
|
|
using UnityEngine;
|
|
|
|
#if ENABLE_INPUT_SYSTEM
|
|
using UnityEngine.InputSystem;
|
|
#endif
|
|
|
|
namespace SDev.Util
|
|
{
|
|
public static class UnifyInputUtil
|
|
{
|
|
public static bool IsInputSystemEnabled
|
|
{
|
|
get
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
return true;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
}
|
|
|
|
public static bool IsLegacyInputEnabled
|
|
{
|
|
get
|
|
{
|
|
#if !ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER
|
|
return true;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
}
|
|
|
|
#if ENABLE_INPUT_SYSTEM
|
|
public static bool GetKeyDown(Key key)
|
|
{
|
|
return Keyboard.current[key].wasPressedThisFrame;
|
|
}
|
|
#endif
|
|
public static bool GetKeyDown(KeyCode keyCode)
|
|
{
|
|
#if !ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER
|
|
return Input.GetKeyDown(keyCode);
|
|
#else
|
|
Key key = Key.None;
|
|
switch (keyCode)
|
|
{
|
|
/*
|
|
* Add your Key mapping as needed (* Not all KeyCodes have a corresponding enum in the Key)
|
|
*/
|
|
case KeyCode.Escape: key = Key.Escape; break;
|
|
case KeyCode.Space: key = Key.Space; break;
|
|
case KeyCode.Return: key = Key.Enter; break;
|
|
case KeyCode.Insert: key = Key.Insert; break;
|
|
case KeyCode.Delete: key = Key.Delete; break;
|
|
case KeyCode.Tab: key = Key.Tab; break;
|
|
case KeyCode.AltGr: key = Key.AltGr; break;
|
|
case KeyCode.LeftAlt: key = Key.LeftAlt; break;
|
|
case KeyCode.RightAlt: key = Key.RightAlt; break;
|
|
case KeyCode.LeftShift: key = Key.LeftShift; break;
|
|
case KeyCode.RightShift: key = Key.RightShift; break;
|
|
case KeyCode.LeftControl: key = Key.LeftCtrl; break;
|
|
case KeyCode.RightControl: key = Key.RightCtrl; break;
|
|
case KeyCode.LeftCommand: key = Key.LeftCommand; break;
|
|
case KeyCode.RightCommand: key = Key.RightCommand; break;
|
|
case KeyCode.LeftWindows: key = Key.LeftWindows; break;
|
|
case KeyCode.RightWindows: key = Key.RightWindows; break;
|
|
case KeyCode.Backspace: key = Key.Backspace; break;
|
|
case KeyCode.Numlock: key = Key.NumLock; break;
|
|
case KeyCode.CapsLock: key = Key.CapsLock; break;
|
|
case KeyCode.ScrollLock: key = Key.ScrollLock; break;
|
|
case KeyCode.End: key = Key.End; break;
|
|
case KeyCode.Home: key = Key.Home; break;
|
|
|
|
case KeyCode.DownArrow: key = Key.DownArrow; break;
|
|
case KeyCode.LeftArrow: key = Key.LeftArrow; break;
|
|
case KeyCode.RightArrow: key = Key.RightArrow; break;
|
|
case KeyCode.UpArrow: key = Key.UpArrow; break;
|
|
case KeyCode.PageDown: key = Key.PageDown; break;
|
|
case KeyCode.PageUp: key = Key.PageUp; break;
|
|
|
|
case KeyCode.F1: key = Key.F1; break;
|
|
case KeyCode.F2: key = Key.F2; break;
|
|
case KeyCode.F3: key = Key.F3; break;
|
|
case KeyCode.F4: key = Key.F4; break;
|
|
case KeyCode.F5: key = Key.F5; break;
|
|
case KeyCode.F6: key = Key.F6; break;
|
|
case KeyCode.F7: key = Key.F7; break;
|
|
case KeyCode.F8: key = Key.F8; break;
|
|
case KeyCode.F9: key = Key.F9; break;
|
|
case KeyCode.F10: key = Key.F10; break;
|
|
case KeyCode.F11: key = Key.F11; break;
|
|
case KeyCode.F12: key = Key.F12; break;
|
|
|
|
case KeyCode.Alpha0: key = Key.Digit0; break;
|
|
case KeyCode.Alpha1: key = Key.Digit1; break;
|
|
case KeyCode.Alpha2: key = Key.Digit2; break;
|
|
case KeyCode.Alpha3: key = Key.Digit3; break;
|
|
case KeyCode.Alpha4: key = Key.Digit4; break;
|
|
case KeyCode.Alpha5: key = Key.Digit5; break;
|
|
case KeyCode.Alpha6: key = Key.Digit6; break;
|
|
case KeyCode.Alpha7: key = Key.Digit7; break;
|
|
case KeyCode.Alpha8: key = Key.Digit8; break;
|
|
case KeyCode.Alpha9: key = Key.Digit9; break;
|
|
case KeyCode.Plus: key = Key.Equals; break;
|
|
case KeyCode.Minus: key = Key.Minus; break;
|
|
|
|
case KeyCode.Keypad0: key = Key.Numpad0; break;
|
|
case KeyCode.Keypad1: key = Key.Numpad1; break;
|
|
case KeyCode.Keypad2: key = Key.Numpad2; break;
|
|
case KeyCode.Keypad3: key = Key.Numpad3; break;
|
|
case KeyCode.Keypad4: key = Key.Numpad4; break;
|
|
case KeyCode.Keypad5: key = Key.Numpad5; break;
|
|
case KeyCode.Keypad6: key = Key.Numpad6; break;
|
|
case KeyCode.Keypad7: key = Key.Numpad7; break;
|
|
case KeyCode.Keypad8: key = Key.Numpad8; break;
|
|
case KeyCode.Keypad9: key = Key.Numpad9; break;
|
|
case KeyCode.KeypadEnter: key = Key.NumpadEnter; break;
|
|
case KeyCode.KeypadPlus: key = Key.NumpadPlus; break;
|
|
case KeyCode.KeypadMinus: key = Key.NumpadMinus; break;
|
|
|
|
case KeyCode.A: key = Key.A; break;
|
|
case KeyCode.B: key = Key.B; break;
|
|
case KeyCode.C: key = Key.C; break;
|
|
case KeyCode.D: key = Key.D; break;
|
|
case KeyCode.E: key = Key.E; break;
|
|
case KeyCode.F: key = Key.F; break;
|
|
case KeyCode.G: key = Key.G; break;
|
|
case KeyCode.H: key = Key.H; break;
|
|
case KeyCode.I: key = Key.I; break;
|
|
case KeyCode.J: key = Key.J; break;
|
|
case KeyCode.K: key = Key.K; break;
|
|
case KeyCode.L: key = Key.L; break;
|
|
case KeyCode.M: key = Key.M; break;
|
|
case KeyCode.N: key = Key.N; break;
|
|
case KeyCode.O: key = Key.O; break;
|
|
case KeyCode.P: key = Key.P; break;
|
|
case KeyCode.Q: key = Key.Q; break;
|
|
case KeyCode.R: key = Key.R; break;
|
|
case KeyCode.S: key = Key.S; break;
|
|
case KeyCode.T: key = Key.T; break;
|
|
case KeyCode.U: key = Key.U; break;
|
|
case KeyCode.V: key = Key.V; break;
|
|
case KeyCode.W: key = Key.W; break;
|
|
case KeyCode.X: key = Key.X; break;
|
|
case KeyCode.Y: key = Key.Y; break;
|
|
case KeyCode.Z: key = Key.Z; break;
|
|
}
|
|
return GetKeyDown(key);
|
|
#endif
|
|
}
|
|
|
|
public static Vector3 GetCursorPosition()
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
var mouse = Mouse.current;
|
|
if (mouse != null)
|
|
{
|
|
return mouse.position.ReadValue();
|
|
}
|
|
|
|
var touchScreen = Touchscreen.current;
|
|
if (touchScreen != null)
|
|
{
|
|
return touchScreen.position.ReadValue();
|
|
}
|
|
return Vector3.zero;
|
|
#else
|
|
return Input.mousePosition;
|
|
#endif
|
|
}
|
|
|
|
public static bool IsTouchOrMousePressed()
|
|
{
|
|
bool isTouched = false;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
var mouse = Mouse.current;
|
|
if (mouse != null && mouse.press.isPressed)
|
|
{
|
|
isTouched = true;
|
|
}
|
|
|
|
var touchScreen = Touchscreen.current;
|
|
if (touchScreen != null && touchScreen.press.isPressed)
|
|
{
|
|
isTouched = true;
|
|
}
|
|
#else
|
|
isTouched = Input.GetMouseButton(0);
|
|
#endif
|
|
return isTouched;
|
|
}
|
|
|
|
public static bool WasTouchOrMousePressed()
|
|
{
|
|
bool isTouchDown = false;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
var mouse = Mouse.current;
|
|
if (mouse != null && mouse.press.wasPressedThisFrame)
|
|
{
|
|
isTouchDown = true;
|
|
}
|
|
|
|
var touchScreen = Touchscreen.current;
|
|
if (touchScreen != null && touchScreen.press.wasPressedThisFrame)
|
|
{
|
|
isTouchDown = true;
|
|
}
|
|
#else
|
|
isTouchDown = Input.GetMouseButtonDown(0);
|
|
#endif
|
|
return isTouchDown;
|
|
}
|
|
|
|
public static bool WasTouchOrMouseReleased()
|
|
{
|
|
bool isTouchUp = false;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
var mouse = Mouse.current;
|
|
if (mouse != null && mouse.press.wasReleasedThisFrame)
|
|
{
|
|
isTouchUp = true;
|
|
}
|
|
|
|
var touchScreen = Touchscreen.current;
|
|
if (touchScreen != null && touchScreen.press.wasReleasedThisFrame)
|
|
{
|
|
isTouchUp = true;
|
|
}
|
|
#else
|
|
isTouchUp = Input.GetMouseButtonUp(0);
|
|
#endif
|
|
return isTouchUp;
|
|
}
|
|
|
|
public static int TouchCount
|
|
{
|
|
get
|
|
{
|
|
int count = 0;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
var touchScreen = Touchscreen.current;
|
|
if (touchScreen != null && touchScreen.press.isPressed)
|
|
{
|
|
for (int i = 0; i < touchScreen.touches.Count; i++)
|
|
{
|
|
if (touchScreen.touches[i].press.isPressed) count++;
|
|
}
|
|
}
|
|
|
|
if (count == 0)
|
|
{
|
|
var mouse = Mouse.current;
|
|
if (mouse != null && mouse.press.isPressed)
|
|
{
|
|
count = 1;
|
|
}
|
|
}
|
|
#else
|
|
count = Input.touchCount;
|
|
#endif
|
|
return count;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Usage: if (SDev.Util.DInputUtil.TryGetTouchPosition(0, out Vector3 touchPos)) { Debug.Log("Touch (screen) position: " + touchPos); }
|
|
/// </summary>
|
|
public static bool TryGetTouchPosition(int touchIndex, out Vector3 touchPosition)
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
var touchScreen = Touchscreen.current;
|
|
if (touchScreen != null && touchScreen.press.isPressed && touchIndex < touchScreen.touches.Count)
|
|
{
|
|
touchPosition = touchScreen.touches[touchIndex].position.ReadValue();
|
|
return true;
|
|
}
|
|
|
|
var mouse = Mouse.current;
|
|
if (mouse != null && mouse.press.isPressed)
|
|
{
|
|
touchPosition = mouse.position.ReadValue();
|
|
return true;
|
|
}
|
|
touchPosition = Vector3.zero;
|
|
return false;
|
|
#else
|
|
touchPosition = Input.mousePosition;
|
|
return true;
|
|
#endif
|
|
}
|
|
}
|
|
}
|