随机对象产生(Unity 2D)

问题描述 投票:-1回答:1

我正在创建一个大亨游戏,我遇到了如何为客户生成随机对象的问题。这不是第一次,我在2年前从其他类型学习过Random Spawning,但是在Unity 5.3中,如果我没有错的话。所以我必须从头开始。所以我尝试了一个教程视频,这里是编码的方式,稍作修改:

public Text TimerText;
public float LevelTimeLimit;
private bool FullTime = false;

public Text TimerText;
public float LevelTimeLimit;
private bool FullTime = false;

public GameObject[] ObjPrefabs;


[System.Serializable]
public class myPool{
    public string tag;
    public GameObject Customer;
    public int ServiceTime;
}

public List<GameObject> CustomerPool;
public Dictionary<string, Queue<GameObject>> PoolDictionary;

public GameObject GetCust(string type)
{
    for (int i = 0; i < ObjPrefabs.Length; i++)
    {

        GameObject newObject = (GameObject)Instantiate(ObjPrefabs[i]);
        newObject.SetActive(false);
        CustomerPool.Add(newObject);

    }

    return null;
}

我的问题是它不会产生随机客户。谁知道我错过了什么?

c# unity3d pool
1个回答
0
投票
for (int i = 0; i < ObjPrefabs.Length; i++)
{

    GameObject newObject = (GameObject)Instantiate(ObjPrefabs[i]);
    newObject.SetActive(false);
    CustomerPool.Add(newObject);

}

此代码生成列表中的每个预制件。如果你想从列表中生成随机预制件,请尝试用此代码替换它。

GameObject newObject = (GameObject)Instantiate(ObjPrefabs[Random.Range(0, ObjPrefabs.Length)]);

random.range(0, ObjPrefabs.Length) - 从0ObjPrefabs.Length挑选随机数。

并从ObjPrefabs获取该位置的元素。

© www.soinside.com 2019 - 2024. All rights reserved.