Unity3d 没有和两个玩家加入同一个房间?

Unity3d 没有和两个玩家加入同一个房间?,unity3d,photon,Unity3d,Photon,这是我的密码 public class AutoLog : MonoBehaviourPunCallbacks { public void Connect() { if (!PhotonNetwork.IsConnected) { if (PhotonNetwork.ConnectUsingSettings()) { log.text += "\nConnected to Server"; }

这是我的密码

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";
}


}
当两个玩家进入时,他们不加入同一个房间,每个玩家创建另一个房间

我用Photon2表示团结


如果您连接到光子云,您是否知道会发生什么或如何解决问题,请确保所有客户端都连接到相同的虚拟应用程序(AppId+AppVersion)并连接到相同的服务器(相同的区域)。
   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";
}


}

仔细阅读“”。

您可以使用此脚本,轻松地将两位玩家加入一个房间。这个脚本正在我的项目中工作。确保设置了从光子仪表板创建的光子应用程序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);
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine;
使用光子双关语;
使用光子。实时;
公共类AutoLog:mono回调
{
字符串gameVersion=“1”;
专用字节maxPlayersPerRoom=2;
公共游戏对象控制面板;
公共游戏对象标签;
布尔断开连接;
无效唤醒()
{       
//#关键
//这确保我们可以在主客户端上使用PhotonNetwork.LoadLevel(),并且同一房间中的所有客户端自动同步其级别
PhotonNetwork.AutomaticallySyncScene=true;
}
void Start()
{
//Connect();
progressLabel.SetActive(false);
控制面板。设置激活(真);
}
公共void Connect()
{
断开连接=正确;
progressLabel.SetActive(true);
控制面板。设置激活(假);
//我们检查是否已连接,如果已连接,则加入,否则启动与服务器的连接。
Debug.Log(PhotonNetwork.IsConnected);
if(光网络断开)
{
//#此时我们需要尝试加入一个随机房间。如果失败,我们将在OnJoinRandomFailed()中收到通知,并创建一个。
PhotonNetwork.JoinRandomRoom();
}
其他的
{
//#至关重要的是,我们必须首先连接到光子在线服务器。
PhotonNetwork.GameVersion=游戏版本;
PhotoNetwork.ConnectUsingSettings();
Debug.Log(“已连接”);
}
}
公共覆盖无效OnConnectedToMaster()
{
如果(断开连接)
{
Log(“OnConnectedToMaster()”;
//#关键:我们尝试做的第一件事是加入一个潜在的现有房间。如果有,很好,否则,我们将使用OnJoinRandomFailed()返回
PhotonNetwork.JoinRandomRoom();
}
}
公共覆盖无效已断开连接(断开原因)
{
PhotonNetwork.Disconnect();
LogWarningFormat(“OnDisconnected()”,原因);
//progressLabel.SetActive(false);
//控制面板。设置激活(真);
}
公共重写void OnJoinRandomFailed(短返回码,字符串消息)
{
Log(“OnJoinRandomFailed()”;
//#关键:我们没有加入一个随机房间,可能没有,或者房间都满了。不用担心,我们创建了一个新房间。
CreateRoom(null,newRoomOptions{MaxPlayers=maxPlayersPerRoom});
}
公共覆盖void OnJoinedRoom()
{
Log(“OnJoinedRoom()”);
Debug.Log(PhotonNetwork.IsConnected);
如果(PhotonNetwork.CurrentRoom.PlayerCount==1)
{PhotonNetwork.LoadLevel(1);
调试日志(“主控室连接”);
//#关键
//把房间装好。
}
如果(PhotonNetwork.CurrentRoom.PlayerCount==2)
{
光电网络。负载水平(1);
}
}
}

您的
maxPlayer
值是多少?(您正在将其传递到文件室选项,但它在其他地方定义,未显示在代码示例中。)