我正在制作无限公路游戏。我正在使用对象池来处理道路和敌人的物体。道路运作良好,但我对敌人的物体有问题,我可以从游泳池添加敌人,但我不能将敌人的物体归还给游泳池。
public void CreateEnemy( EnemyPool pool, int roadLenght, Vector3 startPos , int numberOfEnemies, int enemyType )
{
enemy = new Transform[numberOfEnemies];
enemyIndex = new int[numberOfEnemies];
currentRoadLenght = roadLenght;
//I add enemies along the length of the path. Path Lenght is randomly generated.
currentEnemyNumber = numberOfEnemies;
int arrayedEnemyObject = 0;
for( int i = 0; i < roadLenght; i++ )
{
Vector3 pos = startPos + new Vector3(0, 1, i * 15);
for( int j = 0; j < numberOfEnemies; j++ )
{
Transform obj = pool.PullEnemyFromPool(enemyType);
obj.position = pos;
obj.gameObject.SetActive(true);
enemy[j] = obj;
enemyIndex[j] = enemyType;
Debug.Log(j);
pos.z += 3;
arrayedEnemyObject++;
}
if( arrayedEnemyObject == numberOfEnemies )
{
arrayedEnemyObject = 0;
i += enemyObjectDistance;
}
}
}
public void DestroyEnemy( EnemyPool objectPooler )
{
if (enemy != null)
{
int destroyedObj = 0;
for( int i = 0; i < currentRoadLenght; i++ )
{
for( int j = 0; j < currentEnemyNumber; j++ )
{
Transform obj = enemy[j];
obj.gameObject.SetActive( false );
objectPooler.AddEnemyToPool( enemyIndex[j], obj );
destroyedObj++;
}
if( destroyedObj == currentEnemyNumber )
{
destroyedObj = 0;
i += enemyObjectDistance;
}
}
enemy = null;
}
}
我的EnemyPoolScript
public class EnemyPool : MonoBehaviour
{
private GameObject[] enemyObjects;
private List<Transform>[] enemyObjectsPool;
public void FillPool( GameObject[] enemyObjects , int size )
{
this.enemyObjects = enemyObjects;
Vector3 pos = Vector3.zero;
Quaternion tilt = Quaternion.identity;
GameObject obj;
enemyObjectsPool = new List<Transform>[enemyObjects.Length];
for ( int i = 0; i < enemyObjects.Length; i++ )
{
enemyObjectsPool[i] = new List<Transform>();
for( int j = 0; j < size; j++ )
{
obj = Instantiate( enemyObjects[i], pos, tilt ) as GameObject;
obj.SetActive( false );
enemyObjectsPool[i].Add( obj.transform );
}
}
}
public void AddEnemyToPool( int index, Transform obj )
{
enemyObjectsPool[index].Add (obj);
}
public Transform PullEnemyFromPool( int index )
{
Transform obj;
if( enemyObjectsPool[index].Count <= 0 )
{
obj = ( Instantiate( enemyObjects[index], Vector3.zero, Quaternion.identity ) as GameObject ).transform;
}
else
{
obj = enemyObjectsPool[index][0];
enemyObjectsPool[index].RemoveAt (0);
}
return obj;
}
}
答案 0 :(得分:3)
为什么要在添加和删除过程中记录道路长度。这可能是你的问题所在。您多次在阵列中设置相同的项目。你正在把你的游泳池中的物品拉出每条道路,并将它放在同一个槽中。
for (int i = 0; i < roadCount; i++)
enemy[j] = pullObject;
外环可能是罪魁祸首。
此外,在您的池中,使用队列而不是列表可能会有所帮助。队列专门用于从前面删除项目,这比RemoveAt(0)
修改强>
好吧......它有点难以解释,但你的外环可能并没有做你认为它做的事情。你所有的外循环正在做的是确保你重复你的内循环roudcount次数。我们假装你的道路长度为3,并且有2个敌人。这是你的程序正在做的事情:
pos = startPos + new Vector3(0,1,0);
obj.position = pos;
enemy[0] = obj;
pos.z += 3;
arrayedEnemeyObject++;
obj.position = pos;
enemy[1] = obj;
pos.z += 3;
arrayedEnemeyObject++;
arrayedEnemeyObject = 0;
i += enemyObjectDistance;
pos = startPos + new Vector3(0,1,15);
obj.position = pos;
enemy[0] = obj;
pos.z += 3;
arrayedEnemeyObject++;
obj.position = pos;
enemy[1] = obj;
pos.z += 3;
注意敌人[0]如何设置两次。我仍然不知道什么是道路距离,因为你的外环并没有做任何事情。我......猜测,这就是你想要的......但我真的不知道
enemy[,] = new Transform[roadCount, numberOfEnemies];
enemyIndex[,] = new int[roadCount, numberOfEnemies];
然后在你的内循环中:
enemy[i,j] = obj;