对象在运行时实例化,但每个对象之间有延迟

时间:2018-03-21 19:24:04

标签: c# unity3d

我想要做的是在圆圈中实例化一个预制件,但随着时间的推移。因此,一个预制件将出现,其他预制件将随着时间的推移而出现。我应该使用协程来达到这个效果吗?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class Fire_Circle : MonoBehaviour
 {

 public GameObject prefab;
 public int numberOfObjects = 20;
 public float radius = 5f;
 public float height;
 void Start()
 {
    for (int i = 0; i < numberOfObjects; i++)

    {

        float angle = i * Mathf.PI * 2 / numberOfObjects;
        Vector3 pos = new Vector3(Mathf.Cos(angle),  height / radius, 
        Mathf.Sin(angle)) * radius;
        Instantiate(prefab, pos, prefab.transform.rotation );
        }
    }
}

1 个答案:

答案 0 :(得分:1)

协程当然是个不错的选择:

void Start()
{
   StartCoroutine(SpawnObjs());
}

IEnumerator SpawnObjs()
{
    for (int i = 0; i < numberOfObjects; i++)
    {
        float angle = i * Mathf.PI * 2 / numberOfObjects;
        Vector3 pos = new Vector3(Mathf.Cos(angle),  height / radius, 
        Mathf.Sin(angle)) * radius;
        Instantiate(prefab, pos, prefab.transform.rotation );
        yield return new WaitForSeconds(.8f);
    }
}