unity协程
下面一段小代码是通过一定的延迟,把脚本的挂载物体变为蓝色
1 public float f; 2 void Update() 3 { 4 if (Input.GetKeyDown(KeyCode.W)) 5 { 6 StartCoroutine("Fade"); 7 } 8 } 9 IEnumerator Fade() 10 { 11 for (f = 0f; f<=1f; f +=0.1f) 12 { 13 14 this.GetComponent<MeshRenderer>().material.color = new Color(0,0,f); 15 yield return new WaitForSeconds(.5f);//延迟 16 } 17 }
官方实例:https://docs.unity.cn/cn/current/Manual/Coroutines.html