实例化一定数量的prefabs,unity c#
Instantiate a certain amount of prefabs, unity c#
我想知道如何实例化一定数量的预制件 c#。
例如:
if(Input.GetKeyDown(KeyCode.K))
{
//Instantiate 20 prefabs
}
按下按钮时,运行 一个循环,循环直到达到 Space
值 -1
然后在循环中每次实例化一个预制件。这应该在 Update
函数中完成。如果你这样做正确,它不应该继续实例化。
int Space = 20;
public GameObject prefab;
void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
//Instantiate 20 prefabs
for (int i = 0; i < Space; i++)
{
GameObject obj = Instantiate(prefab);
obj.transform.position = new Vector3(0, 0, 0);
}
}
}
我会创建一个带有单个整数参数的简单方法,允许您指定要创建的敌人数量。您很可能需要使用其他逻辑来处理它们的位置,除非它是在库存槽对象本身内处理的。
public GameObject enemyPrefab;
//runs every frame, to check for the key press in this case
public void Update ()
{
//did we press the specified key? (C)
if (Input.GetKeyDown(KeyCode.C))
{
//call our method to create our enemies!
CreateEnemies(20);
}
}
public void CreateEnemies (int enemies)
{
//the amount of enemies we want to have
//run through this loop until we hit the amount of enemies we want
for (int i = 0; i < enemies; i++)
{
//create the new enemy based on the provided prefab
Instantiate(enemyPrefab, Vector3.zero, Quaternion.identity);
}
}
我想知道如何实例化一定数量的预制件 c#。
例如:
if(Input.GetKeyDown(KeyCode.K))
{
//Instantiate 20 prefabs
}
按下按钮时,运行 一个循环,循环直到达到 Space
值 -1
然后在循环中每次实例化一个预制件。这应该在 Update
函数中完成。如果你这样做正确,它不应该继续实例化。
int Space = 20;
public GameObject prefab;
void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
//Instantiate 20 prefabs
for (int i = 0; i < Space; i++)
{
GameObject obj = Instantiate(prefab);
obj.transform.position = new Vector3(0, 0, 0);
}
}
}
我会创建一个带有单个整数参数的简单方法,允许您指定要创建的敌人数量。您很可能需要使用其他逻辑来处理它们的位置,除非它是在库存槽对象本身内处理的。
public GameObject enemyPrefab;
//runs every frame, to check for the key press in this case
public void Update ()
{
//did we press the specified key? (C)
if (Input.GetKeyDown(KeyCode.C))
{
//call our method to create our enemies!
CreateEnemies(20);
}
}
public void CreateEnemies (int enemies)
{
//the amount of enemies we want to have
//run through this loop until we hit the amount of enemies we want
for (int i = 0; i < enemies; i++)
{
//create the new enemy based on the provided prefab
Instantiate(enemyPrefab, Vector3.zero, Quaternion.identity);
}
}