AssetBundle

前言

AssetBundle(简称AB包)是unity提供的一种资源压缩包,可以通过下载更新的方式,实现应用的更新,可以包含模型、贴图、预制体、声音、甚至整个场景,可以在游戏运行的时候被加载。本来来自UnityManual,参考文献:https://blog.csdn.net/qq_35361471/article/details/82854560

1.基本操作

1.1 生成AssetBundle

    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        string dir = "AB";
        if (Directory.Exists(dir) == false)
        {
            Directory.CreateDirectory(dir);
        }
        BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
    }

1.2 同步本地加载

        var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));//可以实全路径也可以是相对路径如 "AB/cc.dd"
        if (myLoadedAssetBundle == null) {
            Debug.Log("Failed to load AssetBundle!");
            return;
        }
        var prefab = myLoadedAssetBundle.LoadAsset.<GameObject>("MyObject");
        Instantiate(prefab);

1.3 异步本地加载

    IEnumerator LoadFromMemoryAsync(string path)
    {
        AssetBundleCreateRequest createRequest = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));//
        yield return createRequest;
        AssetBundle bundle = createRequest.assetBundle;
        var prefab = bundle.LoadAsset<GameObject>("MyObject");
        Instantiate(prefab);
    }

问题:AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path))中LoadFromMemoryAsync只要传入byte[]类型参数即可,而且在android中如果assetbundle放在streamingassets下file类是无法读取的,所以可以通过其他方法异步加载,如www类。加载到byte[]然后传给LoadFromMemoryAsync方法调用。异步方法还有更直接的方法如下。

1.4 WWW异步加载

此加载assetbundle的方法将被弃用

            while (!Caching.ready)
                    yield return null;

        var www = WWW.LoadFromCacheOrDownload("http://myserver.com/myassetBundle", 5);
        yield return www;
        if(!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield return;
        }
        var myLoadedAssetBundle = www.assetBundle;

        var asset = myLoadedAssetBundle.mainAsset;

如果存在本地,则LoadFromCacheOrDownload(“http://myserver.com/myassetBundle”, 5)中的http链接改为本地路径,但是路径前要加http://,或者采用类new System.Uri(本地路径).absoluteUri;获取相关相关uri路径。

1.5 UnityWebRequest方法

        string uri = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;       
 UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(uri, 0);
        yield return request.Send();
        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
        GameObject cube = bundle.LoadAsset<GameObject>("Cube");
        GameObject sprite = bundle.LoadAsset<GameObject>("Sprite");
        Instantiate(cube);
        Instantiate(sprite);

1.6 加载Assetbundle内容

官方api中提供了众多方法,最基本的方法为abCube.LoadAsset方法

        AssetBundle abMat = AssetBundle.LoadFromFile(mat);
        AssetBundle abCube = AssetBundle.LoadFromFile(cubep);
        AssetBundle abSphere = AssetBundle.LoadFromFile(spherep);

        GameObject cube = abCube.LoadAsset<GameObject>("Cube");
        Instantiate(cube);
        GameObject sphere = abSphere.LoadAsset<GameObject>("Sphere");
        Instantiate(sphere);

1.7 通过打包的总文件获取依赖

AssetBundle assetBundle = AssetBundle.LoadFromFile(manifestFilePath);//打包的总文件位置
AssetBundleManifest manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
string[] dependencies = manifest.GetAllDependencies("cc.dd"); //获取cc.dd的所有依赖文件.
foreach(string dependency in dependencies)
{
    AssetBundle.LoadFromFile(Path.Combine(assetBundlePath, dependency));
}

2.总结

2.1 AB加载方式

AssetBundle.LoadFromFile 从本地加载
AssetBundle.LoadFromMemory 从内存加载
WWW.LoadFromCacheOrDownload 下载后放在缓存中备用(该方法逐渐被弃用)
UnityWebRequest 从服务器下载

2.2 从AB中加载资源

AssetBundle.LoadAsset(assetName)
AssetBundle.LoadAllAssets() 加载AB包中所有的对象,不包含依赖的包
AssetBundle.LoadAssetAsync() 异步加载,加载较大资源的时候
AssetBundle.LoadAllAssetsAsync() 异步加载全部资源
AssetBundle.LoadAssetWithSubAssets 加载资源及其子资源

2.3 AB的卸载

在切换场景,或者确定不使用的时候卸载
AssetBundle.Unload(true) 卸载所有资源,包含其中正被使用的资源
AssetBundle.Unload(false) 卸载所有没被使用的资源
Resources.UnloadUnusedAssets 卸载个别未使用的资源

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值