Unity3D AssetBundle的打包与加载

在Unity项目开发过程中,当要做热更新时常常使用一个叫做AssetBundle的东西,这里做一点个人的学习记录

步骤1: 设置打包标签:具体步骤----进入Unity,选择某一资源然后看右下角,在那个地方做这个事情

步骤2: 打对应平台的Bundle包, 这里或多或少要用到一点编辑器扩展的内容,很简单,这里就不介绍编辑器扩展的内容了。

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

public class AssetBundleBuild {

    [MenuItem("Assets/BuildAssetBundle/Android")] // 这个打的时安卓包,
    static void BuildAssetBundle(){
        
        Debug.Log ("打包");
        string assetBundleDirectory = "AssetsBundles";
        if (!Directory.Exists (assetBundleDirectory)) {
            Directory.CreateDirectory (assetBundleDirectory);
        }
        string[] file = Directory.GetFiles (assetBundleDirectory);
        for (int i = 0; i < file.Length; i++) {
            Debug.Log (file [i]);
        }
        BuildPipeline.BuildAssetBundles (assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.Android);
        AssetDatabase.Refresh ();
    }

    [MenuItem("Assets/BuildAssetBundle/Windows")]
    static void BuildAssetBundleWin(){  // 这个方法执行后资源bundle就会出现在项目汇总AssetsBundles/window 这个目录中
        string path = Application.dataPath + "/AssetsBundles/window";
        if(Directory.Exists(path)){

        }else{
            // 创建路径
            Directory.CreateDirectory(path);
        }
        BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.None, BuildTarget.对应平台);
        AssetDatabase.Refresh();
        Debug.Log("win-finish");
    }
}

注: BuildAssetBundleOptions 这个东西叫做资源包编译选项

这里推荐一个地址需要的可以在这里进行学习:https://blog.csdn.net/AnYuanLzh/article/details/81485762

 

打出来打bundle(以及对应的Manifest文件如图:)

这里提一句:我们所有包的依赖信息都在window里,这里记住一个东西叫依赖

依赖:加入我们有一张图Pic1,在ab1这个bundle里,我们的一个prefab在ab2中,且这个prefab引用了Pic1这个图,

那么,这时就称ab2对ab1有依赖如上图对scene04依赖了sceneo4_mat这个一个assetbundle包那么在其scene04.manifest文件中就会有这么一句:

看图:

知道了这么一个概念我们介绍如何加载资源(我的scene04里面有个Cube,我们这时就要加载这个东西,且,我们Cube引用了scene04_mat的一个材质也就是上图中dependencies存在的原因)

步骤3: 加载资源包

using UnityEngine;
using System.Collections;
public class startLoadbundle : MonoBehaviour
{
    public string manifestAssetBundleName = "Window";//还记得这个window吗? 我们有个window的bundle包,

  // 每一个包的依赖信息就是从这里来的
    public string assetbundleName = "scene04";//想要加载的AssetBundle的名称

    private void Awake()
    {
        StartCoroutine(starting());
    }
    IEnumerator starting()
    {
        print(">> startLoadbundle >> Line 9" + assetbundleName); // 这里的asserbundleName是什么都没有的
        string assetbundlePath = "http://localhost/TAssetBundle/window/";/* 要加载的AssetBundle所在的地址(我的在本机的服务器上,其实这个地址不重要,能访问到就行)*/
        string manifestAssetBundlePath = assetbundlePath + "window"; /*因为资源的引用信息都在这个包里,所以我们要先加载这个bundle,这个包的中其实没有资源(这句话是我的个人理解)但是它有我们的bundle,及其依赖的所有信息,只有这个总的bundle里有这些信息,其他地方都没有,我也不知道为啥*/
        WWW wwwmanifestAssetBundle = WWW.LoadFromCacheOrDownload(manifestAssetBundlePath, 0);//下载
        yield return wwwmanifestAssetBundle;//等待下载完成
        if(wwwmanifestAssetBundle.error != null) {
            print(wwwmanifestAssetBundle.error); //  打印出错信息
        }
        AssetBundle manifestAssetBundle = wwwmanifestAssetBundle.assetBundle;//加载包含依赖关系的AssetBundle
        AssetBundleManifest manifest = (AssetBundleManifest)manifestAssetBundle.LoadAsset("AssetBundleManifest");/*从这个bundle中提取出.manifest文件,这样就可以获得所有bundle的依赖信息了,文末贴图 而且其他bundle中*/
        manifestAssetBundle.Unload(false);
        //获取依赖关系列表
        string[] dependAssetBundles = manifest.GetAllDependencies("scene04"); /*获取“scene04”这个bundle 的依赖并返回string[]数组*/
        if(dependAssetBundles.Length != 0){
            for (int i = 0; i < dependAssetBundles.Length; i++)
            {
                print(dependAssetBundles[i]);
            }
        }else{
            print("null");
        }
        AssetBundle[] abs = new AssetBundle[dependAssetBundles.Length];
        for(int i=0;i<dependAssetBundles.Length;i++)
    {
        // 根据刚才的数组加载左右的依赖bundle
            WWW www = WWW.LoadFromCacheOrDownload(assetbundlePath+dependAssetBundles[i],0);
        yield return www;
            abs[i] = www.assetBundle;
            if(www.assetBundle == null){
                print("依赖为空");
            }
    }
        /*注意:以上我们只是加载到了scene04的所有依赖,并没有加载scene04, 依赖加载结束,在这里加载目标bundle*/
        WWW www2 = WWW.LoadFromCacheOrDownload(assetbundlePath + "scene04", 0);
        yield return www2;
        AssetBundle test = www2.assetBundle;
        if(test == null){
            print("Bundle 是空的");
        }
        if(test.LoadAsset("Cube") == null){
            print("Cube" + "是空的");
        }
        object obj = test.LoadAsset("Cube"); /*加载并实例化Cube*/
        GameObject prefabs = (GameObject)obj;
        GameObject cube = GameObject.Instantiate(prefabs);
        test.Unload(false);
    }
}

 

window.manifest 中的信息,我们能看到这个scene04以及其Dependencies:scene04_mat

转载于:https://www.cnblogs.com/BXLH/p/10496265.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值