Unity Shader ToggleDrawer 布尔开关变量 [Toggle] 设置属性,真机失效

ToggleDrawer

把一个类型的属性显示为一个开关,它的值要么是0要么是1。

当选中它时,Unity还会设置一个名为大写属性名_ON(可以自定义名字)的shader feature

#pragma shader_feature _SELECTED_ON

我们可以在shader里用过#if、#ifdef或者#if defined关键词来判断它当前是否被开启。

shader文件:

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

在这里插入图片描述

设置属性

设置接口 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).

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

我们使用的是第一种方式.

设置代码如下:

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

选中

self._fogMaterial:EnableKeyword("_SELECTED_ON")

禁用

self._fogMaterial:DisableKeyword("_SELECTED_ON")

遇到的问题

问题: 打包后,真机运行时EnableKeyword失效了。
原因: Unity在Build时自动把没有被任何用到的材质使用的variant裁切了 (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:

生成游戏包时,Unity可以检测到某些内在着色器变体没有被使用,然后stripping(裁切、忽略)它们。生成时去除可用于:

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.

上文的翻译是: 个别着色器特性,使用 #pragma shader_feature的着色器。如果一个变体没有被任何用到的材质使用,那么生成时就不把它打进去。参考内在着色器文档 ,内置着色器中的标准着色器使用这种方式。

解决方案很多:

Always Include

Shader加入到Edit->Project Settings->Graphics->Always Included Shaders这个列表里
加入AlwaysIncludedShaders的shader是开始游戏的时候就全部编译了吗?

答案是不会,加到always include 的shader,会将shader的所有变体打包到游戏,用到的时候才会加载用到的变体到内存!

相关文档: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.

跟着意思走

既然用到就不会被裁切,那么

新建一个材质,将被裁切的variant选中,这样被材质使用到的variant不会stripping ( If none of the used materials use a particular variant, then it is not included into the build)

修改shader

根据最开始的解释,这里可以这样做

#pragma shader_feature 改为 #pragma multi_compile

旧版:#pragma shader_feature _SELECTED_ON
新版:#pragma multi_compile __ _SELECTED_ON

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

猜你喜欢

转载自blog.csdn.net/weixin_43149049/article/details/127352865