136 lines
3.5 KiB
C#
136 lines
3.5 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Bansonic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class roomAsk : MonoBehaviour
|
|
{
|
|
[Header("UI Elements")]
|
|
[SerializeField] private Button createRoomButton;
|
|
[SerializeField] private TMP_InputField roomCode_inputField;
|
|
[SerializeField] private Button enterRoomButton;
|
|
[SerializeField] private Button quitchoiceButton;
|
|
|
|
private Func<Task> _onCreate;
|
|
private Func<string, Task> _onJoin;
|
|
|
|
public void Bind(Func<Task> onCreate, Func<string, Task> onJoin)
|
|
{
|
|
_onCreate = onCreate;
|
|
_onJoin = onJoin;
|
|
|
|
TryAutoBindQuitButton();
|
|
|
|
if (createRoomButton != null)
|
|
{
|
|
createRoomButton.onClick.RemoveListener(HandleCreateClicked);
|
|
createRoomButton.onClick.AddListener(HandleCreateClicked);
|
|
}
|
|
|
|
if (enterRoomButton != null)
|
|
{
|
|
enterRoomButton.onClick.RemoveListener(HandleJoinClicked);
|
|
enterRoomButton.onClick.AddListener(HandleJoinClicked);
|
|
}
|
|
|
|
if (quitchoiceButton != null)
|
|
{
|
|
quitchoiceButton.onClick.RemoveListener(HandleQuitChoiceClicked);
|
|
quitchoiceButton.onClick.AddListener(HandleQuitChoiceClicked);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (createRoomButton != null)
|
|
{
|
|
createRoomButton.onClick.RemoveListener(HandleCreateClicked);
|
|
}
|
|
|
|
if (enterRoomButton != null)
|
|
{
|
|
enterRoomButton.onClick.RemoveListener(HandleJoinClicked);
|
|
}
|
|
|
|
if (quitchoiceButton != null)
|
|
{
|
|
quitchoiceButton.onClick.RemoveListener(HandleQuitChoiceClicked);
|
|
}
|
|
}
|
|
|
|
private void TryAutoBindQuitButton()
|
|
{
|
|
if (quitchoiceButton != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Button[] buttons = GetComponentsInChildren<Button>(true);
|
|
foreach (Button button in buttons)
|
|
{
|
|
if (button == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
string name = button.gameObject.name;
|
|
if (string.Equals(name, "quitchoiceButton", StringComparison.OrdinalIgnoreCase)
|
|
|| string.Equals(name, "closeButton", StringComparison.OrdinalIgnoreCase)
|
|
|| string.Equals(name, "cancelButton", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
quitchoiceButton = button;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private async void HandleCreateClicked()
|
|
{
|
|
if (_onCreate == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
await _onCreate.Invoke();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogWarning($"[roomAsk] Create room failed: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private async void HandleJoinClicked()
|
|
{
|
|
if (_onJoin == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
string roomCode = roomCode_inputField != null ? roomCode_inputField.text?.Trim() : string.Empty;
|
|
if (string.IsNullOrWhiteSpace(roomCode))
|
|
{
|
|
Debug.LogWarning("[roomAsk] Room code is empty.");
|
|
gNotice.error.display("房间号不能为空");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
await _onJoin.Invoke(roomCode);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogWarning($"[roomAsk] Join room failed: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private void HandleQuitChoiceClicked()
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|