如何在unity3d中阻止瓷砖在生成一定数量后产生

时间:2018-10-14 20:45:41

标签: unity3d spawn

我有一个脚本可以生成无尽的图块(3D),我希望它停在一定数量(例如100)之后,在最后一个图块的末尾(第100个)结束时,我想生成另一个预制件,例如门户。 我不知道该怎么做。 我在Google上找到的任何答案都无法帮助。 非常感谢您的帮助。

void Start()
{

    nextTileLocation = startPoint;
    nextTileRotation = Quaternion.identity;
    for (int i = 0; i < initSpawnNum; ++i) 
    {
        SpawnNextTile (i >= initNoObstacles);
    }


}


public void SpawnNextTile(bool spawnObstacles = true)
{
    var newTile = Instantiate (tile, nextTileLocation, nextTileRotation);

    var nextTile = newTile.Find ("Next Spawn Point");

    nextTileLocation = nextTile.position;
    nextTileRotation = nextTile.rotation;

    if (!spawnObstacles)
        return;

    var obstacleSpawnPoints = new List<GameObject> ();

    foreach (Transform child in newTile) {

        if (child.CompareTag ("ObstacleSpawn")) {

            obstacleSpawnPoints.Add (child.gameObject);
        }
    }

    if (obstacleSpawnPoints.Count > 0) {

        var spawnPoint = obstacleSpawnPoints [Random.Range (0, obstacleSpawnPoints.Count)];

        var spawnPos = spawnPoint.transform.position;

        var newObstacle = Instantiate (obstacle, spawnPos, Quaternion.identity);

        newObstacle.SetParent (spawnPoint.transform);
    } 
}

1 个答案:

答案 0 :(得分:0)

您可以执行类似的操作来传递布尔值,以告知该函数生成门户。

void Start()
{
    nextTileLocation = startPoint;
    nextTileRotation = Quaternion.identity;

    for (int i = 0; i < initSpawnNum; ++i) 
    {
        // Pass in true for the last tile
        SpawnNextTile (i >= initNoObstacles, i == initSpawnNum - 1);
    }
}

public void SpawnNextTile(bool spawnObstacles = true, bool shouldSpawnPortal = false)
{
   //
}

编辑以显示将图块添加到列表中

class YourScript : MonoBehaviour
{
    private List<GameObject> tiles = new List<GameObject>();

    private void SpawnNextTile(bool spawnObstacles = true, bool shouldSpawnPortal = false)
    {
        var newTile = Instantiate (tile, nextTileLocation, nextTileRotation);
        tiles.Add(newTile);

        // The rest of your code
    }
}