using System.Diagnostics; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.InputSystem.LowLevel; using UnityEngine.InputSystem.Utilities; [StructLayout(LayoutKind.Explicit, Size = kSize)] internal struct MyControlCommand : IInputDeviceCommandInfo { public static FourCC Type { get { return new FourCC('H', 'I', 'D', 'O'); } } internal const int id = 0; internal const int kSize = InputDeviceCommand.BaseCommandSize + 2;//HID的接收字节 [FieldOffset(0)] public InputDeviceCommand baseCommand; [FieldOffset(InputDeviceCommand.BaseCommandSize)] public byte reportId; [FieldOffset(InputDeviceCommand.BaseCommandSize + 1)] public byte data; //Add data here public FourCC typeStatic => Type; // 确保这里返回 FourCC 类型 public static MyControlCommand Create(byte data) { return new MyControlCommand { baseCommand = new InputDeviceCommand(Type, kSize), reportId = id, data = data }; } } public class DeviceTest : MonoBehaviour { private void OnEnable() { //注册事件处理器 InputSystem.onEvent += OnInputEvent; } private void OnDisable() { //注销事件处理器 InputSystem.onEvent -= OnInputEvent; } private void OnInputEvent(InputEventPtr eventPtr, InputDevice device) { //处理输入事件 //UnityEngine.Debug.Log($"Input event for device {device}: {eventPtr}"); } void Update() { var myDevice = InputSystem.GetDevice(); if (myDevice != null) { if (myDevice.button.isPressed) { UnityEngine.Debug.Log("Button pressed on Custom HID Device"); } else { } } else { UnityEngine.Debug.Log("Custom HID Device not detected"); } } public void OnMouseDown() { UnityEngine.Debug.Log("click1"); var myDevice = InputSystem.GetDevice(); //当鼠标点击此对象时触发 if (myDevice != null) { UnityEngine.Debug.Log("click2"); // 构造报告数据 byte reportData = 0x01; // 示例数据 // // 创建并发送报告 var command = MyControlCommand.Create(reportData); myDevice.ExecuteCommand(ref command); UnityEngine.Debug.Log("Report sent to ESP32"); } } }