JoinRandomRoom нет присоединился в одной комнате с 2 игроками? - PullRequest
0 голосов
/ 08 февраля 2020

это мой код ...

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 для единства.

любая идея, что может произойти или как решить

Ответы [ 2 ]

0 голосов
/ 11 февраля 2020

Вы можете использовать этот скрипт, и вы легко присоединитесь к обоим игрокам в одной комнате. Этот скрипт работает над моим проектом. убедитесь, что вы установили идентификатор приложения Photon, который создается из панели инструментов Photon.

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);
        }
    }
}
0 голосов
/ 11 февраля 2020

Если вы подключаетесь к Photon Cloud, убедитесь, что все клиенты подключаются к одному и тому же виртуальному приложению (AppId + AppVersion) и подключаются к одним и тем же серверам (в одном регионе). Go через " Контрольный список ".

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...