简体   繁体   English

试图限制房间最大玩家,但房间选项不能统一工作,光子

[英]trying to limit the room max players but room options does not work in unity , photon

I've created a game room that can be joined and i want it to have 2 players max.我创建了一个可以加入的游戏室,我希望它最多有 2 名玩家。 I used the code bellow but other players still can join even if the room has already 2 players.我使用了下面的代码,但即使房间已经有 2 个玩家,其他玩家仍然可以加入。 What can i do to make it work?我该怎么做才能让它发挥作用?

public class CreateAndJoinRooms : MonoBehaviourPunCallbacks
{
    
    public TMP_InputField createInput;
    public TMP_InputField UserNameInput;
    public TMP_InputField joinInput;


    public void CreateRoom()
    {
        if (string.IsNullOrEmpty(UserNameInput.text) == false && string.IsNullOrEmpty(createInput.text) == false)
        { 
            PhotonNetwork.LocalPlayer.NickName = UserNameInput.text;
            RoomOptions roomOptions = new RoomOptions();
            roomOptions.MaxPlayers = 2;

            PhotonNetwork.CreateRoom(createInput.text);
        }
        else Debug.LogError("Username or room name is empty");
            
             
      

    }
    
    public void JoinRoom()
    {



        if (string.IsNullOrEmpty(UserNameInput.text) == false)
        {
            PhotonNetwork.LocalPlayer.NickName = UserNameInput.text;
            PhotonNetwork.JoinRoom(joinInput.text);
        }
        else Debug.LogError("Username or room name is empty");
        


    }

    public override void OnJoinedRoom()
    {

        
        PhotonNetwork.LoadLevel("Game");
        

    }

}
 

You need to pass the roomOptions as a parameter to the CreateRoom function.您需要将 roomOptions 作为参数传递给 CreateRoom function。

PhotonNetwork.LocalPlayer.NickName = UserNameInput.text;

RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = 2;

PhotonNetwork.CreateRoom(createInput.text, roomOptions, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM