看到很多博客说PhotonNetwork.GetCustomRoomList()的第二个参数给null,但实际这个方法内部存在null判断,会进行报错。
public static bool GetCustomRoomList(TypedLobby typedLobby, string sqlLobbyFilter)
查看官方文档可知,第二个参数sqlLobbyFilter是一个SQL。
示例:
public class GameProperties
{
public const string CustomRoomProperties0="C0";
public const string CustomRoomProperties1="C1";
public const string CustomRoomProperties2="C2";
public const string CustomRoomProperties3="C3";
public const string CustomRoomProperties4="C4";
public const string CustomRoomProperties5="C5";
public const string CustomRoomProperties6="C6";
public const string CustomRoomProperties7="C7";
public const string CustomRoomProperties8="C8";
public const string CustomRoomProperties9="C9";
public const string TypedLoabbyName = "XXX_SqlLobby";
public const string SqlLobbyFilter = "XXX_Room";
}
public void CreateRoom()
{
//创建房间
RoomOptions options = new RoomOptions { MaxPlayers = CONST_MAXPLAYERS };
//Only integer-typed and string-typed values are allowed and once a value was assigned to any column in a specific lobby
//只允许使用整数类型和字符串类型的值,一旦将值分配给特定大厅中的任何列,此列就会锁定为该类型的值。
//所以在这里给需要使用的SQL过滤属性进行赋值
options.CustomRoomProperties = new Hashtable() { { GameProperties.CustomRoomProperties0, GameProperties.SqlLobbyFilter } };
//列出所需要使用的SQL filtering properties
options.CustomRoomPropertiesForLobby = new string[] { GameProperties.CustomRoomProperties0 };
PhotonNetwork.CreateRoom(roomName, options, new TypedLobby(GameProperties.TypedLoabbyName, LobbyType.SqlLobby));
}
public void RefreshRoomList()
{
//调用刷新房间列表
PhotonNetwork.GetCustomRoomList(new TypedLobby(GameProperties.TypedLoabbyName, LobbyType.SqlLobby), $"{GameProperties.CustomRoomProperties0}='{GameProperties.SqlLobbyFilter}'");
}
调用虽然成功了,但有可能因为LobbyType.SqlLobby类型的房间是不会自动触发public override void OnRoomListUpdate(List<RoomInfo> roomList)
回调的。需要调用RefreshRoomList()
后才会触发。
并且很关键的一点,当有玩家从房间退出后的短时间内,无论时自动还是通过调用PhotonNetwork.GetCustomRoomList
来刷新房间列表,都不会获取到房间人数减少。