Last active
January 19, 2016 10:21
-
-
Save tsubaki/cf9f8d0beca60fa09db0 to your computer and use it in GitHub Desktop.
Unity コルーチンサンプルその2。処理の待ち合わせ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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