unity 协程 实现物体颜色动态切换


public class ChangeColor : MonoBehaviour {
    private MeshRenderer meshRender;
 // Use this for initialization
 void Start () {
        meshRender = GetComponent();
        StartCoroutine(ChangeColors());
    }
 
 // Update is called once per frame
 void Update () {
        this.gameObject.transform.Rotate(Vector3.back,800*Time.deltaTime); //实现物体一直绕中心旋转
           
 }
    IEnumerator ChangeColors()
    {
        yield return new WaitForSeconds(0.1f);
        meshRender.material.color = Color.blue;     //蓝 红 黑 三色切换
        yield return new WaitForSeconds(0.1f);
        meshRender.material.color = Color.red;
        yield return new WaitForSeconds(0.1f);
        meshRender.material.color = Color.black;
        yield return StartCoroutine(ChangeColors());

        //yield return new WaitForSeconds(0.1f);
        //meshRender.material.SetColor("_Color", new Color(0.5f,0.8f,1f,1f));
       // yield return new WaitForSeconds(0.1f);
       // meshRender.material.SetColor("_Color", new Color(1f, 1f, 1f, 1f));
       // yield return StartCoroutine(ChangeColors());  //控制颜色的RGDB值 0-255对应0-1



    }
}

你可能感兴趣的:(unity 协程 实现物体颜色动态切换)