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
    ...
}
ToggleDrawer.png

设置属性

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

2. 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

  1. 选中
    self._fogMaterial:EnableKeyword("_SELECTED_ON")

  2. 禁用
    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的着色器。如果一个变体没有被任何用到的材质使用,那么生成时就不把它打进去。参考内在着色器文档 ,内置着色器中的标准着色器使用这种方式。

方案很多:

  1. Shader加入到Edit->Project Settings->Graphics->Always Included Shaders这个列表里

加入AlwaysIncludedShaders的shader是开始游戏的时候就全部编译了吗?

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

相关文档:https://docs.unity3d.com/Manual/OptimizingShaderLoadTime.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.

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

  2. 修改Shader -- multi_compile

#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

我采用方案3.

参考链接

Making multiple shader program variants : https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html

MaterialPropertyDrawer : https://docs.unity3d.com/ScriptReference/MaterialPropertyDrawer.html

Optimizing Shader Load Time

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,985评论 0 23
  • 一、生活事件 今天与女儿一起出门,我开车。一路上她总是指指点点,很不放心让我开车,我们两个人都不开心。我很...
    Cyylinxi阅读 210评论 0 0
  • 昨晚晚自习还剩五分钟的时候班长提示孩子们写反思,我在稿纸上写下了“我是领航人,我怎样,领航就怎样”,写后内心有种难...
    张珍珍zzz阅读 574评论 3 11
  • 有一句经典的广告词 “只有更好,没有最好!” 是啊,遇见更好的自己,我们一直在路上。 每个人都有一个自我成就的梦,...
    齐齐乐阅读 1,701评论 2 11
  • 前言 前几个月一直忙碌公司iOS端App开发,所以没时间管理自己博客,最近看到一篇文章,让我鼓足勇气开通自己的Bl...
    scyworld阅读 840评论 0 5