Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/ecdf84e27e59ed6ce72d to your computer and use it in GitHub Desktop.
Save tsubaki/ecdf84e27e59ed6ce72d to your computer and use it in GitHub Desktop.
戻り値の受け取り
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CoroutineSample1 : MonoBehaviour
{
IEnumerator Start ()
{
Debug.Log ("start");
var ie1 = download ();
var coroutine1 = StartCoroutine (ie1);
var ie2 = download ();
var coroutine2 = StartCoroutine (ie2);
//処理を待ち合わせる
yield return coroutine1;
yield return coroutine2;
// 戻り値を取得
float downloadTime1 = (float)ie1.Current;
float downloadTime2 = (float)ie2.Current;
// 合計をログに出力
float sum = downloadTime1 + downloadTime2;
Debug.LogFormat ("finish! same : {0}", sum);
}
IEnumerator download ()
{
// ランダムな秒数(0.2~1sec)間待つ
float waitSec = Random.Range (0.2f, 1f);
yield return new WaitForSeconds (waitSec);
// 戻り値を返す
yield return waitSec;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment