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/26620b3e32a1cb67784f to your computer and use it in GitHub Desktop.
Save tsubaki/26620b3e32a1cb67784f to your computer and use it in GitHub Desktop.
コルーチンサンプルその3。その2の中断可能版
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