我有一个公共房间列表。public override void OnRoomListUpdate(List)
我有一个公共房间列表。
public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
foreach (RoomInfo room in roomList)
{
if (room.RemovedFromList)
{
cachedRoomList.Remove(room);
}
else
{
int index = cachedRoomList.FindIndex(r => r.Name == room.Name);
if (index != -1)
{
cachedRoomList[index] = room;
}
else
{
cachedRoomList.Add(room);
}
}
}
UpdatePublicRoomUI();
}
void UpdatePublicRoomUI()
{
foreach (Transform roomButton in publicRoomContainer)
{
Destroy(roomButton.gameObject);
}
foreach (RoomInfo room in cachedRoomList)
{
CreatePublicRoomButton(room.Name, room.PlayerCount);
}
}
void CreatePublicRoomButton(string roomName, int playerCount)
{
GameObject roomButton = Instantiate(publicRoomButton, publicRoomContainer);
roomButton.SetActive(true);
roomButton.transform.GetChild(0).GetComponent<TextMeshProUGUI>().SetText(roomName);
roomButton.transform.GetChild(1).GetComponent<TextMeshProUGUI>().SetText(playerCount.ToString());
UnityEngine.UI.Button button = roomButton.GetComponent<UnityEngine.UI.Button>();
button.onClick.AddListener(() =>
{
JoinRoom(roomName);
});
}
因此基本上,每个将选项 IsVisible
设置为 true 的房间都会显示在这里。一切正常,直到玩家离开房间然后创建一个房间。无论房间是否公开,它都不会显示在这里。以下是离开房间的代码:
public void QuitToMenu()
{
if (PhotonNetwork.InRoom)
{
PhotonNetwork.LeaveRoom();
PhotonNetwork.LoadLevel("Menu");
}
}
以下是创建和加入房间的功能:
public void CreateRoom(string roomName, bool isPublic)
{
RoomOptions roomOptions = new()
{
IsVisible = isPublic,
IsOpen = true,
MaxPlayers = 10
};
PhotonNetwork.CreateRoom(roomName, roomOptions);
}
public void JoinRoom(string roomName)
{
PhotonNetwork.JoinRoom(roomName);
}
public override void OnJoinedRoom()
{
string nickname = nickname_input.text.Trim();
if (nickname.Length > 0)
{
PlayerPrefs.SetString("Nickname", nickname);
PhotonNetwork.NickName = nickname;
}
PhotonNetwork.LoadLevel("Game");
}
看到它正常工作真是太棒了。提前致谢。