1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UnityのStartCoroutine ~待機~

Last updated at Posted at 2015-03-07

 まず間違いなく使用される機能などなので、なんとなく理解していたけど適当に使っていたけど、しっかり仕組みを理解して最適な使い方をしたい!!ということで調べてみました。

まずはいつも通り最小形

NewBehaviourScript.cs
using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
	void Start () {
		StartCoroutine (Sample());
	}

	private IEnumerator Sample () {

		yield return null;
	}
}

特に説明不要だと思いますが、Startから始まって、Sampleが呼ばれて、終了してStartに戻る普通のプログラム。

待機

次にWaitForSecondsを入れてみる。

NewBehaviourScript.cs
using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

	void Start () {
		StartCoroutine (Sample());
		Debug.Log ("Start");
	}

	private IEnumerator Sample () {
		yield return new WaitForSeconds(1.0f);
		Debug.Log ("Sample");
	}
}

実行結果
StartCoroutine1.PNG

 Startから始まって、Sampleが呼ばれて、Startに戻る。ここでStartは処理を再開され、Sampleは1秒待ってから処理が再開される。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?