脚本挂在游戏物体上,并且物体要有材质
using UnityEngine;
using System.Collections;
public class RandomColor : MonoBehaviour
{
// Use this for initialization
void Start()
{
//获取组件 材质.颜色
this.gameObject.GetComponent<MeshRenderer>().material.color = RandomColor1();//给游戏物体添加下方随机颜色方法
}
/*float timer;
//随着时间变换颜色
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
this.gameObject.GetComponent<MeshRenderer>().material.color = RandomColor1();
timer = 1;
}
}*/
public Color RandomColor1()
{
float r = Random.Range(0f, 1f);
float g = Random.Range(0f, 1f);
float b = Random.Range(0f, 1f);
Color color = new Color(r, g, b);
return color;
}
}