我有一个公共房间列表。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");
}
看到它正常工作真是太棒了。提前致谢。
中的层 Physics.IgnoreRaycastLayer
,或向该列表添加新层即可。默认情况下, Unity 已经有一个 ID 为 2 的“Ignore Raycast”层 。
层都在其自己的参数中包含该层 Raycast
此方法不起作用, layerMask
但游戏不太可能使用所有 32 个可用层。