Unity AssetBundle resource packaging and resource loading

AssetBundle is a resource packaging method that comes with Unity, and the AssetBundle file can be regarded as an encrypted compressed package.

Generally, an expander tool is written to package AB resources.

AssetBundle resource packaging precondition settings:

Except for C# scripts, this AB package setting interface will appear for anything we select.

 Click New...to create a name we want (it will automatically convert it to lowercase for you), remember to press Enter to save it after entering the name;

Next to it is the suffix. Commonly used suffixes in enterprise development are unity3d, assetbundle, ab, and other customizations.

I define it as pedestal.ab here, ULua's packaging format is hard-coded assetbundle, please pay attention.

After completing these pre-settings, no matter which folder the file is in, it will recognize it and pack it away. Look at the code.

If you want to put it in a folder and write a path, the folder will be automatically created and saved in it. 

The editor packages AB resources:

1. Create an "Editor" directory under the project root folder to store the editor extension script;

2. Introduce the namespace "UnityEditor", and the script does not need to inherit MonoBehaviour;

3. Write a static non-return value method for packaging AB resources;

4. Add a "feature" above the method: [MenuItem("menu/itemName")], this feature can display the method we wrote on the Unity menu, menu is the name of the menu, and itemName is the name of the method ( You can write whatever you want).

 

 Code demo:

BuildPipeline.BuildAssetBundles(path, options, platform);

Parameter analysis:

①BuildAssetBundles: Package all resources with AssetLabels set;

②Path: the location where the packaged AssetBundle file is stored;

③Options: Set the options in the AssetBundle packaging process, None means ignore this option;

④Platform: AssetBundle is incompatible between platforms, IOS and Android are two sets of resources;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class BuildAssetBundles {

    //路径,一般打包在这个StreamAssets文件夹中。
    string path = Application.streamingAssetsPath;

    [MenuItem("AssetBundle/打包所有的AssetBundle")]
    static void BuildAllAssetBundleNoral()    //体积最小,常用这个.
    {
        //                              路径,                 选项.体积最小,        平台.Windows64x.
        BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
        Debug.Log("AssetBundle打包完毕");
    }

    [MenuItem("AssetBundle/打包所有的AssetBundle[不压缩]")]
    static void BuildAllAssetBundleCompre()    //体积最大,加载速度最快.
    {
        //                              路径,                    选项.加载速度最快,                         平台.Windows64x.
        BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows64);
        Debug.Log("AssetBundle打包完毕");
    }

    [MenuItem("AssetBundle/打包所有的AssetBundle[LZ4]")]
    static void BuildAllAssetBundleChunk()    //2者之间.
    {
        //                              路径,                     选项.2者之间,                       平台.Windows64x.
        BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
        Debug.Log("AssetBundle打包完毕");
    }
}

 

Resource loading:

1. Load AB resources into memory

AssetBundle ab = AssetBundle.LoadFromFile("AB package full path")

Load the AB resource bundle from a complete path location to memory, and return an AssetBundle object;

2. Obtain resources from AB resources

T resName = ab.LoadAsset<T>("game resource name")

Obtain the corresponding game object resource from the AssetBundle object through the "load resource" method of the obtained AssetBundle object, and return the resource. The effect is similar to loading a resource using Resources.Load;

3. Instantiate resources

Just like the resources loaded into memory by Resources.Load, instantiate the object directly.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AssetBundleLoad : MonoBehaviour
{
    void Start()
    {
        //从本地加载 AB 资源到内存.
        AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "player/player1.ab");

        //从 AB 资源中获取资源.
        GameObject player = assetBundle.LoadAsset<GameObject>("Necromancer");

        GameObject.Instantiate<GameObject>(player, Vector3.zero, Quaternion.identity);
    }
}

Summary: In fact, the difference with the Resources method is that there is an extra step to load it into the memory first, and then take out the resources we want to use from the AB package.

extension:

When you pack it, there will be 2 more files

 

It is actually the [directory] of the AssetBundle file, which stores the path addresses (including names) of all ab resource bundles. The first one is for the computer to see, and the second .manifest file is for us to see.

Guess you like

Origin blog.csdn.net/weixin_55532142/article/details/125402131