Unity 中的随机生成 (C#)

Random spawning in Unity (C#)

您好,我正在制作我的第一款 2D Unity 游戏,现在我有了这段代码。我在数组箭头中有 3 个不同的游戏对象(称它们为 a、b、c),我想知道生成了其中一个(这样我就可以在另一个函数中使用它)并从场景中删除前一个。现在它只是每 5 秒在另一个游戏对象上生成一个游戏对象,我不知道是哪个游戏对象随机生成的。有什么想法吗?

public GameObject[] arrows;
public float interval = 5;

// Use this for initialization
void Start()
{
    StartCoroutine(SpawnArrow());
}
public IEnumerator SpawnArrow()
{
    WaitForSeconds delay = new WaitForSeconds(interval);
    while (true)
    {
        GameObject prefab = arrows[UnityEngine.Random.Range(0, arrows.Length)];
        GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity);
        yield return delay;


    }
}`

我不知道这会有多大帮助,但您是否尝试过让每个单独的箭头成为一个对象,然后 return将该对象转变成您需要的方法。

public GameObject SpawnArrow(<some params>){
    GameObject prefab = arrows[UnityEngine.Random.Range(0, arrows.Length)];
    while(interval > 0){
        if(interval = 0){
            prefab = arrows[UnityEngine.Random.Range(0, arrows.Length)];
            GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity);
        } else {
            System.Threading.Thread.sleep(1000) // wait for the second.
            interval = interval - 1;
        }
    }
    return prefab;
}

IDK Unity - 从未使用过它,但您需要将函数重写为 return 箭头对象(不是延迟对象)到调用函数。当然,上面的代码应该被认为是无效的,只是伪代码,因为我不使用 unity。但要点只是找到一种更好的方法来生成一个可以return它的方法中的箭头。

要跟踪实例化了哪些对象,您可以创建 GameObjectList,然后在实例化对象时可以将其添加到 List 中。然后您可以使用该列表中的任何 GameObject 来做任何您需要的事情。

public GameObject[] arrows;
public float interval = 5;
private List<GameObject> myObjects = new List<GameObject>();

// Use this for initialization
void Start()
{
    StartCoroutine(SpawnArrow());
}
public IEnumerator SpawnArrow()
{
    WaitForSeconds delay = new WaitForSeconds(interval);
    while (true)
    {
        GameObject prefab = arrows[UnityEngine.Random.Range(0, arrows.Length)];
        GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity);

        //Add the object to the list
        myObjects.Add(clone);

        yield return delay;


    }
}

对于这样的生成对象,最好使用 InvokeRepeating,您可以将重复率以及您希望它何时开始的时间传递给它。 More information on InvokeRepeating

这是 InvokeRepeating

的样子
public GameObject[] arrows;
public float interval = 5;
private List<GameObject> myObjects = new List<GameObject>();

// Use this for initialization
void Start()
{
    InvokeRepeating("SpawnArrow", 0f, interval);
}

void SpawnArrow()
{
    //Check if there's something in the list
    if (myObjects.Count > 0)
    {
        //Destroy the last object in the list.
        Destroy(myObjects.Last());

        //Remove it from the list.
        myObjects.RemoveAt(myObjects.Count - 1);
    }

    GameObject prefab = arrows[UnityEngine.Random.Range(0, arrows.Length)];
    GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity);

    //Add the object to the list
    myObjects.Add(clone);

}

您可以通过执行 Destroy(myObjects.Last()); 来销毁列表中最后生成的对象,它将 return 列表中的最后一个对象并将其销毁。然后您还应该将其从列表中删除 myObjects.RemoveAt(myObjects.Count - 1);