简体   繁体   English

光子为每个玩家创建一个新房间

[英]Photon creates a new room for each player

I have written my Photon script so that the player joins a random room, and if no room is found, the player will automatically create a new room. 我已经编写了Photon脚本,以便播放器加入一个随机的房间,如果找不到房间,播放器将自动创建一个新房间。 However, when I build and run my game on two different computers, no room is found on both of them, so they both create their own room. 但是,当我在两台不同的计算机上构建和运行游戏时,在两台计算机上都没有发现空间,因此它们都创建了自己的空间。 Please can someone tell me why? 请有人能告诉我为什么吗?

The game starts like it should when there only 1 player is required, but when 2 are required it doesn't, because of the issue I mentioned above. 由于我上面提到的问题,当只需要1个玩家时,游戏就应该像应该开始的那样,但是在不需要2个玩家时,它就应该像它那样开始。

using UnityEngine;
using System.Collections;

public class NetworkManager : Photon.PunBehaviour
{

public string playerprefabname = "player";
Vector3 spawner = new Vector3(9.9f, -3.8f, -0.1f);


// Use this for initialization
void Start()
{

    //Log stuff to console
    PhotonNetwork.logLevel = PhotonLogLevel.Full;

    //Connect
    PhotonNetwork.ConnectUsingSettings("v0.1");

    //Sync scenes
    PhotonNetwork.automaticallySyncScene = true;

}

//Display connection state on game
void OnGUI()
{
    GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}


public override void OnConnectedToMaster()
{
    PhotonNetwork.JoinLobby();
}

public override void OnJoinedLobby()
{
    PhotonNetwork.JoinRandomRoom();
}

//Create a room if fail to join one
void OnPhotonRandomJoinFailed()
{
    Debug.Log("Can't join random room!");
    RoomOptions roomOptions = new RoomOptions() { isVisible = false, maxPlayers = 2 };
    PhotonNetwork.CreateRoom(null, roomOptions, TypedLobby.Default);
}

// when joined to a room check if 3 players are there, then send to level
public override void OnJoinedRoom()
{
    if (PhotonNetwork.playerList.Length == 2)
    {
        Debug.Log("2 Players In Room Starting Level");

        GameObject myPlayer = PhotonNetwork.Instantiate(playerprefabname, spawner, spawnpoint.rotation, 0);

        //GameObject MyCam = PhotonNetwork.Instantiate ("Camera", CamPos, Quaternion.identity, 0);
        GameObject camera = GameObject.FindWithTag("MainCamera");

        if (camera != null)
        {
            CameraController followScript = camera.GetComponent("CameraController") as CameraController;
            if (followScript != null)
            {
                followScript.target = myPlayer;
            }
        }

    }
}

public override void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
{
    if (PhotonNetwork.playerList.Length == 2)
    {
        Debug.Log("2 Players In Room Starting Level");

        GameObject myPlayer = PhotonNetwork.Instantiate(playerprefabname, spawner, spawnpoint.rotation, 0);

        //GameObject MyCam = PhotonNetwork.Instantiate ("Camera", CamPos, Quaternion.identity, 0);
        GameObject camera = GameObject.FindWithTag("MainCamera");

        if (camera != null)
        {
            CameraController followScript = camera.GetComponent("CameraController") as CameraController;
            if (followScript != null)
            {
                followScript.target = myPlayer;
            }
        }

    }
}

} }

The reason is isVisible option set to false when you create the room. 原因是在创建房间时将isVisible选项设置为false。 Because of that room is not shown in room list and can not be chosen by random join. 由于该房间未显示在房间列表中,因此无法通过随机连接进行选择。

were the computers connected to each other like via LAN or wireless-ly? 这些计算机是通过局域网还是通过无线方式相互连接的? it creates a room when there is no connection between the two computers. 当两台计算机之间没有连接时,它将创建一个房间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM