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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,923評論 6 535
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,740評論 3 420
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,856評論 0 380
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,175評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,931評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,321評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,383評論 3 443
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,533評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,082評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,891評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,067評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,618評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,319評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,732評論 0 27
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,987評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,794評論 3 394
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,076評論 2 375

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,721評論 0 23
  • 一、生活事件 今天與女兒一起出門,我開車。一路上她總是指指點點,很不放心讓我開車,我們兩個人都不開心。我很...
    Cyylinxi閱讀 202評論 0 0
  • 昨晚晚自習還剩五分鐘的時候班長提示孩子們寫反思,我在稿紙上寫下了“我是領航人,我怎樣,領航就怎樣”,寫后內心有種難...
    張珍珍zzz閱讀 568評論 3 11
  • 有一句經典的廣告詞 “只有更好,沒有最好!” 是啊,遇見更好的自己,我們一直在路上。 每個人都有一個自我成就的夢,...
    齊齊樂閱讀 1,691評論 2 11
  • 前言 前幾個月一直忙碌公司iOS端App開發,所以沒時間管理自己博客,最近看到一篇文章,讓我鼓足勇氣開通自己的Bl...
    scyworld閱讀 818評論 0 5