Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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