时间:2022-06-20 10:24:17 | 栏目:.NET代码 | 点击:次
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 () { } }
补充:unity同个物体多个材质替换处理
我就废话不多说了,大家还是直接看代码吧~
public Material[] MatAry; public Material[] mat; public GameObject obj; MatAry = obj.GetComponent<Renderer>().materials; MatAry = mat; obj.GetComponent<Renderer>().materials = MatAry;