unity 代码替换材质贴图

动态替换贴图:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIDataShow : MonoBehaviour
{
    public GameObject Obj_;
    public MeshRenderer meshRenderer;
    public List<Texture>  textures = new List<Texture>(); //贴图  
    void Awake()
    {
       
    }

    private void SetToggle(bool bo, int index)
    {
        if (bo)

        Obj_.SetActive(true);
        //替换贴图(Albedo属性)
        meshRenderer.material.SetTexture("_MainTex", textures[index]);

        //将贴图赋给材质球的properties属性
        meshRenderer.material.SetTexture("_Emission", textures[index]);

       // Material mat = gameObject.GetComponent<MeshRenderer>().material; // 获取材质
       // mat.SetFloat("_BumpScale", 0.5f); // 设置 Shader 中某 Float 变量的值
        //mat.SetFloat("_变量名", 新值);

       // Material mat = gameObject.GetComponent<MeshRenderer>().material; // 获取材质
       // mat.SetColor("_Color", new Color(1, 0, 0, 1)); // 设置 Shader 中某 Color 变量的值
        // mat.SetTexture("_MainTex", tex); // 当然如果有需要也可设置贴图
        //mat.SetFloat("_变量名", 新值);


    }


  

}

猜你喜欢

转载自blog.csdn.net/qq_37524903/article/details/131071378