简体   繁体   English

随机对象生成 (Unity 2D)

[英]Random Object Spawning (Unity 2D)

I'm Currently creating a tycoon game, and i've got a problem on how to spawn random objects for the customer.我目前正在创建一个大亨游戏,我在如何为客户生成随机对象方面遇到了问题。 this is not the first time, i learned Random Spawning once, 2 ago from other genre, but in Unity 5.3, if i'm not wrong.这不是第一次,我在 2 年前从其他类型中学习了一次随机生成,但是在 Unity 5.3 中,如果我没记错的话。 so i have to start from scratch again.所以我必须从头开始。 so i tried a tutorial video, and here's how the coding goes, with a little modification:所以我尝试了一个教程视频,这里是编码的过程,稍作修改:

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;
}

my problem is it won't spawn random customer.我的问题是它不会产生随机客户。 anybody know what did i miss?有人知道我错过了什么吗?

for (int i = 0; i < ObjPrefabs.Length; i++)
{

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

}

this code spawn each of the prefabs in the list.此代码生成列表中的每个预制件。 if you want to spawn random prefab from the list try to replace it with this code.如果您想从列表中生成随机预制件,请尝试用此代码替换它。

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

random.range(0, ObjPrefabs.Length) - pick random number from 0 to ObjPrefabs.Length . random.range(0, ObjPrefabs.Length) - 从0ObjPrefabs.Length选择随机数。

and take from ObjPrefabs the element in that position.并从ObjPrefabs该位置的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM