C# 如何在一个脚本中使用池列表中的两个对象?

C# 如何在一个脚本中使用池列表中的两个对象?,c#,unity3d,pooling,C#,Unity3d,Pooling,我试图理解对象池。我能够让脚本一次拉一个对象,但我需要能够同时从列表中拉三个或更多对象 我的对象池脚本很大,所以除非有必要,否则我真的不想共享整个脚本 我需要能够更改火焰繁殖的位置,因此我创建了一个脚本: private void CreateWavesForFlames(GameObject flame, float xBase, float xDisplacement, float dropHeight) { flame.transform.position = new Vect

我试图理解对象池。我能够让脚本一次拉一个对象,但我需要能够同时从列表中拉三个或更多对象

我的对象池脚本很大,所以除非有必要,否则我真的不想共享整个脚本

我需要能够更改火焰繁殖的位置,因此我创建了一个脚本:

 private void CreateWavesForFlames(GameObject flame, float xBase, float xDisplacement, float dropHeight)
 {
    flame.transform.position = new Vector3(xBase + xDisplacement, dropHeight, 0);
    flame.SetActive(true); //this turn the pooled object on
} 
所以我需要同时产生三个火焰并改变它们的产生位置

wave call看起来像这样:

void Wave1() {
    Debug.Log("Wave1");
    tempGOHolder = gm.GetLargeFire();


    CreateWavesForFlames(tempGOHolder, 0, 0, 12);
    CreateWavesForFlames(tempGOHolder, 10, 0, 12);
    CreateWavesForFlames(tempGOHolder, 15, 0, 12);

}
GameObject GetLargeFire()
{
    return availableGameObject;
}
所发生的事情是只创建了一个火焰,它使用了最后一个CreateWavesforFlames。我需要这三个不同


任何关于如何做到这一点的建议都将非常棒。

好吧。。这就是您的代码所期望的。如果需要3个不同的flame对象,则必须执行此操作(假设“gm”是池管理器对象):


我知道这个问题已经得到了回答,但我认为有更好的解决办法。上面的答案很好。假设您希望通过不重复调用
GetLargeFire()
函数来缩短代码,您可以使用下面的方法

假设池脚本中的
GetLargeFire()
函数如下所示:

void Wave1() {
    Debug.Log("Wave1");
    tempGOHolder = gm.GetLargeFire();


    CreateWavesForFlames(tempGOHolder, 0, 0, 12);
    CreateWavesForFlames(tempGOHolder, 10, 0, 12);
    CreateWavesForFlames(tempGOHolder, 15, 0, 12);

}
GameObject GetLargeFire()
{
    return availableGameObject;
}
您可以创建
GetLargeFire()
函数的重载,以填充传递给它的数组。我不建议返回数组,因为这样会分配内存,所以池脚本就没用了。填充传入的数组更好

public void GetLargeFire(GameObject[] gOBJ, int amountToReturn)
{
    for (int i = 0; i < gOBJ.Length; i++)
    {
        if (i < amountToReturn)
        {
            gOBJ[i] = GetLargeFire();
        }
        else
        {
            //Fill the rest with null to override what was inside of it previously
            gOBJ[i] = null;
        }
    }
}

您甚至可以使用loop创建具有
CreateWavesForFlames
的波浪,代码更少。

。这是一个简单的解决方案。我不认为我能重复,但显然我能!谢谢。你也可以扩展你的
GetLargeFire()
来返回一个游戏对象数组,并在数组上循环,而不是重复这些行n次。我必须弄清楚这一点。谢谢你的建议。