/** * UniWindowController 的示例脚本 * * 作者: Kirurobo http://twitter.com/kirurobo * 许可证: MIT */ using System; using UnityEngine; #if ENABLE_LEGACY_INPUT_MANAGER // Don't use InputSystem in this script to prevent TouchPhase duplication #elif ENABLE_INPUT_SYSTEM using UnityEngine.InputSystem; #endif namespace Kirurobo { /// /// 用于消除 Input System 和 Input Manager 差异的代理 /// public class InputProxy { public static Vector3 mousePosition { get { return GetMousePosition(); } } /// /// 配合 Input System 获取按键抬起 /// /// public static bool GetKeyUp(String key) { #if ENABLE_LEGACY_INPUT_MANAGER return Input.GetKeyUp(key); #elif ENABLE_INPUT_SYSTEM // 简易实现。仅支持单字母字符或数字 // 非单字符的仅支持 escape, space if (key.Length == 1) { Key k = Key.None; char c = key[0]; if (c >= '0' && c <= '9') { // 数字时同时响应 Digit0~Digit9 和 Numpad0~Numpad9 k = (Key)Enum.ToObject(typeof(Key), (int)Key.Numpad0 + (int)(c - '0')); if (Keyboard.current[k].wasReleasedThisFrame) return true; // Digit 键中 Digit0 的数值最大 k = (Key)Enum.ToObject(typeof(Key), (int)Key.Digit1 + (int)((c == '0' ? 9 : c - '1'))); if (Keyboard.current[k].wasReleasedThisFrame) return true; } if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { // 字母时大小写均可 k = (Key)Enum.ToObject(typeof(Key), (int)Key.A + (int)(Char.ToUpper(c) - 'A')); if (Keyboard.current[k].wasReleasedThisFrame) return true; } } else if (key == "escape") { return Keyboard.current.escapeKey.wasReleasedThisFrame; } else if (key == "space") { return Keyboard.current.spaceKey.wasReleasedThisFrame; } return false; #else return false; #endif } /// /// 配合 Input System 获取鼠标坐标 /// /// private static Vector3 GetMousePosition() { #if ENABLE_LEGACY_INPUT_MANAGER return Input.mousePosition; #elif ENABLE_INPUT_SYSTEM return Mouse.current.position.ReadValue(); #else return Vector3.zero; #endif } /// /// 判断鼠标按钮当前是否被按下 /// /// /// public static bool GetMouseButton(int button) { #if ENABLE_LEGACY_INPUT_MANAGER return Input.GetMouseButton(button); #elif ENABLE_INPUT_SYSTEM if (button == 0) return Mouse.current.leftButton.isPressed; if (button == 1) return Mouse.current.rightButton.isPressed; if (button == 2) return Mouse.current.middleButton.isPressed; return false; #else return false; #endif } /// /// 判断本帧是否按下了鼠标按钮 /// /// /// public static bool GetMouseButtonDown(int button) { #if ENABLE_LEGACY_INPUT_MANAGER return Input.GetMouseButtonDown(button); #elif ENABLE_INPUT_SYSTEM if (button == 0) return Mouse.current.leftButton.wasPressedThisFrame; if (button == 1) return Mouse.current.rightButton.wasPressedThisFrame; if (button == 2) return Mouse.current.middleButton.wasPressedThisFrame; return false; #else return false; #endif } /// /// 判断本帧是否松开了鼠标按钮 /// /// /// public static bool GetMouseButtonUp(int button) { #if ENABLE_LEGACY_INPUT_MANAGER return Input.GetMouseButtonUp(button); #elif ENABLE_INPUT_SYSTEM if (button == 0) return Mouse.current.leftButton.wasReleasedThisFrame; if (button == 1) return Mouse.current.rightButton.wasReleasedThisFrame; if (button == 2) return Mouse.current.middleButton.wasReleasedThisFrame; return false; #else return false; #endif } } }