不加入房间 ||光子双关语
我最近正在使用 Unity3d 和 Photon PUN2 开发我的多人游戏;
我想制作一个系统,玩家可以点击按钮加入房间,如果房间不可用然后创建;
当两个玩家(parrelSync)从p1开始游戏时,它只会使房间正常(因为在开始时没有可用的房间)
从p2开始,它不会加入房间
< em>这里是代码
string roomID = "002";
string maxPlayers = "10";
RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = (byte)int.Parse(maxPlayers);
if(PhotonNetwork.CurrentRoom != null){
PhotonNetwork.JoinRoom(roomID);
}else{
PhotonNetwork.CreateRoom(roomID, roomOptions);
}
}
I am recently working on my multiplayer game Using Unity3d and Photon PUN2;
I want to make a system where player clicks the button to join the room if it is not avilabe then create;
When staringt the game from two players (parrelSync) from p1 it just make a room normal (because in starting no rooms are available)
form p2 it does not join the room
here is the code
string roomID = "002";
string maxPlayers = "10";
RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = (byte)int.Parse(maxPlayers);
if(PhotonNetwork.CurrentRoom != null){
PhotonNetwork.JoinRoom(roomID);
}else{
PhotonNetwork.CreateRoom(roomID, roomOptions);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PhotonNetwork.CurrentRoom 返回您当前所在的房间,在您的情况下该房间始终为空。
无需检查
PhotonNetwork.CurrentRoom
,只需使用PhotonNetwork.JoinRoom
并覆盖OnJoinRoomFailed()
,当我们尝试加入时将调用该方法。roomID
的房间尚不存在,只需在其中创建新房间即可。像下面这样的东西会起作用
并覆盖
OnJoinRoomFailed()
确保您的脚本派生自MonoBehaviourPunCallbacks
类PhotonNetwork.CurrentRoom
returns the room which you are currently in, which will always be null in your case.Instead of checking for
PhotonNetwork.CurrentRoom
just usePhotonNetwork.JoinRoom
and also override theOnJoinRoomFailed()
which will be called when we attempt to join the room withroomID
which does not exist yet, and in it just create new room.Something like following would work
And to override
OnJoinRoomFailed()
make sure your script is derived fromMonoBehaviourPunCallbacks
class