首页 > 解决方案 > 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 来统一。

知道会发生什么或如何解决

标签: unity3dphoton

解决方案


您可以使用此脚本,轻松地将两个玩家加入一个房间。该脚本正在处理我的项目。确保您设置了从 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);
        }
    }
}

推荐阅读