您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
193 行
5.3 KiB
193 行
5.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Unity.MegaCity.Gameplay;
|
|
using UnityEngine.Events;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace Unity.MegaCity.UI
|
|
{
|
|
public class LobbyPanelController : MonoBehaviour
|
|
{
|
|
// 面板切换
|
|
public GameObject joinPanel;
|
|
public GameObject createPanel;
|
|
public Image joinButton;
|
|
public Image createButton;
|
|
public Sprite enableSprite;
|
|
public Sprite disableSprite;
|
|
|
|
private MultiverseRoomAPI _lobbyInstance;
|
|
public static UnityEvent<RoomInfo> JoinRoomByRoomInfo = new();
|
|
|
|
// 房间列表
|
|
public GameObject roomItemPrefab;
|
|
public Transform roomList;
|
|
|
|
// 根据 UUID 加入房间
|
|
public InputField roomUuidInput;
|
|
|
|
// 创建房间
|
|
public InputField roomName;
|
|
|
|
// 房间名称
|
|
private List<string> _gameList = new()
|
|
{
|
|
"帕斯卡契约",
|
|
"海盗王国",
|
|
"炉石传说",
|
|
"崩坏3",
|
|
"剑网3指尖江湖",
|
|
"闪耀暖暖",
|
|
"明日方舟",
|
|
"江南百景图",
|
|
"卷轴",
|
|
"空洞骑士",
|
|
"劳拉Go",
|
|
"王者荣耀",
|
|
"原神",
|
|
"逆水寒",
|
|
"星穹铁道",
|
|
};
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
JoinRoomByRoomInfo.AddListener(JoinRoomByRoomInfoHandler);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
_lobbyInstance = MultiverseRoomAPI.Instance;
|
|
JoinTab();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
// 重置
|
|
JoinTab();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换到加入房间面板
|
|
/// </summary>
|
|
public void JoinTab()
|
|
{
|
|
joinPanel.SetActive(true);
|
|
createPanel.SetActive(false);
|
|
joinButton.sprite = enableSprite;
|
|
createButton.sprite = disableSprite;
|
|
GetRoomList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换到创建房间面板
|
|
/// </summary>
|
|
public void CreateTab()
|
|
{
|
|
joinPanel.SetActive(false);
|
|
createPanel.SetActive(true);
|
|
joinButton.sprite = disableSprite;
|
|
createButton.sprite = enableSprite;
|
|
|
|
// 随机生成名字
|
|
roomName.text = GetRandomName();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取房间列表
|
|
/// </summary>
|
|
private async void GetRoomList()
|
|
{
|
|
GetRoomListReq req = new()
|
|
{
|
|
start = 0,
|
|
count = 20
|
|
};
|
|
var res = await _lobbyInstance.GetRoomList(req);
|
|
var rooms = res.rooms;
|
|
DestroyAllChildren(roomList);
|
|
foreach (var room in rooms)
|
|
{
|
|
var roomItem = Instantiate(roomItemPrefab, roomList);
|
|
roomItem.GetComponent<LobbyRoomItem>().Set(room);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 销毁所有子项
|
|
/// </summary>
|
|
/// <param name="parent"></param>
|
|
private void DestroyAllChildren(Transform parent)
|
|
{
|
|
var count = parent.childCount;
|
|
for (int i = 0; i < count; i += 1)
|
|
{
|
|
Destroy(parent.GetChild(i).gameObject);
|
|
}
|
|
}
|
|
|
|
public async void JoinRoomByUUID()
|
|
{
|
|
var uuid = roomUuidInput.text;
|
|
JoinRoomByUUID(uuid);
|
|
}
|
|
|
|
private async void JoinRoomByUUID(string uuid)
|
|
{
|
|
if (uuid != "")
|
|
{
|
|
MainMenu.Instance.ShowLoading();
|
|
try
|
|
{
|
|
var req = new JoinRoomReq()
|
|
{
|
|
roomUUID = uuid,
|
|
};
|
|
|
|
await _lobbyInstance.JoinRoom(req);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
MainMenu.Instance.HideLoading();
|
|
UIMessage.Instance.Show("Failed to join room. Please try again.");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void JoinRoomByRoomInfoHandler(RoomInfo room)
|
|
{
|
|
JoinRoomByUUID(room.roomUUID);
|
|
}
|
|
|
|
public async void CreateAndJoinRoom()
|
|
{
|
|
var name = roomName.text;
|
|
if (name != "")
|
|
{
|
|
MainMenu.Instance.ShowLoading();
|
|
var req = new CreateRoomReq()
|
|
{
|
|
name = name,
|
|
};
|
|
try
|
|
{
|
|
await _lobbyInstance.CreateRoom(req);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
UIMessage.Instance.Show("Failed to create room. Please try again.");
|
|
MainMenu.Instance.HideLoading();
|
|
}
|
|
}
|
|
}
|
|
|
|
private string GetRandomName()
|
|
{
|
|
int index = Random.Range(0, _gameList.Count);
|
|
int num = Random.Range(1000, 9999);
|
|
return $"{_gameList[index]}-{num}";
|
|
}
|
|
}
|
|
}
|