Last active
August 29, 2015 14:18
-
-
Save tsubaki/26620b3e32a1cb67784f to your computer and use it in GitHub Desktop.
コルーチンサンプルその3。その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; | |
using System.Collections.Generic; | |
public class CoroutineSample2 : MonoBehaviour | |
{ | |
bool isRunning = false; | |
Coroutine coroutine; | |
[SerializeField] | |
Renderer leftRenderer = null, rightRenderer = null; | |
Stack<Coroutine> coroutineStack = new Stack<Coroutine>(); | |
void OnMouseDown() | |
{ | |
var coroutine = StartCoroutine(ChangeColorCoroutine()); | |
coroutineStack.Push (coroutine ); | |
} | |
void OnMouseUp() | |
{ | |
foreach( var coroutine in coroutineStack ){ | |
StopCoroutine(coroutine); | |
} | |
ChangeColor(GetComponent<Renderer>(), Color.white); | |
ChangeColor(leftRenderer, Color.green); | |
ChangeColor(rightRenderer, Color.green); | |
isRunning = false; | |
} | |
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)); | |
coroutineStack.Push (leftCoroutine ); | |
coroutineStack.Push (rightCoroutine ); | |
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