Files
Zombie/Assets/_Zombie/Scripts/UI/TrueGearPanel.cs

390 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using UnityEngine;
using XUI;
using TMPro;
using TruegearSdk;
public class TrueGearPanel : UIBehaviour
{
// TrueGear设备选择 - 使用XuanBtn列表
public GameObject XuanBtnPrefab;
public Transform DeviceListContainer;
public List<XuanBtn> XuanBtns;
public UnityEngine.UI.Button connectButton;
public UnityEngine.UI.Button refreshButton;
// 连接按钮的图片(选中时显示选择图片,未选中时显示无马甲图片)
public UnityEngine.UI.Image connectButtonImage;
public Sprite selectedSprite; // 选择图片(选中设备时使用)
public Sprite noVestSprite; // 无马甲图片(未选中设备时使用)
private List<DeviceData> scannedDevices = new List<DeviceData>();
// 选中的设备
private XuanBtn _selectedBtn = null;
private DeviceData _selectedDevice = null;
// 连接成功反馈的Text组件
public UnityEngine.UI.Text connectStatusText;
// 面板的GameObject用于隐藏/显示)
public GameObject panelGameObject;
public GameObject Conpanel;
public override void OnUIShow(params object[] args)
{
base.OnUIShow(args);
// 从TrueGearEffectManager获取开关状态
bool isEnabled = TrueGearEffectManager.Ins != null && TrueGearEffectManager.Ins.EnableTrueGear;
// 如果未启用马甲功能则显示面板用于连接外部设备隐藏Conpanel
if (!isEnabled)
{
Debug.Log("TrueGear功能未启用显示面板连接外部设备");
if (panelGameObject != null)
{
panelGameObject.SetActive(true);
}
if (Conpanel != null)
{
Conpanel.SetActive(false);
}
// 打开面板时自动扫描设备
return;
}
// 启用功能时隐藏面板显示Conpanel
if (panelGameObject != null)
{
panelGameObject.SetActive(false);
}
if (Conpanel != null)
{
Conpanel.SetActive(true);
}
ScanAndPopulateDevices();
// 注册游戏开始时停止搜索(使用协程监听)
StartCoroutine(WaitForGameStartAndStopScan());
}
private System.Collections.IEnumerator WaitForGameStartAndStopScan()
{
// 等待游戏开始
while (!GameManager.Ins.isStart)
{
yield return null;
}
// 游戏开始后停止搜索
if (TrueGearEffectManager.Ins != null)
{
TrueGearEffectManager.Ins.StopScan();
Debug.Log("游戏开始停止TrueGear蓝牙扫描");
}
}
/// <summary>
/// 扫描设备并填充列表
/// </summary>
private void ScanAndPopulateDevices()
{
if (XuanBtnPrefab == null || DeviceListContainer == null)
{
Debug.LogWarning("XuanBtnPrefab 或 DeviceListContainer 未设置");
return;
}
// 初始化蓝牙InitTrueGear会自动开始搜索
if (TrueGearEffectManager.Ins != null)
{
TrueGearEffectManager.Ins.InitTrueGear();
}
// 等待蓝牙鉴权完成后获取设备列表
StartCoroutine(InitAndScanCoroutine());
}
/// <summary>
/// 等待蓝牙鉴权完成后获取设备列表
/// </summary>
private System.Collections.IEnumerator InitAndScanCoroutine()
{
// 等待蓝牙鉴权完成
yield return new WaitForSeconds(0.3f);
// InitTrueGear()已经自动开始扫描,这里直接获取设备列表
StartCoroutine(ScanDevicesCoroutine());
}
private System.Collections.IEnumerator ScanDevicesCoroutine()
{
// 等待扫描完成
yield return new WaitForSeconds(0.5f);
// 获取设备列表
if (TrueGearEffectManager.Ins != null)
{
Debug.Log("开始获取设备列表...");
scannedDevices = TrueGearEffectManager.Ins.GetDeviceList();
Debug.Log($"扫描到设备数量: {scannedDevices?.Count ?? 0}");
FreshDevices();
}
}
/// <summary>
/// 刷新设备列表显示
/// </summary>
private void FreshDevices()
{
// 清除所有按钮的显示
for (int i = 0; i < XuanBtns.Count; i++)
{
if (XuanBtns[i] != null)
{
XuanBtns[i].gameObject.SetActive(false);
}
}
if (scannedDevices == null || scannedDevices.Count == 0)
{
// 没有设备时显示提示
ShowConnectStatus("未扫描到设备", false);
// 如果没有设备了,清除选中状态
if (_selectedBtn != null)
{
ClearSelection();
UpdateConnectButtonImage(false);
}
return;
}
// 检查当前选中的设备是否还在列表中
bool selectedDeviceStillExists = false;
if (_selectedDevice != null)
{
selectedDeviceStillExists = scannedDevices.Exists(d => d.address == _selectedDevice.address);
}
// 显示扫描到的设备
for (int i = 0; i < scannedDevices.Count; i++)
{
if (i < XuanBtns.Count)
{
XuanBtns[i].Init(scannedDevices[i], i);
XuanBtns[i].OnSelectCallback = OnSelectDevice;
XuanBtns[i].gameObject.SetActive(true);
// 恢复之前选中的按钮状态
if (selectedDeviceStillExists && _selectedBtn != null && _selectedDevice != null)
{
if (scannedDevices[i].address == _selectedDevice.address)
{
_selectedBtn = XuanBtns[i];
XuanBtns[i].SetSelected(true);
}
}
}
}
// 如果选中的设备不在新列表中,清除选中状态
if (!selectedDeviceStillExists)
{
ClearSelection();
UpdateConnectButtonImage(false);
ShowConnectStatus($"扫描到 {scannedDevices.Count} 个设备,请选择要连接的设备", false);
}
else
{
// 选中的设备仍然存在,显示已选择状态
ShowConnectStatus($"已选择: {_selectedDevice.address}", false);
}
}
/// <summary>
/// 选中设备
/// </summary>
private void OnSelectDevice(XuanBtn btn)
{
// 检查点击的是否是当前已经选中的按钮
bool isCurrentlySelected = (_selectedBtn == btn);
if (isCurrentlySelected)
{
// 点击的是当前已选中的按钮:取消选中
ClearSelection();
UpdateConnectButtonImage(false);
ShowConnectStatus("请选择要连接的设备", false);
}
else
{
// 点击的是新按钮:选中新按钮,取消其他按钮的选中状态
ClearSelectionExcept(btn);
// 设置选中新设备
_selectedBtn = btn;
_selectedDevice = btn.GetDeviceInfo();
// 更新连接按钮图片为选择图片
UpdateConnectButtonImage(true);
Debug.Log($"选中设备: {_selectedDevice.address}");
ShowConnectStatus($"已选择: {_selectedDevice.address}", false);
}
}
/// <summary>
/// 更新连接按钮的图片
/// </summary>
private void UpdateConnectButtonImage(bool isSelected)
{
if (connectButtonImage == null) return;
if (isSelected && selectedSprite != null)
{
connectButtonImage.sprite = selectedSprite;
}
else if (!isSelected && noVestSprite != null)
{
connectButtonImage.sprite = noVestSprite;
}
}
/// <summary>
/// 清除选中状态(除了指定按钮)
/// </summary>
private void ClearSelectionExcept(XuanBtn exceptBtn)
{
// 清除其他按钮的选中状态
foreach (var xuanBtn in XuanBtns)
{
if (xuanBtn != null && xuanBtn != exceptBtn)
{
xuanBtn.SetSelected(false);
}
}
}
/// <summary>
/// 清除选中状态
/// </summary>
private void ClearSelection()
{
if (_selectedBtn != null)
{
_selectedBtn.SetSelected(false);
_selectedBtn = null;
}
_selectedDevice = null;
}
/// <summary>
/// 连接按钮点击事件
/// </summary>
public void OnClickConnectButton()
{
if (_selectedDevice == null)
{
Debug.LogWarning("请先选择要连接的设备");
ShowConnectStatus("请先选择要连接的设备", false);
return;
}
// 查找选中设备在列表中的索引
int deviceIndex = scannedDevices.IndexOf(_selectedDevice);
if (deviceIndex < 0)
{
Debug.LogWarning("设备信息无效");
ShowConnectStatus("设备信息无效", false);
return;
}
// 连接选中的设备
if (TrueGearEffectManager.Ins != null)
{
TrueGearEffectManager.Ins.ConnectToDeviceByIndex(deviceIndex);
// 检查是否连接成功
if (TrueGearEffectManager.Ins.isGetConnect)
{
ShowConnectStatus("连接成功!", true);
}
else
{
ShowConnectStatus("连接失败,请重试", false);
}
}
}
/// <summary>
/// 显示连接状态
/// </summary>
private void ShowConnectStatus(string message, bool isSuccess)
{
if (connectStatusText != null)
{
connectStatusText.text = message;
connectStatusText.color = isSuccess ? Color.green : Color.red;
}
Debug.Log($"连接状态: {message}");
}
/// <summary>
/// 刷新按钮点击事件
/// </summary>
public void OnClickRefreshButton()
{
Debug.Log("点击刷新按钮,重新扫描设备...");
// 清除之前的选中状态
ClearSelection();
// 重新初始化并扫描
if (TrueGearEffectManager.Ins != null)
{
TrueGearEffectManager.Ins.InitTrueGear();
}
StartCoroutine(InitAndScanCoroutine());
}
public void Start()
{
// 初始化XuanBtn列表
if (XuanBtns == null)
{
XuanBtns = new List<XuanBtn>();
}
// 初始化连接按钮图片为无马甲图片
UpdateConnectButtonImage(false);
// 预先创建XuanBtn实例
if (XuanBtnPrefab != null && DeviceListContainer != null)
{
for (int i = 0; i < 50; i++)
{
// 使用false参数让实例保持世界坐标为false这样会使用父物体的布局设置
GameObject btnObj = Instantiate(XuanBtnPrefab, DeviceListContainer, false);
XuanBtn btn = btnObj.GetComponent<XuanBtn>();
if (btn != null)
{
btn.gameObject.SetActive(false);
XuanBtns.Add(btn);
}
}
}
// 注册连接按钮点击事件
if (connectButton != null)
{
connectButton.onClick.AddListener(OnClickConnectButton);
}
// 注册刷新按钮点击事件
if (refreshButton != null)
{
refreshButton.onClick.AddListener(OnClickRefreshButton);
}
}
}