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/6845e31dcd8b906001a1 to your computer and use it in GitHub Desktop.
Save tsubaki/6845e31dcd8b906001a1 to your computer and use it in GitHub Desktop.
コルーチン1サンプルの中断可能版
using UnityEngine;
using System.Collections;
public class CoroutineSample1 : MonoBehaviour
{
bool isRunning = false;
Coroutine coroutine;
void OnMouseDown()
{
coroutine = StartCoroutine(ChangeColorCoroutine());
}
void OnMouseUp()
{
StopCoroutine(coroutine);
ChangeColorCoroutine(renderer, Color.green);
isRunning = false;
}
IEnumerator ChangeColorCoroutine()
{
if( isRunning ) { yield break; }
isRunning = true;
var renderer = GetComponent<Renderer>();
ChangeColorCoroutine(renderer, Color.red);
// 0.5秒待つ
yield return new WaitForSeconds(0.5f);
ChangeColorCoroutine(renderer, Color.green);
isRunning = false;
}
void ChangeColorCoroutine(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