需要在程序代码中设置材质"Standard (Specular setup)"的渲染模式,则编写如下代码:
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;
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;
}
}
执行代码:
Material currentMaterial = new Material(Shader.Find("Standard (Specular setup)"));
SetMaterialRenderingMode (currentMaterial, RenderingMode.Cutout);