直接上码:
public class test04 : MonoBehaviour {
//对象
private GameObject obj;
//渲染器
private Renderer render;
//贴图
public Texture textture;
// Use this for initialization
void Start () {
obj = GameObject.Find ("Cube");
render = obj.GetComponent<Renderer> ();
}
// Update is called once per frame
void Update () {
}
void OnGUI() {
if (GUILayout.Button ("添加颜色", GUILayout.Width(100), GUILayout.Height (50))) {
//为了避免残留,将贴图置空
render.material = null;
//修改渲染颜色为绿色
render.material.color = Color.green;
}
if (GUILayout.Button ("添加贴图", GUILayout.Width(100), GUILayout.Height (50))) {
//为了避免残留,将贴图置空
render.material = null;
//设置贴图
render.material.mainTexture = textture;
}
if (GUILayout.Button ("添加颜色和贴图", GUILayout.Width(100), GUILayout.Height (50))) {
//为了避免残留,将贴图置空
render.material = null;
//修改渲染颜色为绿色
render.material.color = Color.green;
//设置贴图
render.material.mainTexture = textture;
}
}
}
创建一个Cube对象在场景中。上面的脚本挂在摄像机上面。
记得要拉入图像贴图到这里:
最初的样子:
选择“添加颜色”:
选择“添加贴图”:
选择“添加颜色和贴图”: