簡體   English   中英

Unity中的隨機產卵(C#)

[英]Random spawning in Unity (C#)

嗨,我正在2D制作我的第一個團結游戲,現在我有了這個代碼。 我在數組箭頭中有3個不同的GameObjects(稱為a,b,c),我想知道它們中的哪一個被生成(所以我可以在另一個函數中使用它)並從場景中刪除前一個。 現在它只是每隔5秒在另一個上生成一個GameObject,我不知道它們中的哪一個隨機生成它。 任何的想法?

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;


    }
}`

要跟蹤您實例化的對象,可以創建List of GameObject ,然后在實例化對象時,可以將其添加到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 ,你可以傳遞它的重復率,並在你希望它開始時。 有關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());Destroy(myObjects.Last());列表中的最后一個衍生對象Destroy(myObjects.Last()); 它將返回列表中的最后一個對象並將其銷毀。 然后,您還應該從列表myObjects.RemoveAt(myObjects.Count - 1);刪除它myObjects.RemoveAt(myObjects.Count - 1);

我不知道這會有多大幫助,但你是否嘗試將每個箭頭作為一個對象,然后將該對象返回到你需要它的方法。

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 - 從未使用它,但您需要重寫函數以將箭頭對象(不是延遲對象)返回給調用函數。 當然上面的代碼應該被視為不工作,只是偽代碼,因為我不使用統一。 但要點只是找到一種更好的方法來生成一個可以返回它的方法中的箭頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM