Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
コルーチンで処理の待機、ただし進行も表示したい
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CoroutineSample1 : MonoBehaviour
{
void OnMouseDown()
{
StartCoroutine(ChangeColorCoroutine(1));
}
IEnumerator ChangeColorCoroutine(float time)
{
MaterialPropertyBlock materialBlock = new MaterialPropertyBlock();
Renderer renderer = GetComponent<Renderer>();
Color firstColor = Color.green;
Color endColor = Color.red;
float endTime = Time.time + time;
while( true ){
float diff = endTime - Time.time;
if( diff <= 0 ) { break; }
float rate = 1 - Mathf.Clamp01( diff / time);
materialBlock.SetColor("_Color", Color.Lerp(firstColor, endColor, rate));
renderer.SetPropertyBlock( materialBlock );
yield return null;
}
materialBlock.SetColor("_Color", Color.red);
renderer.SetPropertyBlock( materialBlock );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment