Unity AssetBundle

打包过程

  1. 创建文件夹Editor
  2. 在Editor中创建脚本,写入打包代码
  3. using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    public class BuildBundles : Editor
    {
        [MenuItem("Custom/BuildBundles")]
        static void BuildBundle()
        {
            BuildPipeline.BuildAssetBundles(Application.dataPath + "/AssetBundles",BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
        }
    }
    
  4. 在Project视图中选择待打包物体
  5. 选择Inspector下预览窗口AssetBundle处后面下拉按钮
  6. 在第一个下拉为AssetBundle包命名(如果将多个物体放到同一个包,选择相同的包名即可),在第二个下拉为AssetBundle定义后缀(可不写)
  7. 选择菜单栏Custom ---> BuildBundles
  8. 完成打包

打包后,如果对资源进行了更改,重新打包,即可覆盖原包。

 加载过程

创建脚本并挂载到游戏物体上。

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

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

    void Load()
    {
        string path = Application.dataPath + "/AssetBundles/Test.ab";
        AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
        GameObject cube = assetBundle.LoadAsset<GameObject>("Cube");
        Instantiate(cube);
    }
}

依赖关系

资源之间往往存在互相依赖关系,如:某个物体上使用了某种材质。 

如果存在依赖关系的资源不在同一个包里,加载资源时需要将此物体所在包与其依赖的资源所在包都加载出来。

依赖关系可以在打包后的文件说明中查看。

如下:Cube预制体依赖材质Blue。Cube打包到test包中,Blue打包到mat包中。

加载代码:

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

public class TestLoad : MonoBehaviour
{
    void Start ()
    {
        LoadMat("mat.ab");
        LoadPrefab("test.ab");
    }

    void LoadMat(string p)
    {
        //获取test包依赖的其它包
        string path = Application.dataPath + "/AssetBundles/AssetBundles";
        AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
        AssetBundleManifest assetBundleManifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        string[] strs = assetBundleManifest.GetAllDependencies("test.ab");
        //遍历加载具有依赖关系的包
        foreach (string item in strs)
        {
            AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/" + item);
        }
    }
    void LoadPrefab(string p)
    {
        string path = Application.dataPath + "/AssetBundles/test.ab";
        AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
        GameObject cube = assetBundle.LoadAsset<GameObject>("Cube");
        Instantiate(cube);
    }
}

异步加载

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

public class TestLoad : MonoBehaviour
{
    void Start ()
    {
        StartCoroutine(AsyncLoadMat());
        StartCoroutine(AsyncLoadPrefab());
    }
    IEnumerator AsyncLoadMat()
    {
        string path = Application.dataPath + "/AssetBundles/AssetBundles";
        AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
        AssetBundleManifest assetBundleManifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        string[] strs = assetBundleManifest.GetAllDependencies("test.ab");
        foreach (string item in strs)
        {
            AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(Application.dataPath + "/AssetBundles/" + item);
            yield return request;
        }
    }
    IEnumerator AsyncLoadPrefab()
    {
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(Application.dataPath + "/AssetBundles/test.ab");
        yield return request;
        AssetBundle assetBundle = request.assetBundle;
        GameObject cube = assetBundle.LoadAsset<GameObject>("Cube");
        Instantiate(cube);
    }
}

网络获取

方式一: 

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

public class TestLoad : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(WWWLoad());
    }

    IEnumerator WWWLoad()
    {
        string url = "file:///D:/Test/test.ab";//此处可以改为网络接口

        UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
        yield return request.SendWebRequest();
        AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(request);
        GameObject cube = assetBundle.LoadAsset<GameObject>("Cube");
        Instantiate(cube);
    }
}

方式二:

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

public class TestLoad : MonoBehaviour
{
    void Start ()
    {
        StartCoroutine(WWWLoad());
    }

    IEnumerator WWWLoad()
    {
        WWW www;
        string url = "file:///D:/Test/test.ab";//此处可以改为网络接口
        www = new WWW(url);
        yield return www;
        if (www.error == null)
        {
            if (www.isDone)
            {
                AssetBundle assetBundle = www.assetBundle;
                GameObject cube = assetBundle.LoadAsset<GameObject>("Cube");
                Instantiate(cube);
            }
        }
        else
        {
            Debug.Log(www.error);
        }
    }
}

注意

如果一个AssetBundle包已经被加载过,是不能进行再次加载的。

但已经加载的AssetBundle包可以被卸载。

卸载使用UnLoad()方法。

此方法参数为布尔值,表示是否拆卸所有包中资源。

如果为真,则所有资源卸载。

如果为假,则场景中用到的资源不卸载。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值