'How to change Material Rendering Mode to Fade by script?
The material rendering mode is now set by default to opaque. I want to change it to fade.
So far i did :
if (g.GetComponent<SkinnedMeshRenderer>() != null)
{
Material[] mats = g.GetComponent<SkinnedMeshRenderer>().materials;
}
The material is at index 0 there is only one material but i'm not sure how to access the rendering mode of the material and how to change it to fade.
What i tried so far :
I created a new public static class :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class MaterialUtils
{
public enum BlendMode
{
Opaque,
Cutout,
Fade,
Transparent
}
public static void SetupBlendMode(Material material, BlendMode blendMode)
{
switch (blendMode)
{
case BlendMode.Transparent:
material.SetOverrideTag("RenderType", "Transparent");
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 = (int)UnityEngine.Rendering.RenderQueue.Transparent;
material.SetFloat("_Mode", 3.0f);
break;
case BlendMode.Opaque:
material.SetOverrideTag("RenderType", "");
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;
material.SetFloat("_Mode", 0.0f);
break;
default:
Debug.LogWarning("Warning: BlendMode: " + blendMode + " is not yet implemented!");
break;
}
}
}
Then back to the editor script :
Inside OnGUI :
if (GUILayout.Button("Start"))
{
allChildren = new List<Transform>();
foreach (Transform g in transform.GetComponentsInChildren<Transform>())
{
var level = ObjectLevel(g);
names.Add(" Level " + level.ToString());
if (g.GetComponent<SkinnedMeshRenderer>() != null)
{
Material[] mats = g.GetComponent<SkinnedMeshRenderer>().sharedMaterials;
Material mat = mats[0];
MaterialUtils.SetupBlendMode(mat, isOpaque ? MaterialUtils.BlendMode.Fade : MaterialUtils.BlendMode.Fade);
mats[0] = mat;
allChildren.Add(g);
}
}
}
but i'm getting warning in the editor :
Warning: BlendMode: Fade is not yet implemented!
On the line :
MaterialUtils.SetupBlendMode(mat, isOpaque ? MaterialUtils.BlendMode.Fade : MaterialUtils.BlendMode.Opaque);
The rendering mode is still opaque.
Solution 1:[1]
If the last character is a letter or digit, you never reach that "last conditional" to see if it is the last. Using a debugger to watch the execution would make crystal clear as to why.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Scott Hunter |

