如何在 Unity3D 中阻止磁贴在一定数量后生成



我有一个脚本,可以生成无尽的图块(3D(,我希望它在某个数字(例如100(之后停止,在最后一个图块(第100个(的末尾,我想生成另一个预制件,如门户。 我不知道该怎么做。 我在谷歌上找到的任何答案都无济于事。 任何帮助都非常感谢。

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);
} 
}

你可以做一些事情,比如传入一个布尔值来告诉函数生成一个门户。

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
}
}

相关内容

  • 没有找到相关文章

最新更新