Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the entry-level knowledge points of Unity AssetBundle

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about the introduction to Unity AssetBundle. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1. The definition and function of AssetBundle

AssetBundle (AB package for short) is a resource package that contains models, stickers, preforms, sounds, and even entire scenes that can be loaded while the game is running.

AssetBundle itself maintains interdependencies.

Compressed packets can be compressed using LZMA and LZ4 compression algorithms to reduce packet size and transmit faster over the network.

Putting some downloadable content in AssetBundle can reduce the size of the installation package.

two。 What is AssetBundle?

It is a file that exists on the hard disk. You can call it a compressed package. This package can be thought of as a folder that contains multiple files. These files can be divided into two categories: serialized file and resource files. (serialize files and source files)

Serialized file: resources are broken into an object, and finally unified into a separate file (only one)

Resource files: some binary resources (pictures, sounds) are saved separately, easy to load quickly, can be read on Editor, and easy to view

It is an AssetBundle object that we can load from a specific package through code. This object contains all the content we added to the package, which we can load and use.

3. Steps for using AssetBundle

Specify the AssetBundle property of the resource

(xxxa/xxx) here xxxa will generate a directory named xxx.' / 'can be used for directory partitioning, and Remove UnUsed name can remove unused attribute names

Build the AssetBundle package

Upload AB package

Load the AB package and resources in the package

4. Code packaging AssetBundle

Using the editor extension method, place the package button Build AssetBundles under the Asset menu

Using UnityEditor

Using System.IO

Public class CreateAssetBundles {[MenuItem ("Assets/Build AssetBundles")] static void BuildAllAssetBundles () {string dir = "AssetBundles"; if (Directory.Exists (dir) = = false) {Directory.CreateDirectory (dir);} / / BuildTarget selects the platform BuildPipeline.BuildAssetBundles (dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64) to be used by the AB package from build;}}

Compression mode

The path to Build (as long as it is on the hard drive)

BuildAssetBundleOptions

BuildAssetBundleOptions.None: using the LZMA algorithm to compress, the compressed package is smaller, but takes longer to load. Need to be decompressed as a whole before use. Once unzipped, the package will be recompressed using LZ4. There is no need for overall decompression when using resources. The LZMA algorithm can be used when downloading, and once it is downloaded, it will be saved locally using the LZ4 algorithm.

BuildAssetBundleOptions.UncompressedAssetBundle: no compression, large package, fast loading

BuildAssetBundleOptions.ChunkBasedCompression: with LZ4 compression, the compression ratio is not as high as LZMA, but we can load specified resources without decompressing them all.

Note: with LZ4 compression, you can get a load speed comparable to that of an uncompressed file, and less than an uncompressed file.

Dependent packaging

Put the resources that need to be loaded at the same time in the same package, and the interdependent information will be saved among the packages.

5. Loading and unloading of AssetBundle

Loading of AB

Put the AB package locally during the development phase, and then upload it to the server after development

Public class LoadFromLocal: MonoBehaviour {private void Start () {AssetBundle ab = AssetBundle.LoadFromFile ("AssetBundles/scene/wall.jy"); GameObject obj = ab.LoadAsset ("wall"); Instantiate (obj);}} private void Start () {AssetBundle ab = AssetBundle.LoadFromMemory (File.ReadAllBytes ("AssetBundles/scene/cube.jy")); GameObject obj = ab.LoadAsset ("Cube (Clone)"); Instantiate (obj) } IEnumerator Start () {AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync (File.ReadAllBytes ("AssetBundles/scene/cube.jy")); yield return request; AssetBundle ab = request.assetBundle; GameObject obj = ab.LoadAsset ("Cube (Clone)"); Instantiate (obj);} IEnumerator Start () {while (Caching.ready = = false) {yield return null;}

WWW www = WWW.LoadFromCacheOrDownload (@ "file://E:\U3D-Projects\Test2017.2.0\AssetBundles\scene\cube.jy", 1); yield return www; if (! string.IsNullOrEmpty (www.error)) {Debug.Log (www.error); yield break;} AssetBundle ab = www.assetBundle; GameObject obj = ab.LoadAsset (" Cube (Clone) "); Instantiate (obj) } IEnumerator Start () {string url = @ "file:///E:\U3D-Projects\Test2017.2.0\AssetBundles\scene\cube.jy"; UnityWebRequest request = UnityWebRequest.GetAssetBundle (url); yield return request.SendWebRequest (); / Mode 1 / / AssetBundle ab = DownloadHandlerAssetBundle.GetContent (request); / / Mode 2 AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle) .assetBundle; GameObject obj = ab.LoadAsset (" Cube (Clone) "); Instantiate (obj);}

AB loading method:

AssetBundle.LoadFromFile is loaded locally

AssetBundle.LoadFromMemory is loaded from memory

Put the WWW.LoadFromCacheOrDownload in the cache after downloading (this method is gradually deprecated)

UnityWebRequest downloaded from the server

Load resources from AB:

AssetBundle.LoadAsset (assetName)

AssetBundle.LoadAllAssets () loads all objects in the AB package, excluding dependent packages

AssetBundle.LoadAssetAsync () loads asynchronously, when loading larger resources

AssetBundle.LoadAllAssetsAsync () loads all resources asynchronously

AssetBundle.LoadAssetWithSubAssets loads resources and their child resources

Uninstall AB

Reduce memory usage

Could lead to loss.

Uninstall when switching scenes or deciding not to use it

AssetBundle.Unload (true) uninstalls all resources, including those being used

AssetBundle.Unload (false) uninstalls all unused resources

Resources.UnloadUnusedAssets uninstalls individual unused resources

6. Summary of AssetBundle grouping strategy

Logical entity grouping

One UI interface or one package for all UI interfaces (one package for mapping and layout information in this interface)

One character or all characters one package (one package of models and animations in this character)

A package shared by all scenarios (including maps and models)

Group by type

All sound resources are packed into one package, all shader into one package, all models into one package, and all materials into one package.

Grouping by usage

Pack all the resources used at a certain time into a package. According to the level points, all the resources needed for a level, including characters, maps, sounds, etc., can be packed into a package. It can also be divided according to the scene, with a package of resources needed for a scene.

Be careful

Frequently updated resources are placed in a separate package, separate from packages that are not updated frequently

Put the resources that need to be loaded at the same time in a package

Resources shared by other packages can be placed in a separate package.

Package some small resources that need to be loaded at the same time into a package

If there are two versions of the same resource, consider distinguishing them by suffixes, such as v1, v2, v3

7. Manifest file

What is a Manifest file

Crc is the check code. Check whether it is complete or not.

Assets indicates how many resources are contained in the package

Dependencies indicates what dependencies the package has

Note: before loading these packages, you also need to load dependent packages, otherwise this part of the content will be lost and the display effect will be incorrect.

Get the dependency of a package through the Manifest file

Notice that scene/cube.jy depends on material.jy, while material.jy depends on texture.jy

AssetBundle manifestAB = AssetBundle.LoadFromFile ("AssetBundles/AssetBundles"); AssetBundleManifest manifest = manifestAB.LoadAsset ("AssetBundleManifest"); / / GetAllDependencies gets all dependent objects string [] strs = manifest.GetAllDependencies ("scene/cube.jy"); / / loads all dependent objects in turn foreach (var item in strs) {Debug.Log (item); AssetBundle.LoadFromFile ("AssetBundles/" + item);}

In this way, the material of the object is also loaded and displayed:

8. Document check

CRC, MD5, and SHA1 all generate a check value by calculating the data, which is used to verify the integrity of the data.

CRC is generally used for communication data verification, MD5 and SHA1 are used in security areas, such as file verification, password encryption, etc.

9. AssetBundles browsing tool

GitHub download address

The tools panel is as follows:

Build Target is used to set the target platform for AB packages

OutPut Path sets the output path of AB

Build one-click packing

These are all the contents of this article entitled "what are the basic knowledge points of Unity AssetBundle?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report