unity 对象 设置颜色或贴图
2017-05-11 本文已影响0人
木子才
直接上码:
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对象在场景中。上面的脚本挂在摄像机上面。
BAAA106E-ED96-4513-8EA7-FDBEE5991F45.png记得要拉入图像贴图到这里:
5AE18592-CC00-4BEE-AD93-CE22033D7A88.png
最初的样子:
BF4ABBF3-75F1-4858-A3D7-83E7091961CD.png
选择“添加颜色”:
92D6C65F-3927-4901-AE3B-D13237E79B39.png
选择“添加贴图”:
A9DAE1D9-EC85-4D27-812E-F0D8F9CA9616.png
选择“添加颜色和贴图”:
5F90AF57-76C0-49B2-978A-422F91BA9069.png