unity3d - 进入房间的最少玩家数量 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();
}
}
解决方案
感谢您选择光子!
PhotonNetwork.CurrentRoom.PlayerCount
当您加入房间 ( OnJoinedRoom
) 或其他玩家加入房间 ( ) 时,您需要获取演员的数量( OnPlayerEnteredRoom
)。当加入的演员数量对您来说足够时,启动游戏逻辑,例如加载场景、发送自定义事件等。
推荐阅读
- typescript - 在运行时分析类型或接口并将一些逻辑应用于其成员
- python - 鉴于两个数组使用numpy具有相同数量的元素,如何将矩阵重塑为等于给定矩阵的形状?
- c# - xUnit:传递包含对静态字段的引用的对象作为理论
- widget - 如何在 android 小部件中使用新线程
- java - 在GridLayout java swing中移动JLabel
- spring - Spring transactions - 新事务中的异常导致父事务中的回滚
- mysql - 如何使用节点 js 模型和 mysql 处理日期
- postgresql - 将 Terraform 的输出(数据库密码)传递到 CICD 管道中的 Kubernetes 清单
- java - 使用 Tomcat 在 Eclipse 中调试远程应用程序
- angular - 函数总是在响应来自嵌套函数之前执行