-
-
Save tsubaki/1a09eaefe601d29f543d to your computer and use it in GitHub Desktop.
クリック毎に色を切り替える。ついでにテキストへ文字を出力したり。
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 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