- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
最后附上学习Coroutines & Yield时所做的小例子,脚本的作用是不断随机改变材质的颜色,演示demo使用”V字仇杀队”中的面具。
01 using UnityEngine;
02 using System.Collections;
03
04 public class RandomColor : MonoBehaviour {
05
06 public float delayInSecond = 1;
07 public Material targetMaterial;
08
09 // Use this for initialization
10 void Start () {
11 StartCoroutine(AutoChangeColor());
12 }
13
14 // Update is called once per frame
15 void Update () {
16 }
17
18 IEnumerator AutoChangeColor()
19 {
20 yield return 0; //确保Time.deltaTime为0
21
22 Color colorNew = GenerateRandomColor();
23 Color colorNow = targetMaterial.GetColor("_Color");
24 float timeEclapsed = 0;
25 for (timeEclapsed = 0; timeEclapsed < delayInSecond; timeEclapsed += Time.deltaTime)
26 {
27 float progress = timeEclapsed / delayInSecond;
28 Color colorTween = new Color(
29 (colorNew.r - colorNow.r) * progress + colorNow.r,
30 (colorNew.g - colorNow.g) * progress + colorNow.g,
31 (colorNew.b - colorNow.b) * progress + colorNow.b
32 );
33 targetMaterial.SetColor("_Color", colorTween);
34 yield return 1;
35 }
36
37 StartCoroutine(AutoChangeColor());
38 }
39
40 Color GenerateRandomColor(){
41 Color color = new Color();
42 color.r = Random.value;
43 color.g = Random.value;
44 color.b = Random.value;
45
46 return color;
47 }
48 }
由 uke 发表 |
|