unity 5.0以后 assetbundle

unity 5.0以后修改了 bundle的机制  简单来说易用行更强了


在任何的 prefab 或者 材质 资源 下方都有一个 assetbundle的选项


建议命名规则为  物体名称.assetbundle


比如 我有一个 player的预制体


设置其 bundle 名称为 player.assetbundle


打包bundle的方法为


BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows);

第一个参数为输出路径,第二个为压缩类型,(建议选择LZ4压缩,在解包的时候 unity 会自动解压缩的,第三个就是平台了)

关于加载bundle代码如下
<pre name="code" class="csharp">  AssetBundle manifestBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/Windows/Windows");
        if (manifestBundle != null)
        {
            Debug.Log("加载本地bundle");
            AssetBundleManifest manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");
            foreach (var item in Bund)
            {
                Debug.Log("缓存本地资源信息:"+ item);
                string[] bunds = manifest.GetAllDependencies(item);
                AssetBundle[] dependsAssetbundle = new AssetBundle[bunds.Length];

                for (int index = 0; index < bunds.Length; index++)
                {
                    dependsAssetbundle[index] = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/Windows/" + bunds[index]);
                }

                AssetBundle Bundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/Windows/" + item);
                if (item.Contains("skins"))
                {
                    string _skins = item.Split('.')[0];
                    Material obj = Bundle.LoadAsset(_skins) as Material;
                    skins.Add(_skins, obj);
                }
                else
                {
                    string name = item.Split('.')[0];
                    try
                    {
                        GameObject obj = Bundle.LoadAsset(name) as GameObject;
                        allEquip.Add(name, obj);
                    }
                    catch
                    {

                        Debug.LogError("严重错误,本地资源预加载缓存出错,错误的bundle名称为name");
                    }

                }
                foreach (AssetBundle _item in dependsAssetbundle)
                {
                    _item.Unload(false);
                }
                  Bundle.Unload(false);
                _InsAssetBundle.Add(Bundle);
            }
        }
        isInit = true;
        print("bundle初始化完成");


 

具体流程为 首先加载关联信息的主bundle  然后逐个加载需要的bundle的关联bundle 然后加载使用的bundle,  加载内容为bundle的名称(这就是和为什么我们前面为什么要用命名规则)  中间判断否好友 skin 是我项目里的 请忽略

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值