Photon为每位玩家创造了一个新的空间

时间:2015-10-19 16:00:35

标签: c# unity3d unity5 photon

我已经编写了我的Photon脚本,以便玩家加入随机房间,如果找不到房间,玩家将自动创建一个新房间。但是,当我在两台不同的计算机上构建并运行我的游戏时,两者都没有找到空间,所以他们都创建了自己的房间。请有人能告诉我原因吗?

当只需要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;
            }
        }

    }
}

}

2 个答案:

答案 0 :(得分:1)

原因是创建房间时isVisible选项设置为false。 因为房间没有显示在房间列表中,所以无法通过随机连接选择。

答案 1 :(得分:0)

是通过局域网还是无线连接到彼此的计算机?当两台计算机之间没有连接时,它会创建一个房间。