Unity Shader ToggleDrawer Boolean switch variable [Toggle] set properties, the real machine is invalid

ToggleDrawer

Displays a property of a type as a switch whose value is either 0 or 1.

When it is selected, Unity also sets a shader feature named uppercase attribute name _ON (the name can be customized)

#pragma shader_feature _SELECTED_ON

We can use the #if, #ifdef or #if defined keywords in the shader to determine whether it is currently enabled.

shader file:

Properties
{
    
    
    ...
    [Toggle]_selected("selected", Int) = 0
    ...
}
...
SubShader
{
    
    
    ...
    #pragma shader_feature _SELECTED_ON
    ...
    #ifdef _SELECTED_ON
        ...
    #else
        ...
    #endif
    ...
}

insert image description here

set properties

Set the interface EnableKeyword/DisableKeyword


At runtime, the appropriate shader variant is picked up from the Material keywords (Material.EnableKeyword and DisableKeyword) or global shader keywords (Shader.EnableKeyword and DisableKeyword).

Two ways of keyword

Toggle displays a float as a toggle. The property value will be 0 or 1, depending on the toggle state.

When it is on, a shader keyword with the uppercase property name +“_ON” will be set, or an explicitly specified shader keyword.

  1. uppercase property name +“_ON”
// Will set "_INVERT_ON" shader keyword when set
[Toggle] _Invert ("Invert?", Float) = 0
  1. an explicitly specified shader keyword
// Will set "ENABLE_FANCY" shader keyword when set.
[Toggle(ENABLE_FANCY)] _Fancy ("Fancy?", Float) = 0

We use the first method.

The setup code is as follows:

self._fogMaterial = self.fogObj:GetComponent("Renderer").material

checked

self._fogMaterial:EnableKeyword("_SELECTED_ON")

disabled

self._fogMaterial:DisableKeyword("_SELECTED_ON")

problems encountered

Problem: After packaging, the EnableKeyword is invalid when the real machine is running.
Reason: Unity automatically cuts the variants used by materials that are not used by any materials during Build ( Shader build time stripping )

While building the game, Unity can detect that some of the internal shader variants are not used by the game, and skip them from build data. Build-time stripping is done for:

When generating game bundles, Unity can detect that certain intrinsic shader variants are not being used, and strip (crop, ignore) them. Build-time stripping can be used to:

Individual shader features, for shaders that use #pragma shader_feature. If none of the used materials use a particular variant, then it is not included into the build. See internal shader variants documentation. Out of built-in shaders, the Standard shader uses this.

The translation above is: Individual shader features, shaders using #pragma shader_feature. If a variant is not used by any of the used materials, it will not be entered when generating. Referring to the built-in shader documentation , the standard shader in built-in shaders uses this method.

There are many solutions:

Always Include

Shaders are added to the list of Edit->Project Settings->Graphics->Always Included Shaders. Shaders
added to AlwaysIncludedShaders are all compiled when the game is started?

The answer is no, the shader added to the always include will package all the shader variants into the game, and only load the used variants to the memory when they are used!

Related Documentation: https://docs.unity3d.com/Manual/shader-variant-collections.html

Under all default settings, Unity loads the object into memory, but does not create the until they are actually needed.
This means that shader variants that are included into the game build can still potentially be used, but there’s no memory or load time cost paid until they are needed. For example, shaders always include a variant to handle point lights with shadows, but if you never end up using a point light with shadows in your game, then there’s no point in loading this particular variant.

follow the meaning

Since it is used, it will not be cut, then

Create a new material and select the trimmed variant so that the variant used by the material will not be stripped (If none of the used materials use a particular variant, then it is not included into the build)

Modify shader

According to the original explanation, here it can be done

#pragma shader_featurechange to#pragma multi_compile

Old: #pragma shader_feature _SELECTED_ON
New: #pragma multi_compile __ _SELECTED_ON

Individual shader features, for shaders that use #pragma shader_feature. shader_feature才会Shader build time stripping

Supongo que te gusta

Origin blog.csdn.net/weixin_43149049/article/details/127352865
Recomendado
Clasificación