Files
tcg-client/Assets/TcgEngine/Scripts/Menu/MatchmakingPanel.cs
xianyi 12a19ba1a9 匹配自动重连
- 修复第一次连接时ClientID为0导致匹配失败的问题
- 在GameClientMatchmaker中添加ClientID检测和自动重连逻辑
- 当检测到ClientID为0时,等待0.5秒后自动断开并重新连接
- 优化MatchmakingPanel显示逻辑,根据ClientID状态显示正确的连接状态
2025-10-16 11:43:27 +08:00

80 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TcgEngine.Client;
using TcgEngine;
namespace TcgEngine.UI
{
/// <summary>
/// Matchmaking panel is just a loading panel that displays how many players are found yet
/// </summary>
public class MatchmakingPanel : UIPanel
{
public Text text;
public Text players_txt;
public Text code_txt;
private static MatchmakingPanel instance;
protected override void Awake()
{
base.Awake();
instance = this;
}
protected override void Start()
{
base.Start();
code_txt.text = "";
}
protected override void Update()
{
base.Update();
GameClientMatchmaker matchmaker = GameClientMatchmaker.Get();
bool is_connected = matchmaker.IsConnected();
ulong client_id = TcgNetwork.Get().ClientID;
// 显示连接状态
if (is_connected && client_id != 0)
text.text = "正在寻找对手...";
else
text.text = "正在连接到服务器...";
code_txt.text = "";
string group = matchmaker.GetGroup();
if (group != null && group.StartsWith("code_"))
code_txt.text = group.Replace("code_", "");
}
public void SetCount(int players)
{
if (players_txt != null)
players_txt.text = players.ToString() + "/" + GameClientMatchmaker.Get().GetNbPlayers();
}
public void OnClickCancel()
{
GameClientMatchmaker.Get().StopMatchmaking();
Hide();
}
public override void Show(bool instant = false)
{
base.Show(instant);
if (players_txt != null)
players_txt.text = "";
}
public static MatchmakingPanel Get()
{
return instance;
}
}
}