如何断言 spawn 是 "filled"?

How to assert a spawn is "filled"?

我有两个出生点,玩家会在连接后出现。

我需要这样,当其中一名玩家连接到其中一个地点时,任何其他玩家将始终在另一个地点重生。

这里有一些视觉效果,希望对您有所帮助:https://goo.gl/Y0ohZC

提前致谢, IC

您可以将这两个可能的生成点添加为空游戏对象。 然后我会制作一个布尔数组并根据该位置是否被占用将其状态设置为 true 或 false。这些点没有直接存储在这个数组中,所以你应该制作另一个数组。 在 C# 中,这大致如下所示:

    public GameObject[] Spots = new GameObject[2]; //Drag the spots in here (In the editor)
bool[] OccupiedSpawnSpots = new bool[2];
int mySpotNumber = -1;

void Start() {
    PhotonNetwork.ConnectUsingSettings ("v1.0.0");
}

void OnGUI() {
    GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
}

void OnJoinedLobby() 
{
    Debug.Log ("Joined the lobby");
    PhotonNetwork.JoinRandomRoom ();
}

void OnPhotonRandomJoinFailed() 
{
    Debug.Log ("No room found. Creating a new one...");
    PhotonNetwork.CreateRoom (null);
}

void OnPhotonPlayerConnected(PhotonPlayer player)
{
    //If we are the MasterClient, send the list to the connected player
    if (PhotonNetwork.isMasterClient)
        GetComponent<PhotonView>().RPC("RPC_SendList", player, OccupiedSpawnSpots);
}

void OnApplicationQuit()
{
    //If I am outside and others want to connect, my spot shouldn't be still set as occupied:
    //I mean if the application quits I'm of course going to be disconnected. 
    //You have to do this in every possibility of getting disconnected or leaving the room
    OccupiedSpawnSpots[mySpotNumber] = false;
    //Send the changed List to the others
    GetComponent<PhotonView>().RPC("RPC_UpdateList", PhotonTargets.All, OccupiedSpawnSpots);
}

[RPC]
void RPC_SendList(bool[] ListOfMasterClient)
{
    OccupiedSpawnSpots = ListOfMasterClient;

    //Get the free one
    if (OccupiedSpawnSpots[0] == false)
    {
        //Spawn your player at 0
        SpawnMyPlayer(0);
        //Set it to occupied
        OccupiedSpawnSpots[0] = true;
    } 
    else //so the other must be free
    {
        //Spawn your player at 1
        SpawnMyPlayer(1);
        //Set it to occupied
        OccupiedSpawnSpots[1] = true;
    }

    //Send the changed List to the others
    GetComponent<PhotonView>().RPC("RPC_UpdateList", PhotonTargets.All, OccupiedSpawnSpots);
}

[RPC]
void RPC_UpdateList(bool[] RecentList)
{
    OccupiedSpawnSpots = RecentList;
}

void SpawnMyPlayer(int SpotNumber) {
    // Check if spawnspots are set OK
    if (Spots == null) {
        Debug.LogError ("SpawnSpots aren't assigned!");
        return;
    }

    mySpotNumber = SpotNumber;

    // The player object for the network
    GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate ("PlayerController", 
                                                                   Spots[SpotNumber].transform.position,
                                                                   Spots[SpotNumber].transform.rotation, 0);

    // Enable a disabled script for *this player only, or all would have the same camera, movement, etc
    //((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;

    // Set a camera just for this player
    //myPlayerGO.transform.FindChild ("Main Camera").gameObject.SetActive (true);

    //standbyCamera.SetActive(false);
}

注意:如果你有两个以上的玩家,它会变得更加困难。此代码未优化,因此您可能会找到更好的代码,但它应该会给您正确的想法。

有什么不明白的请追问...