Related Posts Plugin for WordPress, Blogger...

6-29 Asset Assignment Strategies

本章要來介紹AssetBundle的分組策略,以下只有文字介紹,會稍嫌無聊一點。我會擷取部份重點進行翻譯,供大家參考,詳細官方文檔可參考如下網址:
https://docs.unity3d.com/Manual/AssetBundles-Preparing.html

https://unity3d.com/learn/tutorials/topics/best-practices/assetbundle-usage-patterns

Deciding how to divide a project's assets into AssetBundles is not simple. It is tempting to adopt a simplistic strategy, such as placing all Objects in their own AssetBundle or using only a single AssetBundle, but these solutions have significant drawbacks:

Having too few AssetBundles...
Increases runtime memory usage
Increases loading times
Requires larger downloads

Having too many AssetBundles...
Increases build times
Can complicate development
Increases total download time

The key decision is how to group Objects into AssetBundles. The primary strategies are:

Logical entities
Object Types
Concurrent content

有一種很簡單的分組策略方式,比如將資源分別放置在單獨的AssetBundle中,或將所有資源都打包於一個AssetBundle中,但這樣的分組策略具有明顯的缺點:

擁有太少的AssetBundles ...
1. 增加運行時內存使用量
2. 增加加載時間
3. 需要更大的下載量

擁有太多的AssetBundles ...
1. 增加構建時間
2. 可能會使開發複雜化
3. 增加總下載時間

好的策略有:

1. 邏輯實體分組
2. 資源類型分組
3. 同時使用內容分組

Logical Entity Grouping


Examples

  • Bundling all the textures and layout data for a User-Interface screen
  • Bundling all the models and animations for a character/set of characters
  • Bundling the textures and models for pieces of the scenery shared across multiple levels
Logical Entity Grouping is ideal for downloadable content (DLC) for the fact that, with everything separated in this way, you’re able to make a change to a single entity and not require the download of additional, unchanged, assets.

The biggest trick to being able to properly implement this strategy is that the developer assigning assets to their respective bundles must be familiar with precisely when and where each asset will be used by the project.

邏輯實體分組
1. 一個UI介面或所有UI介面一個包(包含材質與版面資料)
2. 一個角色或所有角色一個包(包含角色模型與動畫)
3. 所有場景所共享的資源一個包(包含材質與模型)

邏輯實體分組對於可下載內容(DLC)非常理想,因為通過這種方式分離所有內容,可以對單個實體進行更改,而不需要下載其他未更改的資產。

能夠正確實施這一策略的最大訣竅是,開發人員必須熟悉項目何時何地使用每種資產。

Type Grouping

For this strategy you’ll assign assets that are of similar type, such as audio tracks or language localization files, to a single AssetBundle.

Type grouping is one of the better strategies for building AssetBundles to be used by multiple platforms. For example if your audio compression settings are identical between windows and mac platforms, you can pack all audio data into AssetBundles by themselves and reuse those bundles, whereas shaders tend to get compiled with more platform specific options so a shader bundle you build for mac may not be reused on windows. In addition, this method is great for making your AssetBundles compatible with more unity player versions as textures compression formats and settings change less frequently than something like your code scripts or prefabs.

按照類型分組
所有音頻一個包,所有shader一個包,所有模型一個包,然後材質一個包。

類型分組是構建AssetBundles以供多平台使用的更好策略之一。 例如,如果您的音頻壓縮在Windows和Mac平台之間相同,則可以將所有音頻打包到AssetBundles中。而shader傾向於使用更多特定平台的選項進行編譯,因此您為Mac構建的shader包可能會不能在windows上重用。

這種方法非常適合讓您的AssetBundles與更多的遊戲版本兼容,畢竟隨著遊戲版本更新,更多修改的是代碼腳本與prefab,而如同紋理壓縮格式和設置一類的修改頻率低,可都放於同一個AssetBundles。

Concurrent Content Grouping

Concurrent Content Grouping is the idea that you will bundle assets together that will be loaded and used at the same time. You could think of these types of bundles as being used for a level based game where each level contains totally unique characters, textures, music, etc. You would want to be absolutely certain that an asset in one of these AssetBundles is only used at the same time the rest of the assets in that bundle are going to be used. Having a dependency on a single asset inside a Concurrent Content Grouping bundle would result in significant increased load times. You would be forced to download the entire bundle for that single asset.

The most common use-case for Concurrent Content Grouping bundles is for bundles that are based on scenes. In this assignment strategy, each scene bundle should contain most or all of that scenes dependencies.

Note that a project absolutely can and should mix these strategies as your needs require. Using the optimal asset assignment strategy for any given scenario greatly increases efficiency for any project.

For example, a project may decide to group its User-Interface (UI) elements for different platforms into their own Platform-UI specific bundle but group its interactive content by level/scene.

同時使用內容分組
把在某一時間內使用的所有資源打包成一個AssetBundle,可以按照關卡分類,一個關卡所需要的資源包括角色、貼圖、聲音打包成一個AssetBundle。也可以按照場景分類,一個場景所需的資源打包在一起。

請注意,項目絕對可以,且應該根據需要混合上述的三種策略。針對任何特定情況使用最佳分配策略可大大提高任何項目的效率。

例如,項目可能決定將其用於不同平台的用戶界面(UI)元素分組到他們自己的Platform-UI特定包中,但另外按照關卡/場景對其交互式內容進行詳細的分組。

Regardless of the strategy you follow, here are some additional tips that are good to keep in mind across the board:
  • Split frequently updated objects into AssetBundles separate from objects that rarely change
  • Group objects that are likely to be loaded simultaneously. Such as a model, its textures, and its animations
  • If you notice multiple objects across multiple AssetBundles are dependant on a single asset from a completely different AssetBundle, move the dependency to a separate AssetBundle. If several AssetBundles are referencing the same group of assets in other AssetBundles, it may be worth pulling those dependencies into a shared AssetBundle to reduce duplication.
  • If two sets of objects are unlikely to ever be loaded at the same time, such as Standard and High Definition assets, be sure they are in their own AssetBundles.
  • Consider splitting apart an AssetBundle if less that 50% of that bundle is ever frequently loaded at the same time
  • Consider combining AssetBundles that are small (less that 5 to 10 assets) but whose content is frequently loaded simultaneously
  • If a group of objects are simply different versions of the same object, consider AssetBundle Variants

AssetBundle 分組策略總結
1. 把經常更新的資源放在同一個包,跟不經常更新的包分離。
2. 把需要同時加載的放在同一個包。
3. 把其他包共享的資源分離出來放在同一個包。
4. 有些資源不可能同時加載,應分離出來,如標準解析度與高解析度的資源。
5. 如果有一個包只有50%的內容經常加載,應該要進行分割。
6. 把小的AssetBundle(少於5到10個資源)合併成同一個包。
7. 如果同一個資源有兩個版本,可以考慮使用後綴名來區分。


接下來依上面的分組策略舉一個簡單的例子,說明如何『把其他包共享的資源分離出來放在同一個包』。首先,針對我的兩個模型Building_a跟Building_b,依照需求要分開打包,分別取名為building_a.neko跟building_b.neko。

打包完之後如下圖,會發現兩個檔案大小分別是757KB跟758KB,佔用容量非常大,原因是兩個模型雖然使用相同的材質,但因為分開打包的關係,兩個包都有重複的材質。

選擇building的材質,然後AssetBundle取名為share。

同理,材質球的部分也同樣取名為share。

最後打包結果如下圖,building_a.neko與building_b.neko的檔案大小降為682 byte與681 byte。然後他們兩個共用的材質資源被打包進share內部。

留言