137 lines
4.1 KiB
C#
137 lines
4.1 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace XGame
|
|
{
|
|
public class MatchWaitingController : MonoBehaviour
|
|
{
|
|
private RectTransform spinnerRect;
|
|
private TMP_Text titleText;
|
|
private TMP_Text gameText;
|
|
private TMP_Text statusText;
|
|
private TMP_Text elapsedText;
|
|
private TMP_Text closeText;
|
|
private Button closeButton;
|
|
private string luaModule = "client/baseworld";
|
|
private string currentGameId = string.Empty;
|
|
private float startedAt;
|
|
public Action OnCloseRequested;
|
|
|
|
private void Awake()
|
|
{
|
|
CacheControls();
|
|
WireEvents();
|
|
startedAt = Time.realtimeSinceStartup;
|
|
UpdateElapsedText();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (spinnerRect != null)
|
|
{
|
|
spinnerRect.Rotate(0f, 0f, -180f * Time.unscaledDeltaTime);
|
|
}
|
|
if (elapsedText != null && startedAt > 0f)
|
|
{
|
|
UpdateElapsedText();
|
|
}
|
|
}
|
|
|
|
public void Bind(string module)
|
|
{
|
|
if (!string.IsNullOrEmpty(module))
|
|
{
|
|
luaModule = module;
|
|
}
|
|
}
|
|
|
|
public void SetWaiting(string gameId, string message)
|
|
{
|
|
CacheControls();
|
|
currentGameId = gameId ?? string.Empty;
|
|
startedAt = Time.realtimeSinceStartup;
|
|
SetText(titleText, "匹配等待");
|
|
SetText(gameText, string.IsNullOrEmpty(currentGameId) ? "正在寻找对局" : currentGameId);
|
|
UpdateElapsedText();
|
|
SetStatus(string.IsNullOrEmpty(message) ? "正在匹配,请稍候..." : message, false);
|
|
}
|
|
|
|
public void SetStatus(string message)
|
|
{
|
|
SetStatus(message, false);
|
|
}
|
|
|
|
public void SetStatus(string message, bool canClose)
|
|
{
|
|
CacheControls();
|
|
SetText(statusText, message);
|
|
if (closeText != null)
|
|
{
|
|
closeText.text = canClose ? "返回大厅" : "退出匹配";
|
|
}
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
private void CacheControls()
|
|
{
|
|
spinnerRect = spinnerRect ? spinnerRect : Find<RectTransform>("Img_Spinner");
|
|
titleText = titleText ? titleText : Find<TMP_Text>("Txt_Title");
|
|
gameText = gameText ? gameText : Find<TMP_Text>("Txt_Game");
|
|
statusText = statusText ? statusText : Find<TMP_Text>("Txt_Status");
|
|
elapsedText = elapsedText ? elapsedText : Find<TMP_Text>("Txt_Elapsed");
|
|
closeButton = closeButton ? closeButton : Find<Button>("Btn_Close");
|
|
closeText = closeText ? closeText : Find<TMP_Text>("Txt_Close");
|
|
if (closeText == null && closeButton != null)
|
|
{
|
|
closeText = closeButton.transform.FindTransform("Text")?.GetComponent<TMP_Text>();
|
|
}
|
|
}
|
|
|
|
private void WireEvents()
|
|
{
|
|
if (closeButton == null)
|
|
{
|
|
return;
|
|
}
|
|
closeButton.onClick.RemoveListener(OnCloseClicked);
|
|
closeButton.onClick.AddListener(OnCloseClicked);
|
|
}
|
|
|
|
private void OnCloseClicked()
|
|
{
|
|
OnCloseRequested?.Invoke();
|
|
}
|
|
|
|
private T Find<T>(string name) where T : Component
|
|
{
|
|
Transform target = transform.FindTransform(name);
|
|
return target == null ? null : target.GetComponent<T>();
|
|
}
|
|
|
|
private static void SetText(TMP_Text text, string value)
|
|
{
|
|
if (text != null)
|
|
{
|
|
text.text = value ?? string.Empty;
|
|
}
|
|
}
|
|
|
|
private void UpdateElapsedText()
|
|
{
|
|
if (elapsedText == null || startedAt <= 0f)
|
|
{
|
|
return;
|
|
}
|
|
int seconds = Mathf.Max(0, Mathf.FloorToInt(Time.realtimeSinceStartup - startedAt));
|
|
elapsedText.text = string.Format("已匹配时间 {0:00}:{1:00}", seconds / 60, seconds % 60);
|
|
}
|
|
|
|
}
|
|
}
|