0

我已经编写了我的 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;
            }
        }

    }
}

}

4

2 回答 2

2

原因是创建房间时 isVisible 选项设置为 false。因为那个房间没有显示在房间列表中,不能通过随机加入来选择。

于 2015-10-20T09:05:01.227 回答
0

计算机是通过 LAN 还是通过无线方式相互连接的?当两台计算机之间没有连接时,它会创建一个房间。

于 2015-10-19T16:07:21.893 回答