Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created May 5, 2015 14:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/1a09eaefe601d29f543d to your computer and use it in GitHub Desktop.
Save tsubaki/1a09eaefe601d29f543d to your computer and use it in GitHub Desktop.
クリック毎に色を切り替える。ついでにテキストへ文字を出力したり。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ColorChanger : MonoBehaviour {
private IEnumerator<Color> action;
[SerializeField] private UnityEngine.UI.Text text;
void Update () {
if( Input.GetMouseButtonDown(0)){
if( action == null ){ action = Actions(); }
action.MoveNext();
}
}
IEnumerator<Color> Actions(){
// 色を塗るために使用するパラメータを取得する
var renderer = GetComponent<Renderer>();
MaterialPropertyBlock block = new MaterialPropertyBlock();
while(true){
// 赤く塗る
block.SetColor("_Color", Color.red);
renderer.SetPropertyBlock(block);
transform.position = Vector3.right;
text.text = "RED";
yield return Color.red;
// 青く塗る
block.SetColor("_Color", Color.blue);
renderer.SetPropertyBlock(block);
transform.position = Vector3.right + Vector3.up;
text.text = "BLUE";
yield return Color.blue;
// 緑に塗る
block.SetColor("_Color", Color.green);
renderer.SetPropertyBlock(block);
transform.position = Vector3.up;
text.text = "GREEN";
yield return Color.green;
// 白く塗る
block.SetColor("_Color", Color.white);
renderer.SetPropertyBlock(block);
transform.position = Vector3.zero;
text.text = "WHITE";
yield return Color.white;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment