Home >Backend Development >C#.Net Tutorial >Unity 3D briefly discusses the problems encountered in Shader runtime status and rendering mode
The Shader in Unity has four rendering modes, which are:
——Opaque(不透明) ——Cutout(镂空) ——Fade(隐现) ——Transparent(透明)
Before I encountered a need to add a freezing effect to a specific game object, but to my Shader
is a completely frozen Shader, and its color value cannot be adjusted through parameters. Later, my colleague told me that I could
change the Alpha value of the object itself by modifying the rendering mode, so the first step was to find the code on the Internet about modifying the runtime Shader rendering mode, as follows.
http://www.voidcn.com/article...
//获取目标身上的渲染组件 var renderer = GetComponentsInChildren<renderer>(); // 建立一个冰冻数组 第二个数组元素是事先找好的冰冻特效 Material[] frostMatetials = new Material[2] { renderer.material, frostEffect }; //添加冰冻特效到人物身上的数组中 renderer.materials = frostMatetials; //实例化一个渲染模式变量 var fadeMode = SetMaterialRenderingMode.RenderingMode.Fade; //根据网上的方法更改你的 Shader 渲染模式为 Fade SetMaterialRenderingMode.SetMaterialRenderingModeMethod( renderer.materials[0],fadeMode);</renderer>Since the Alpha value of Color is read-only and cannot be modified, but Color can be modified, so you need to get the RGB value of the target
object Color, and then instantiate a Color and use the constructor to change it to what you want. The desired Alpha value. As follows:
Color currentcolor03 = new Color { a = 0.8f, r = renderer.materials[0].color.r, g = renderer.materials[0].color.g, b = renderer.materials[0].color.b }; //替换目标 Color 属性就可以实现了In fact, this method is not very good, mainly because the given Shader cannot modify the attribute value. Later, I got a Shader that can change the
attribute value, and things became much simpler.
parameters in the brackets. One is a parameter of String type. , indicating that the parameter you want to change is obtained through String, and the second one is the changed value.
conditions, and I used Three Colors are replaced in sequence to achieve a gradient effect, but if I select this object in the
Hierarchy panel, the changing object will suddenly return to the default state before the change, or if
you select it in advance The same effect will occur if the object to be changed is selected, that is, the game object cannot be selected in the compilation state, and its rendering status cannot be viewed in the Inspector panel. Finally, I came up with a runtime rendering Words to
describe this behavior,
If you change the rendering mode of a game object Shader at runtime, then viewing its rendering mode will cause its rendering mode
properties to be reset.
The above is the detailed content of Unity 3D briefly discusses the problems encountered in Shader runtime status and rendering mode. For more information, please follow other related articles on the PHP Chinese website!