纳金网

标题: Unity3D使用C#实现Coroutines & Yield(二) [打印本页]

作者: 会飞的鱼    时间: 2011-12-20 15:05
标题: Unity3D使用C#实现Coroutines & Yield(二)
最后附上学习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  发表
作者: 奇    时间: 2012-1-26 23:22
健康是最佳的礼物,知足是最大的财富,信心是最好的品德,关心是最真挚的问候,牵挂是最无私的思念,祝福是最美好的话语。祝你新年快乐!平安幸福!

作者: C.R.CAN    时间: 2012-2-12 23:30
不错 非常经典  实用

作者: tc    时间: 2012-3-4 23:29
人过留名!

作者: 晃晃    时间: 2012-3-26 23:33
呵呵,很漂亮啊

作者: 晃晃    时间: 2012-4-10 23:18
百度的叫度娘,网易的叫易娘,新浪内部还在为是叫新娘还是浪娘而争论不休!……不管你们是企鹅的额娘,豆瓣的伴娘,还是华为的伪娘,都要记得,淘宝才是你们的亲娘啊!亲!!

作者: 奇    时间: 2012-5-7 23:24
我也来支持下

作者: tc    时间: 2012-5-15 23:20
路过、路过、快到鸟,列位请继续...ing

作者: C.R.CAN    时间: 2012-5-28 23:25
非常感谢,管理员设置了需要对新回复进行审核,您的帖子通过审核后将被显示出来,现在将转入主题

作者: 晃晃    时间: 2012-6-7 23:18
读铁系缘分,顶铁系友情

作者: tc    时间: 2012-7-1 23:22
我看看就走,你们聊!

作者: tc    时间: 2012-7-8 23:18
不错哦,顶一下......

作者: 晃晃    时间: 2012-7-29 23:26
其实楼主所说的这些,俺支很少用!

作者: 奇    时间: 2012-8-13 23:54
都闪开,介个帖子,偶来顶

作者: tc    时间: 2012-8-22 00:18
加精、加亮滴铁子,尤其要多丁页丁页

作者: 菜刀吻电线    时间: 2012-9-22 23:25
先顶上去,偶要高亮加精鸟!

作者: tc    时间: 2012-10-19 23:18
加精、加亮滴铁子,尤其要多丁页丁页

作者: 晃晃    时间: 2013-2-2 23:31
我是老实人,我来也!

作者: 奇    时间: 2013-2-25 23:31
跑着去顶朋友滴铁

作者: 奇    时间: 2013-3-1 23:30
先顶上去,偶要高亮加精鸟!





欢迎光临 纳金网 (http://wwww.narkii.com/club/) Powered by Discuz! X2.5