using UnityEngine; using System.Collections; public class textMesh : MonoBehaviour { public Material meshRender = null; public Renderer rend; public Texture texture; void Start () { //把名字为bangongshi4 的材质球放到Resources文件夹下 meshRender = Resources.Load("bangongshi4") as Material; if (meshRender == null) { Debug.Log("11111111111"); return; } rend = GetComponent<Renderer>(); //rend.enabled = true; rend.sharedMaterial = meshRender;//代表这个对象的共享材质资源(这个是替换材质球) //MeshRenderer继承自Renderer所以上面定义成MeshRenderer也可以, //GetComponent<MeshRenderer>().materials[0] = meshRender;//这个表示找到对应的材质但是不能替换材质球, //GetComponent<MeshRenderer>().material.mainTexture = texture;//和上面的一样,可以替换材质的texture Debug.Log(GetComponent<Renderer>().material); Debug.Log(GetComponent<MeshRenderer>().material.mainTexture); } // Update is called once per frame void Update () { } }