[Unity 3D] AssetBundle packaging, uploading, loading, and unloading detailed explanation and demonstration (source code attached)

If you need the source code and dependencies, please like, follow, and leave a private message in the comment area after collecting it~~~

The most important operation of AssetBundle is AssetBundle packaging, downloading, loading and unloading. Let’s learn about the common operations of AssetBundle.

1: AssetBundle packaging

AssetBundle packaging mainly uses the following two APIs

BuildAssetBundles(string outputPath,AssetBundleBuild[] builds,

BuildAssetBundleOptions assetBundleOptions,BuildTarget targetPlatform);

BuildAssetBundles(string outputPath,BuildAssetBundleOptions assetBundleOptions,BuildTatget targetPlarform);

 Next, create a new PackBundles.cs script and put it in the Editor folder. The code is as follows:

using System.Collections.Generic;
using System.IO;
using UnityEditor;

public class PackBundles : Editor
{
    //选定资源打包
    [MenuItem("PackBundles/PackBundles")]
    static void PutBundleAssetes()
    {
        //初始化一个AssetBundleBuild表
        List<AssetBundleBuild> buildMap = new List<AssetBundleBuild>();
        AssetBundleBuild build = new AssetBundleBuild();
        //设置AssetBundleBuild的名字和资源路径
        build.assetBundleName = "tempImg.unity3d";
        build.assetNames = new[] { "Assets/Textures/tempImg.jpg" };
        //添加进表
        buildMap.Add(build);

        //将这些资源包放在一个名为ABs的目录下        
        string assetBundleDirectory = "Assets/ABs";
        //如果目录不存在,就创建一个目录        
        if (!Directory.Exists(assetBundleDirectory))
        {
            Directory.CreateDirectory(assetBundleDirectory);
        }
        //资源打包
        BuildPipeline.BuildAssetBundles(assetBundleDirectory, buildMap.ToArray(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }
}

All resources packaged

Before packaging all resources, you need to set the properties of each resource that needs to be packaged. Then we modify the PackBundles.cs script to add the selected resource packaging box and all resource packaging functions.

using System.Collections.Generic;
using System.IO;
using UnityEditor;

public class PackBundles : Editor
{
    //选定资源打包
    [MenuItem("PackBundles/PackBundles")]
    static void PutBundleAssetes()
    {
        //初始化一个AssetBundleBuild表
        List<AssetBundleBuild> buildMap = new List<AssetBundleBuild>();
        AssetBundleBuild build = new AssetBundleBuild();
        //设置AssetBundleBuild的名字和资源路径
        build.assetBundleName = "tempImg.unity3d";
        build.assetNames = new[] { "Assets/Textures/tempImg.jpg" };
        //添加进表
        buildMap.Add(build);

        //将这些资源包放在一个名为ABs的目录下        
        string assetBundleDirectory = "Assets/ABs";
        //如果目录不存在,就创建一个目录        
        if (!Directory.Exists(assetBundleDirectory))
        {
            Directory.CreateDirectory(assetBundleDirectory);
        }
        //资源打包
        BuildPipeline.BuildAssetBundles(assetBundleDirectory, buildMap.ToArray(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }
    //全部打包
    [MenuItem("PackBundles/AllPackBundles")]
    static void PutBundleAssetesAll()
    {
        //将这些资源包放在一个名为ABs的目录下        
        string assetBundleDirectory = "Assets/ABs";
        //如果目录不存在,就创建一个目录        
        if (!Directory.Exists(assetBundleDirectory))
        {
            Directory.CreateDirectory(assetBundleDirectory);
        }
        BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}

Select the PackBundles->AllPachBundles command in the menu bar. You can see the AssetBundle file generated in the ABs folder in the Project view. You can see that the packaged files have a characteristic, that is, each AssetBundle resource package will have a suffix named .manifest. document

2: Manifest file

The Manifest file is actually the dependency relationship of the AssetBundle resource file and the record file of the package information. Based on this file, you can obtain the dependency relationship of a certain package, or determine the size of the obtained package, and whether the verification code is normal. Use text for this file. After opening the editor, you can see the file content

CRC: Verification code used to check whether the package is complete

Assets: Indicates how many resources are included in the package

Dependencies: Indicates the dependencies of the package

3: AssetBundle file upload 

Next, use IIS to build a local server

1: Open the following interface in Windows

2: Open IIS and create a new website. Set the website name, port number and physical path. Set the physical path to the path where the AssetBundle resource file is stored.

 3: Set IIS properties

4: Place the generated AssetBundle file into the folder of the set physical path and delete the file with the suffix .meta

 4: AssetBundle loading

There are four ways to load AssetBundle:

AssetBundle.LoadFromFile:Load from local

AssetBundle.LoadFromMemory loads from memory

WWW.LoadFromCacheOrDownload is loaded and placed in the cache for later use.

UnityWebRequest loads from server

Create a new script below to load the AssetBundle file

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class LoadBundles : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(Load());
    }

    IEnumerator Load()
    {
        //从远程服务器上进行下载和加载
        string url = "http://localhost:8090/cube.unity3d";
        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
        //等待文件下载完毕
        yield return request.SendWebRequest();
        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
        //加载AssetBundle资源
        AssetBundleRequest ABrequest = bundle.LoadAssetAsync("Cube.prefab", typeof(GameObject));
        //根据资源生成文件
        Instantiate(ABrequest.asset as GameObject, new Vector3(0f, 0f, 0f), Quaternion.identity);
        yield return ABrequest;
        //释放资源
        request.Dispose();
    }
}

 5: AssetBundle uninstall

There are also four ways to uninstall AssetBundle. The code of the new test script is as follows:

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class LoadBundles : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(Load());
    }

    IEnumerator Load()
    {
        //从远程服务器上进行下载和加载
        string url = "http://localhost:8090/cube.unity3d";
        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
        //等待文件下载完毕
        yield return request.SendWebRequest();
        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
        //加载AssetBundle资源
        AssetBundleRequest ABrequest = bundle.LoadAssetAsync("Cube.prefab", typeof(GameObject));
        //根据资源生成文件
        GameObject go = Instantiate(ABrequest.asset as GameObject, new Vector3(0f, 0f, 0f), Quaternion.identity);
        yield return ABrequest;
        //释放资源
        request.Dispose();

        //卸载资源 包含所有Load创建出来的对象
        bundle.Unload(true);
        //卸载资源 除了Load创建出来的对象
        bundle.Unload(false);
        //释放已加载的资源Object
        Resources.UnloadAsset(go);
        //卸载所有没有被场景引用的资源对象
        Resources.UnloadUnusedAssets();
    }
}

6: AssetBundle packaging tool

We can use the Unity AssetBundle Browser Tool plug-in to view AssetBundle to help package AssetBundle and view AssetBundle content

It’s not easy to create. If you find it helpful, please like, follow and collect~~~

Guess you like

Origin blog.csdn.net/jiebaoshayebuhui/article/details/128591347