Unity Software Inc.

04/16/2024 | Press release | Distributed by Public on 04/16/2024 07:40

Unity Asset Bundles tips and pitfalls

TypeTrees define the serialized layout of the objects contained in the Asset Bundles.

Their size depends on how many different types of objects are contained within the bundle. For this reason, it's a good idea to avoid large bundles where objects of many different types are mixed together.

TypeTrees are necessary to maintain compatibility when upgrading the Unity version of your game build while still trying to load Asset Bundles built on older versions of the engine. For example, if the format or the structure of the object have changed, they allow you to do a Safe Binary read so Unity can attempt to load it regardless. This has a performance cost, so in general it's recommended to update bundles whenever possible when you update the engine.

It can optionally be disabled, by setting the BuildAssetBundleOptions.DisableWriteTypeTree flag when building your bundles. This will make your bundles and the related memory overhead smaller, but it also means that you'll need to rebuild all your bundles whenever you update the engine version of your game build. This is especially painful if you rely on bundles built from your players for user-generated content, so unless you have a very strong reason to do so, it's recommended to keep TypeTrees enabled.

One case where TypeTrees can normally be safely disabled is for bundles included directly in your game build. In this case, upgrading the engine would require making a new game build and new Asset Bundles anyway, so its retrocompatibility aspect isn't relevant.

Each bundle has their own TypeTrees, so having multiple small bundles containing the same type of objects will slightly increase the total size on disk. On the other hand, when loaded, TypeTrees are stored in a global cache in memory, so you won't incur a higher runtime memory cost if multiple asset bundles are storing the same type of objects.