首页 > 解决方案 > 进入房间的最少玩家数量 PHOTON

问题描述

我正在做一个多人游戏,我想知道如何为用户添加最少数量的玩家进入房间。喜欢在至少连接一名玩家之前不要孤单。我应该在我的脚本中添加什么?我有这个房间选项功能,但它不能像 Min 播放器或其他东西一样工作。(我预计 MaxPlayers 是否也存在 MinPlayers)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon;
using Photon.Pun;
using UnityEngine.UI;
using Photon.Realtime;

public class MPManager : MonoBehaviourPunCallbacks, IPunObservable
{
    public PlayFabAuth auth;

    


    public string GameVersion;
    public Text connectState;
    
    public GameObject[] DisableOnConnected;
    public GameObject[] DisableOnJoinRoom;
    public GameObject[] EnableOnConnected;
    public GameObject[] EnableOnJoinRoom;
    public LoadedPlayer loadPlayer;
    // Start is called before the first frame update
    void Start()
    {
       

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void FixedUpdate()
    {
        connectState.text = "Connection: " + PhotonNetwork.NetworkClientState;
    }
    public void ConnectToMaster()
    {
        //  PhotonNetwork.connectionStateDetailed
        PhotonNetwork.ConnectUsingSettings();
    }

    public override void OnConnectedToMaster()
    {
        PhotonNetwork.JoinLobby();
    }
    public override void OnJoinedLobby(){
        
        foreach(GameObject disable in DisableOnConnected){
            disable.SetActive(false);
        }
        foreach (GameObject enable in EnableOnConnected){
            enable.SetActive(true);
        }
        
        
    }

    public void CreateOrJoin()
    {
        PhotonNetwork.LeaveLobby();
        PhotonNetwork.JoinRandomRoom();
    }
    public override void OnJoinRandomFailed(short returnCode, string message)
    {

        RoomOptions rm = new RoomOptions
        {
            
            MaxPlayers = 3,
            IsVisible = true

        };
        int rndID = Random.Range(0, 3000);
        PhotonNetwork.CreateRoom("Default: " + rndID, rm, TypedLobby.Default);
    }
   
    public override void OnJoinedRoom()
    {
        foreach (GameObject disable in DisableOnJoinRoom)
        {
            disable.SetActive(false);
        }
        foreach (GameObject enable in EnableOnJoinRoom)
        {
            enable.SetActive(true);

        }
        Debug.Log(loadPlayer.currentPlayer.name);

        GameObject player =  PhotonNetwork.Instantiate(loadPlayer.currentPlayer.name , Vector3.zero, Quaternion.identity, 0);
        

    }
    public override void OnLeftRoom()
   
    {
        PhotonNetwork.LeaveRoom();
       
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        throw new System.NotImplementedException();
    }
}

标签: unity3dphoton

解决方案


感谢您选择光子!

PhotonNetwork.CurrentRoom.PlayerCount当您加入房间 ( OnJoinedRoom) 或其他玩家加入房间 ( ) 时,您需要获取演员的数量( OnPlayerEnteredRoom)。当加入的演员数量对您来说足够时,启动游戏逻辑,例如加载场景、发送自定义事件等。


推荐阅读