assetbundle 坑爹總結

2021-07-10 12:28:29 字數 3043 閱讀 1319

1、當乙個被打包的資源a引用了其他的資源b,並且沒有被打成乙個包時,要選用【buildassetbundleoptions.collectdependencies 收集依賴關係】或者【buildassetbundleoptions.completeassets 完整資源】(兩者並不衝突可以全選)來進行打包,否則打包的時候unity也會發出警告:a上的元件丟失。

當選擇了buildassetbundleoptions.collectdependencies時,果然你沒有將a依賴的資源b使用buildpipeline.pushassetdependencies() 和buildpipeline.popassetdependencies()來進行依賴打包,那麼a會把其引用的資源都打包到自己的assetbundle 中。如果引用的資源是、sprite或自定義的shader(內建的shader不會打包,這裡的自定義shader被unity看作是一種資源,打包處理的時候也是如同資源來處理的),那麼會打包到assetbundle 中。如果引用的是**,那麼會打包乙個對工程中**的引用,也就是說引用的**必須存在於工程中,這樣當c被載入到本地的時候才可以和本地的**進行關聯,如果本地沒有這個**,則會丟失對這個指令碼的引用。如圖所示可以看到打包的資源(和ngui中自帶的shader)和指令碼**的引用:

當只選擇buildassetbundleoptions.completeassets進行打包的時候,則不會將a引用的資源打包進來,通過除錯也可以檢視到包比使用collectdependencies的小很多,斷點後也發現不包括依賴的資源。我的理解是等於我們告訴了unity我已經把所有資源放進資源包了,所以它也不會報錯。據官方文件說在打包模型和動畫的時候這個選擇會起作用,還需要進一步的實踐驗證(驗證結果是使用completeassets時,會把模型的所有動畫剪輯打包進去,包括在animationcontroller中沒有使用到的)。如果只是用completeassets而不使用collectdependencies打包,那麼由於資源包中不帶引用的資訊,即使是引用的指令碼(或資源)在本地工程存在,也會在assetbundle 被載入下來後丟失掉對指令碼(或資源)的引用。

1、如果單獨打包乙個資源c,c引用了乙個ngui自定義shader unlit/transparent colored,選擇了buildassetbundleoptions.collectdependencies,那麼這個shader會打包到這個資源包中

2、如果不選擇buildassetbundleoptions.collectdependencies,那麼shader則不會打包。那麼這個資源包在載入到本地的時候是否會自動關聯到本地的shader呢?這個要分為兩種情況討論。

第一種,就是如圖 5.1,將shader unlit/transparent colored加入到graphicssettings中,這樣設定之後再打包,即使沒有選擇buildassetbundleoptions.collectdependencies,當資源包載入到本地的時候也可以自動關聯這個shader。(需要保證shader會被打包到安裝包,要放在 resource資料夾下)

圖 5.1

第二種,就是如圖 5.2,沒有在graphicssettings中設定這個shader,然後打包,那麼資源包載入到本地的時候是不能自動關聯這個shader的,結果如圖 5.3。

圖 5.2

圖 5.3

3、如果選擇buildassetbundleoptions.collectdependencies,包shader打包成基礎包,c去依賴這個shader基礎包又分為兩種情況

3.1 c用到的shader都在shader基礎包中,那麼一切正常。

3.2 c用到的shader一部分在shader包中,那麼這一部分正常,不在基礎中的shader c並不會打包到自己身上,而回在在被載入回來後在本地尋找graphicssettings列表,找不到測丟失shader。(但是內建shader是可以自動關聯)

在做打包工具的時候我們要對多個預設已經依賴打包。

比如打包bcdef,他們都依賴a

你以為可以這樣寫:

push

build a

push

for bcdef列表 

build 

poppop

然後你發現指令碼各種丟失。。。

實際上應該這樣:

push

build a

push

for bcdef列表 

push

build 

poppop

pop這層push pop一定要寫,表示bcdef是不互相依賴的並列關係~

文件如下:

public static 

assetbundle 

createfromfile(string path);

path

path of the file on disk

see also: www.assetbundle, www.loadfromcacheordownload.

loads an asset bundle from a disk.

only uncompressed(只支援不壓縮的,打包的使用需要選擇uncompressed,預設是壓縮的)

asset bundles are supported by this function. this is the fastest way to load an asset bundle

還有乙個非常肯定的地方,用這兒方法載入的assetbundle不能有重名的,即使路徑不一樣,不然會報錯,提示已經載入過相同的包!

載入assetbundle坑記錄

1 關於霧效 載入ab場景後,場景霧效要用指令碼動態設定。並且在graphicssettings中設定shader stripping如下圖 2 場景用到地形terrain shader會有問題。解決辦法在graphicssettings中設定新增shader如下圖 3 assetbundle.lo...

python坑爹的黑店 1023 坑爹的黑店

題目描述 今天小明去了乙個風景如畫的地方散心,但是自己帶的飲料喝完了,小明口渴難耐,見不遠處有家小商店,於是跑去買飲料。小明 我要買飲料!店主 我們這裡有三種飲料,礦泉水1.5元一瓶,可樂2元一瓶,橙汁3.5元一瓶。小明 好的,給我一瓶礦泉水。說完他掏出一張n元的大鈔遞給店主。小明 小明環顧四周,就...

坑爹的if語句

前兩天的時候,自己在修改 的時候,自己感覺對除錯比較有心得體會了。但遇到乙個問題,總在乙個函式中返回了false,明明條件就是不應該返回。修改前的示例 如下 string topic topic is not empty if topic.empty return false 我想對這行 加乙個日誌...