这是我的代码...
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用于统一。
任何想法都会发生什么或如何解决
答案 0 :(得分:0)
如果连接到Photon Cloud,请确保所有客户端都连接到相同的虚拟应用程序(AppId + AppVersion)并连接到相同的服务器(相同区域)。 仔细检查“ docs”。
答案 1 :(得分:0)
您可以使用此脚本,轻松地将两个玩家加入一个房间。这个脚本正在我的项目上。确保您设置了从光子仪表板创建的“光子应用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);
}
}
}