好吧,我在 Unity 中使用 C# 工作,我创建了一些简单的函数来根据玩家的 z 位置无限期地生成一系列平台(游戏对象)。一旦游戏对象安全地看不见,我就会删除它们,并在每次玩家向前移动时调用此更新方法:
void spawnSectorGroup()
{
int i = numSectorsToSpawn; //get position of last sectors, when its exceeded by player then delete and spawn again
while (i >= 0) {
int j = Random.Range (0, sectors.Length);
spawnSector (gamePosition.position, sectors [j]);
i--;
}
}
void checkAndUpdateSectors()
{
//print ("Player pos"+playerPosition.position);
//print ("last sector"+gamePosition.position);
if (gamePosition.position.z - playerPosition.position.z <= 20) { //safe value ahead
print ("theyre almost there, spawn new group");
print (gamePosition.position.z - playerPosition.position.z);
spawnSectorGroup ();
}
//destroy past ones
GameObject[] objects = FindObjectsOfType(typeof(GameObject)) as GameObject[];
//GameObject[] objects = GameObject.FindGameObjectsWithTag("Block");
foreach (GameObject o in objects) {
if (o.gameObject.tag == "Block" || o.gameObject.tag == "Cage" || o.gameObject.tag == "Animal" || o.gameObject.tag == "Enemy") {
if (playerPosition.position.z - o.transform.position.z >= 100) { //safe aways back
print ("destroying past object");
print (o.transform.position);
Destroy (o.gameObject);
}
}
}
}
void spawnSector(Vector3 relativePosition,GameObject sector){
GameObject newSector = GameObject.Instantiate (sector, relativePosition, transform.rotation) as GameObject;
gamePosition.position += newSector.GetComponent<Sector> ().length;
}
这一切都行得通,但是从长远来看,我担心如果每次玩家在最后一个生成的扇区的 20 个左右单位范围内生成大约 10 个新“扇区”会累积并从众多游戏对象中产生滞后。
我没有无限期产卵的经验 - 这会是个问题吗?或者这是无限期产卵的好习惯?