添加选择马甲初版

This commit is contained in:
ZYT
2026-04-07 17:33:45 +08:00
parent 82d716c419
commit 184ffe2299
10 changed files with 2193 additions and 13 deletions

View File

@@ -1,6 +1,8 @@
using System.Collections.Generic;
using UnityEngine;
using XUI;
using TMPro;
using TruegearSdk;
public class ConPanel : UIBehaviour
{
@@ -12,7 +14,6 @@ public class ConPanel : UIBehaviour
public List<IPBtn> IPBtns;
public bool IsInit = false;
public static void Show()
{
WorldUIManager.Ins.Cover("UI/ConPanel", false);
@@ -26,6 +27,7 @@ public class ConPanel : UIBehaviour
btn.gameObject.SetActive(false);
IPBtns.Add(btn);
}
IsInit = true;
}
@@ -40,6 +42,11 @@ public class ConPanel : UIBehaviour
public void OnClickHost()
{
// 用户未选择设备直接开始游戏,停止扫描
if (TrueGearEffectManager.Ins != null)
{
TrueGearEffectManager.Ins.StopScan();
}
WorldUIManager.Ins.Back();
MRNetworkManager.Ins.CreateAndJoinRoom();
MRNetworkManager.Ins.networkDiscovery.AdvertiseServer();
@@ -79,6 +86,11 @@ public class ConPanel : UIBehaviour
public void OnOnePlayerClick()
{
// 用户未选择设备直接开始游戏,停止扫描
if (TrueGearEffectManager.Ins != null)
{
TrueGearEffectManager.Ins.StopScan();
}
GameLocal.Ins.GameMode = 0;
WorldUIManager.Ins.Back();
MRNetworkManager.Ins.CreateAndJoinRoom();
@@ -87,6 +99,11 @@ public class ConPanel : UIBehaviour
public void OnServerPlayerClick()
{
// 用户未选择设备直接开始游戏,停止扫描
if (TrueGearEffectManager.Ins != null)
{
TrueGearEffectManager.Ins.StopScan();
}
GameLocal.Ins.GameMode = 1;
WorldUIManager.Ins.Back();
MRNetworkManager.Ins.CreateAndJoinRoom();

View File

@@ -0,0 +1,232 @@
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<DeviceData> scannedDevices = new List<DeviceData>();
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();
}
/// <summary>
/// 扫描设备并填充Dropdown
/// </summary>
private void ScanAndPopulateDevices()
{
if (deviceDropdown == null)
{
Debug.LogWarning("DeviceDropdown 未设置");
return;
}
// 先初始化蓝牙
if (TrueGearEffectManager.Ins != null)
{
TrueGearEffectManager.Ins.InitTrueGear();
}
// 延迟1秒后开始扫描等待蓝牙初始化完成
StartCoroutine(InitAndScanCoroutine());
}
/// <summary>
/// 初始化蓝牙后开始扫描
/// </summary>
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();
}
}
/// <summary>
/// 填充Dropdown选项
/// </summary>
private void PopulateDropdown()
{
if (deviceDropdown == null) return;
deviceDropdown.ClearOptions();
if (scannedDevices == null || scannedDevices.Count == 0)
{
// 没有设备时显示提示
List<TMP_Dropdown.OptionData> options = new List<TMP_Dropdown.OptionData>();
options.Add(new TMP_Dropdown.OptionData("未扫描到设备"));
deviceDropdown.AddOptions(options);
return;
}
List<TMP_Dropdown.OptionData> deviceOptions = new List<TMP_Dropdown.OptionData>();
foreach (var device in scannedDevices)
{
// 显示设备IDaddress
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);
}
/// <summary>
/// 设备Dropdown选择改变时调用
/// </summary>
public void OnDeviceDropdownChanged(int index)
{
if (index < 0 || index >= scannedDevices.Count)
{
Debug.LogWarning("无效的设备索引");
return;
}
// 只保存选中的设备索引,不自动连接
selectedDeviceIndex = index;
Debug.Log($"选中设备索引: {index}, 地址: {scannedDevices[index].address}");
}
/// <summary>
/// 连接按钮点击事件
/// </summary>
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);
}
}
}
/// <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("点击刷新按钮,重新扫描设备...");
// 重新初始化并扫描
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);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 752e5b94c1848a74da7497e498739cf6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: