unity3d - JoinRandomRoom 没有加入 2 个玩家的同一个房间?
问题描述
这是我的代码...
public class AutoLog : MonoBehaviourPunCallbacks
{
public void Connect()
{
if (!PhotonNetwork.IsConnected)
{
if (PhotonNetwork.ConnectUsingSettings())
{
log.text += "\nConnected to Server";
}
else
{
log.text += "\nFalling Connecting to Server";
}
}
}
public override void OnConnectedToMaster()
{
connect.interactable = false;
join.interactable = true;
}
…………
public void JoinRandom()
{
if (!PhotonNetwork.JoinRandomRoom())
{
log.text += "\nFail Joinned Room";
}
}
知道会发生什么或如何解决
public override void OnJoinRandomFailed(short returnCode, string message)
{
log.text += "\nNo Rooms to Join, creating one...";
if(PhotonNetwork.CreateRoom(null, new Photon.Realtime.RoomOptions() { MaxPlayers = maxPlayer }))
{
log.text += "\nRoom Create";
}
else
{
log.text += "\nFail Creating Room";
}
}
public override void OnJoinedRoom()
{
log.text += "\nJoinned";
}
}
当 2 名玩家进入他们不加入同一个房间时,每个玩家创建另一个房间。
我使用 Photon2 来统一。
知道会发生什么或如何解决
解决方案
您可以使用此脚本,轻松地将两个玩家加入一个房间。该脚本正在处理我的项目。确保您设置了从 photon 仪表板创建的 Photon App ID。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class AutoLog : MonoBehaviourPunCallbacks
{
string gameVersion = "1";
private byte maxPlayersPerRoom = 2;
public GameObject controlPanel;
public GameObject progressLabel;
bool isConnecting;
void Awake()
{
// #Critical
// this makes sure we can use PhotonNetwork.LoadLevel() on the master client and all clients in the same room sync their level automatically
PhotonNetwork.AutomaticallySyncScene = true;
}
void Start()
{
// Connect();
progressLabel.SetActive(false);
controlPanel.SetActive(true);
}
public void Connect()
{
isConnecting = true;
progressLabel.SetActive(true);
controlPanel.SetActive(false);
// we check if we are connected or not, we join if we are , else we initiate the connection to the server.
Debug.Log(PhotonNetwork.IsConnected);
if (PhotonNetwork.IsConnected)
{
// #Critical we need at this point to attempt joining a Random Room. If it fails, we'll get notified in OnJoinRandomFailed() and we'll create one.
PhotonNetwork.JoinRandomRoom();
}
else
{
// #Critical, we must first and foremost connect to Photon Online Server.
PhotonNetwork.GameVersion = gameVersion;
PhotonNetwork.ConnectUsingSettings();
Debug.Log("<color=green>Connected </color>");
}
}
public override void OnConnectedToMaster()
{
if (isConnecting)
{
Debug.Log(" OnConnectedToMaster() ");
// #Critical: The first we try to do is to join a potential existing room. If there is, good, else, we'll be called back with OnJoinRandomFailed()
PhotonNetwork.JoinRandomRoom();
}
}
public override void OnDisconnected(DisconnectCause cause)
{
PhotonNetwork.Disconnect();
Debug.LogWarningFormat("OnDisconnected()", cause);
// progressLabel.SetActive(false);
//controlPanel.SetActive(true);
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
Debug.Log("OnJoinRandomFailed() ");
// #Critical: we failed to join a random room, maybe none exists or they are all full. No worries, we create a new room.
PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = maxPlayersPerRoom });
}
public override void OnJoinedRoom()
{
Debug.Log(" OnJoinedRoom() ");
Debug.Log(PhotonNetwork.IsConnected);
if (PhotonNetwork.CurrentRoom.PlayerCount == 1)
{ PhotonNetwork.LoadLevel(1);
Debug.Log("Master Connected in Room");
// #Critical
// Load the Room Level.
}
if (PhotonNetwork.CurrentRoom.PlayerCount == 2)
{
PhotonNetwork.LoadLevel(1);
}
}
}
推荐阅读
- javascript - 尝试在 JavaScript 中递归查找属性
- python - Python 导师 + 谷歌 Colab
- sql - 具有数字的相关子查询等于 where 条件下的子查询
- css - 如何使用 Sass Lint 允许重复的 CSS 属性?
- javascript - 使用 socket.io-stream 节点包通过 socket io 将文件从服务器发送到客户端(浏览器)
- python-3.x - 如何将输出打印到python中的文本文件
- python - 将 numpy.fill 与 Numba 'nopython' 模式一起使用
- django - 在 Django / Postgres 中选择每个组的最新项目的高性能方法
- c# - C# 如何从 SQL 数据库中填充对象列表
- c++ - C++ 中正确的核裂变模拟