From 4e24bd4f5d0503deb1b1ced766d9ad75daea8c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Tue, 16 Dec 2025 14:00:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/Input/InputDeviceWatcher.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Runtime/Input/InputDeviceWatcher.cs b/Runtime/Input/InputDeviceWatcher.cs index d2a3ed5..e0f7db1 100644 --- a/Runtime/Input/InputDeviceWatcher.cs +++ b/Runtime/Input/InputDeviceWatcher.cs @@ -24,6 +24,7 @@ public static class InputDeviceWatcher private static int _lastDeviceId = -1; private static float _lastInputTime = -Mathf.Infinity; + private static InputDeviceCategory _lastEmittedCategory = InputDeviceCategory.Keyboard; public static event Action OnDeviceChanged; @@ -37,6 +38,7 @@ public static class InputDeviceWatcher CurrentCategory = InputDeviceCategory.Keyboard; CurrentDeviceName = ""; + _lastEmittedCategory = CurrentCategory; // 初始化同步 _anyInputAction = new InputAction("AnyDevice", InputActionType.PassThrough); _anyInputAction.AddBinding("/anyKey"); @@ -77,6 +79,8 @@ public static class InputDeviceWatcher OnDeviceChanged = null; initialized = false; + + _lastEmittedCategory = InputDeviceCategory.Keyboard; } private static void OnAnyInputPerformed(InputAction.CallbackContext ctx) @@ -96,7 +100,7 @@ public static class InputDeviceWatcher CurrentCategory = DetermineCategoryFromDevice(device); CurrentDeviceName = device.displayName ?? $"Device_{curId}"; - EmitChangeLog(); + EmitChange(); } // ------------------ 监听设备变更 -------------------- @@ -110,7 +114,7 @@ public static class InputDeviceWatcher _lastInputTime = -Mathf.Infinity; CurrentDeviceName = ""; CurrentCategory = InputDeviceCategory.Keyboard; - EmitChangeLog(); + EmitChange(); } } } @@ -183,8 +187,13 @@ public static class InputDeviceWatcher } // ------------------ 输出 -------------------- - private static void EmitChangeLog() + private static void EmitChange() { + if (CurrentCategory == _lastEmittedCategory) + { + return; + } + int vid = GetVendorId(); int pid = GetProductId(); @@ -192,7 +201,9 @@ public static class InputDeviceWatcher Debug.Log($"输入设备变更 -> {CurrentCategory} 触发设备: {CurrentDeviceName} vid=0x{vid:X} pid=0x{pid:X}"); #endif + // 触发事件并记录已发射的分类 OnDeviceChanged?.Invoke(CurrentCategory); + _lastEmittedCategory = CurrentCategory; } private static int GetVendorId()