Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active January 19, 2016 10:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsubaki/cf9f8d0beca60fa09db0 to your computer and use it in GitHub Desktop.
Save tsubaki/cf9f8d0beca60fa09db0 to your computer and use it in GitHub Desktop.
Unity コルーチンサンプルその2。処理の待ち合わせ
using UnityEngine;
using System.Collections;
public class CoroutineSample1 : MonoBehaviour
{
bool isRunning = false;
Coroutine coroutine;
[SerializeField]
Renderer leftRenderer = null, rightRenderer = null;
void OnMouseDown()
{
StartCoroutine(ChangeColorCoroutine());
}
IEnumerator ChangeColorCoroutine()
{
if( isRunning ) { yield break; }
isRunning = true;
var renderer = GetComponent<Renderer>();
ChangeColor(renderer, Color.blue);
var leftCoroutine = StartCoroutine(ChangeColorCoroutineWithRuntimetime(leftRenderer));
var rightCoroutine = StartCoroutine(ChangeColorCoroutineWithRuntimetime(rightRenderer));
yield return leftCoroutine;
yield return rightCoroutine;
yield return new WaitForSeconds(0.2f);
ChangeColor(renderer, Color.white);
isRunning = false;
}
IEnumerator ChangeColorCoroutineWithRuntimetime(Renderer renderer)
{
ChangeColor(renderer, Color.red);
float waitSec = Random.Range(0.2f, 1f);
yield return new WaitForSeconds(waitSec);
ChangeColor(renderer, Color.green);
}
void ChangeColor(Renderer renderer, Color color)
{
renderer.material.color = color;
DynamicGI.UpdateMaterials(renderer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment