# TapBattleClient.GetRoomList
# 功能描述
获取房间列表,查看当前可用房间。可以根据房间类型过滤,支持分页查询。
# 方法签名
public static void GetRoomList(GetRoomListOption option)
# 参数说明
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| data | GetRoomListRequest | 否 | 获取房间列表请求数据 |
| success | Action<GetRoomListSuccessResponse> | 是 | 成功回调 |
| fail | Action<TapCallbackResult> | 否 | 失败回调 |
| complete | Action<TapCallbackResult> | 否 | 完成回调 |
# GetRoomListRequest
| 字段名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| roomType | string | 否 | 房间类型(不填则拉取全部类型的房间) |
| offset | int | 否 | 偏移量(默认0,第一次请求时为0) |
| limit | int | 否 | 请求获取的房间数量(默认20,最大100) |
# GetRoomListSuccessResponse
| 字段名 | 类型 | 说明 |
|---|---|---|
| rooms | RoomInfo[] | 房间列表 |
| errMsg | string | 成功时为 "getRoomList:ok" |
# RoomInfo
| 字段名 | 类型 | 说明 |
|---|---|---|
| id | string | 房间ID |
| name | string | 房间名称 |
| type | string | 房间类型 |
| ownerId | string | 房主ID |
| maxPlayerCount | int | 房间最大人数 |
| players | PlayerInfo[] | 房间内玩家列表 |
| customProperties | string | 自定义房间属性(JSON字符串) |
| createTime | string | 创建时间(时间戳) |
# 使用说明
# 注意事项
- 连接后可用 - 需要先连接服务器才能获取房间列表
- 分页查询 - 支持通过offset和limit进行分页查询
- 类型过滤 - 可以通过roomType参数过滤指定类型的房间
# 代码示例
# 示例1:获取所有房间列表
using UnityEngine;
using System.Collections.Generic;
public class RoomListController : MonoBehaviour
{
private List<RoomInfo> allRooms = new List<RoomInfo>();
public void FetchRoomList()
{
TapBattleClient.GetRoomList(new GetRoomListOption
{
data = new GetRoomListRequest
{
offset = 0,
limit = 20
},
success = (result) => {
allRooms.Clear();
allRooms.AddRange(result.rooms);
Debug.Log($"获取到 {result.rooms.Length} 个房间");
DisplayRoomList(result.rooms);
},
fail = (result) => {
Debug.LogError($"获取房间列表失败: {result.errMsg}");
}
});
}
private void DisplayRoomList(RoomInfo[] rooms)
{
foreach (var room in rooms)
{
Debug.Log($"房间: {room.name}, ID: {room.id}, 人数: {room.players.Length}/{room.maxPlayerCount}");
}
}
}
# 示例2:按类型过滤并分页加载
using UnityEngine;
public class RoomListPagination : MonoBehaviour
{
private int currentOffset = 0;
private const int pageSize = 20;
private string selectedRoomType = "竞技场";
public void LoadMoreRooms()
{
TapBattleClient.GetRoomList(new GetRoomListOption
{
data = new GetRoomListRequest
{
roomType = selectedRoomType,
offset = currentOffset,
limit = pageSize
},
success = (result) => {
if (result.rooms.Length > 0)
{
AddRoomsToUI(result.rooms);
currentOffset += result.rooms.Length;
Debug.Log($"加载了 {result.rooms.Length} 个房间,当前偏移: {currentOffset}");
}
else
{
Debug.Log("没有更多房间了");
}
}
});
}
private void AddRoomsToUI(RoomInfo[] rooms) { }
}
# 示例3:刷新房间列表
using UnityEngine;
public class RoomRefresher : MonoBehaviour
{
public void RefreshRoomList()
{
TapBattleClient.GetRoomList(new GetRoomListOption
{
success = (result) => {
Debug.Log($"刷新房间列表,共 {result.rooms.Length} 个房间");
UpdateRoomListUI(result.rooms);
}
});
}
private void UpdateRoomListUI(RoomInfo[] rooms)
{
// 清空现有列表
// 显示新的房间列表
}
}
# 相关API
- TapBattleClient.JoinRoom - 加入指定房间
- TapBattleClient.CreateRoom - 创建新房间
- TapBattleClient.MatchRoom - 自动匹配房间
