LoginSignup
1
1

More than 5 years have passed since last update.

UnityでのCoroutine取り扱いメモ

Last updated at Posted at 2018-03-05

UnityでCoroutineを利用する際のメモ

IEnumeratorに参照を入れてコルーチンを一時停止・再開させるとき

  • 入れ子呼び出しは再開されない
  • WaitForSecondsは内部でカウントが止まらない。

一時停止・再開できるサウンド再生完了コールバック

AudioSourceExtention.cs
public static IEnumerator PlayWithCompCallback(this AudioSource audioSource, AudioClip audioClip, float volume = 1f, UnityAction compCallback = null)
{
    audioSource.clip = audioClip;
    audioSource.volume = volume;
    audioSource.Play();
    float timer = 0f;

    //WaitForSecondsを使うとCoroutineを一時停止・再開できなくなるのでwhileで対応//
    while (timer < audioClip.length)
    {
        timer += Time.deltaTime;
        yield return null;
    }

    if (compCallback != null)
    {
        compCallback();
    }
}
1
1
3

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