Unity学习笔记011.动态更改物体材质渲染方式

目录

  • Utils
  • 调用

Utils

namespace Utils
{
	public class UtilsRenderingSetting
    {
        public enum RenderingMode
        {
            Opaque,
            Cutout,
            Fade,
            Transparent
        }
        //设置材质的渲染模式
        public static void setMaterialRenderingMode(Material material, RenderingMode renderingMode)
        {
            switch (renderingMode)
            {
                case RenderingMode.Opaque:
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                    material.SetInt("_ZWrite", 1);
                    material.DisableKeyword("_ALPHATEST_ON");
                    material.DisableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = -1;
                    break;
                case RenderingMode.Cutout:
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                    material.SetInt("_ZWrite", 1);
                    material.EnableKeyword("_ALPHATEST_ON");
                    material.DisableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = 2450;
                    break;
                case RenderingMode.Fade:
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                    material.SetInt("_ZWrite", 0);
                    material.DisableKeyword("_ALPHATEST_ON");
                    material.EnableKeyword("_ALPHABLEND_ON");
                    material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = 3000;
                    //material.SetFloat("" _Mode & quot;", 2);
                    break;
                case RenderingMode.Transparent:
                    material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                    material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                    material.SetInt("_ZWrite", 0);
                    material.DisableKeyword("_ALPHATEST_ON");
                    material.DisableKeyword("_ALPHABLEND_ON");
                    material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
                    material.renderQueue = 3000;
                    break;
            }
        }
    }
}

调用

	private Color hideColor = new Color(1.0f, 1.0f, 1.0f, 0.5f);
    private Color showColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);

	/// 
    /// 播放时把toushi改成透明(运动动画	0-120)
    /// 
    public void TouShiStart()
    {
        for (int i = 0; i < CRG.GetComponent<MeshRenderer>().materials.Length; i++)
        {
            UtilsRenderingSetting.setMaterialRenderingMode(CRG.GetComponent<MeshRenderer>().materials[i], UtilsRenderingSetting.RenderingMode.Fade);
            CRG.GetComponent<MeshRenderer>().materials[i].color = hideColor;
        }

        for (int i = 0; i < toushi.childCount; i++)
        {
            for (int j = 0; j < toushi.GetChild(i).GetComponent<MeshRenderer>().materials.Length; j++)
            {
                UtilsRenderingSetting.setMaterialRenderingMode(toushi.GetChild(i).GetComponent<MeshRenderer>().materials[j], UtilsRenderingSetting.RenderingMode.Fade);
                toushi.GetChild(i).GetComponent<MeshRenderer>().materials[j].color = hideColor;
                //Debug.Log(toushi.GetChild(i).GetComponent().materials[j].name + toushi.GetChild(i).GetComponent().materials[j].color.ToString());                
            }
        }
    }

 	/// 
    /// 播放完毕把toushi改回不透明
    /// 
    public void TouShiEnd()
    {
        for (int i = 0; i < CRG.GetComponent<MeshRenderer>().materials.Length; i++)
        {
            UtilsRenderingSetting.setMaterialRenderingMode(CRG.GetComponent<MeshRenderer>().materials[i], UtilsRenderingSetting.RenderingMode.Opaque);
            CRG.GetComponent<MeshRenderer>().materials[i].color = showColor;
        }
        
        for (int i = 0; i < toushi.childCount; i++)
        {
            for (int j = 0; j < toushi.GetChild(i).GetComponent<MeshRenderer>().materials.Length; j++)
            {
                UtilsRenderingSetting.setMaterialRenderingMode(toushi.GetChild(i).GetComponent<MeshRenderer>().materials[j], UtilsRenderingSetting.RenderingMode.Opaque);
                toushi.GetChild(i).GetComponent<MeshRenderer>().materials[j].color = showColor;
            }
        }
    }

你可能感兴趣的:(UnityProject)