using System.Collections.Generic; using UnityEngine; using XUI; using TMPro; using TruegearSdk; public class TrueGearPanel : UIBehaviour { // TrueGear设备选择 public TMP_Dropdown deviceDropdown; public UnityEngine.UI.Button connectButton; public UnityEngine.UI.Button refreshButton; private List scannedDevices = new List(); private int selectedDeviceIndex = -1; // 连接成功反馈的Text组件 public UnityEngine.UI.Text connectStatusText; // 面板的GameObject(用于隐藏/显示) public GameObject panelGameObject; public override void OnUIShow(params object[] args) { base.OnUIShow(args); // 从TrueGearEffectManager获取开关状态 bool isEnabled = TrueGearEffectManager.Ins != null && TrueGearEffectManager.Ins.EnableTrueGear; // 如果未启用马甲功能,则隐藏面板并返回 if (!isEnabled) { Debug.Log("TrueGear功能未启用,隐藏面板"); if (panelGameObject != null) { panelGameObject.SetActive(false); } return; } // 启用功能时显示面板 if (panelGameObject != null) { panelGameObject.SetActive(true); } // 打开面板时自动扫描设备 ScanAndPopulateDevices(); } /// /// 扫描设备并填充Dropdown /// private void ScanAndPopulateDevices() { if (deviceDropdown == null) { Debug.LogWarning("DeviceDropdown 未设置"); return; } // 先初始化蓝牙 if (TrueGearEffectManager.Ins != null) { TrueGearEffectManager.Ins.InitTrueGear(); } // 延迟1秒后开始扫描,等待蓝牙初始化完成 StartCoroutine(InitAndScanCoroutine()); } /// /// 初始化蓝牙后开始扫描 /// private System.Collections.IEnumerator InitAndScanCoroutine() { // 增加等待时间,确保蓝牙初始化完成 yield return new WaitForSeconds(2f); // 开始扫描 if (TrueGearEffectManager.Ins != null) { TrueGearEffectManager.Ins.StartScanTrueGear(); } // 延迟获取设备列表,等待扫描完成 StartCoroutine(ScanDevicesCoroutine()); } private System.Collections.IEnumerator ScanDevicesCoroutine() { // 等待3秒让扫描完成 yield return new WaitForSeconds(3f); // 获取设备列表 if (TrueGearEffectManager.Ins != null) { Debug.Log("开始获取设备列表..."); scannedDevices = TrueGearEffectManager.Ins.GetDeviceList(); Debug.Log($"扫描到设备数量: {scannedDevices?.Count ?? 0}"); PopulateDropdown(); } } /// /// 填充Dropdown选项 /// private void PopulateDropdown() { if (deviceDropdown == null) return; deviceDropdown.ClearOptions(); if (scannedDevices == null || scannedDevices.Count == 0) { // 没有设备时显示提示 List options = new List(); options.Add(new TMP_Dropdown.OptionData("未扫描到设备")); deviceDropdown.AddOptions(options); return; } List deviceOptions = new List(); foreach (var device in scannedDevices) { // 显示设备ID(address) string displayText = device.address; deviceOptions.Add(new TMP_Dropdown.OptionData(displayText)); } deviceDropdown.AddOptions(deviceOptions); // 如果只有一台设备,自动选中 if (scannedDevices.Count == 1) { selectedDeviceIndex = 0; deviceDropdown.value = 0; Debug.Log($"只有一台设备,自动选中: {scannedDevices[0].address}"); } // 添加Dropdown选择事件 deviceDropdown.onValueChanged.RemoveAllListeners(); deviceDropdown.onValueChanged.AddListener(OnDeviceDropdownChanged); } /// /// 设备Dropdown选择改变时调用 /// public void OnDeviceDropdownChanged(int index) { if (index < 0 || index >= scannedDevices.Count) { Debug.LogWarning("无效的设备索引"); return; } // 只保存选中的设备索引,不自动连接 selectedDeviceIndex = index; Debug.Log($"选中设备索引: {index}, 地址: {scannedDevices[index].address}"); } /// /// 连接按钮点击事件 /// public void OnClickConnectButton() { if (selectedDeviceIndex < 0 || selectedDeviceIndex >= scannedDevices.Count) { Debug.LogWarning("请先选择要连接的设备"); ShowConnectStatus("请先选择要连接的设备", false); return; } // 连接选中的设备 if (TrueGearEffectManager.Ins != null) { TrueGearEffectManager.Ins.ConnectToDeviceByIndex(selectedDeviceIndex); // 检查是否连接成功 if (TrueGearEffectManager.Ins.isGetConnect) { ShowConnectStatus("连接成功!", true); } else { ShowConnectStatus("连接失败,请重试", false); } } } /// /// 显示连接状态 /// private void ShowConnectStatus(string message, bool isSuccess) { if (connectStatusText != null) { connectStatusText.text = message; connectStatusText.color = isSuccess ? Color.green : Color.red; } Debug.Log($"连接状态: {message}"); } /// /// 刷新按钮点击事件 /// public void OnClickRefreshButton() { Debug.Log("点击刷新按钮,重新扫描设备..."); // 重新初始化并扫描 if (TrueGearEffectManager.Ins != null) { TrueGearEffectManager.Ins.InitTrueGear(); } StartCoroutine(InitAndScanCoroutine()); } public void Start() { // 注册连接按钮点击事件 if (connectButton != null) { connectButton.onClick.AddListener(OnClickConnectButton); } // 注册刷新按钮点击事件 if (refreshButton != null) { refreshButton.onClick.AddListener(OnClickRefreshButton); } } }