IEnumerator start()の正しい書き方
IEnumerator
は非同期の書き方ですが UnityではStart()
は初期化の時に使う関数なのでそれを非同期に関数の名前にしてしまうとStart()
を使いたいときに使えなくなるのでほんとはよろしくない書き方だと思うのでIEnumerator e()
という関数を作ってそれを非同期したのですがStart
を非同期にするのとどう違うのでしょうか?
public class Emitter : MonoBehaviour {
public GameObject[] waves;
private int currentWave;
// Use this for initialization
/*
IEnumerator Start ()
{
if(waves.Length == 0)
{
yield break;
}
while(true)
{
Debug.Log("コルーチン");
GameObject wave = (GameObject)Instantiate(waves[currentWave],
transform.position,
Quaternion.identity);
wave.transform.parent = transform;
while(wave.transform.childCount != 0)
{
yield return new WaitForEndOfFrame();
}
Destroy(wave);
if (waves.Length <= ++currentWave)
{
currentWave = 0;
}
}
}
*/
private IEnumerator e()
{
if (waves.Length == 0)
{
yield break;
}
while (true)
{
Debug.Log("コルーチン");
GameObject wave = (GameObject)Instantiate(waves[currentWave],
transform.position,
Quaternion.identity);
wave.transform.parent = transform;
while (wave.transform.childCount != 0)
{
yield return new WaitForEndOfFrame();
}
Destroy(wave);
if (waves.Length <= ++currentWave)
{
currentWave = 0;
}
}
}
private void Start()
{
StartCoroutine(e());
}
// Update is called once per frame
void Update () {
}
}