AssetBundle学习(二)

AssetBundle的标记和打包

在Unity中AssetBundle是必不可少的资源管理方案指以,所以啦,Unity也提供了很方便的AB包打包方式,就简单的学习一下啦。

标记AB资源

1.手动标记资源
打开Unity,看到Project视图下,任意选择一个资源或者文件夹,然后再Inspector视图下面有一个AssetLabels的标签。
在这里插入图片描述
点击下拉框有几个选项

  1. new :创建一个AB包的名称
  2. Remove Unused Names :移除掉未使用的AB Name

第二个下拉框也是一样的功能,不过这个就是你打包出来的后缀名啦,不影响,一般设置成.assetbundle或者.ab 用于识别。

2.代码标记资源
我用的是标记所有的Asset/_Res文件下面的文件夹,因为资源的管理大多是根据文件夹来管理的,这样方便可控.

 [MenuItem("AssetBundle/Mark AB")]
    static void MarkAllAssetBundles()
    {
        //获取所有的AssetBundle名称  
        string[] abNames = AssetDatabase.GetAllAssetBundleNames();

        //强制删除所有AssetBundle名称  
        for (int i = 0; i < abNames.Length; i++)
        {
            AssetDatabase.RemoveAssetBundleName(abNames[i], true);
        }

        string resDir = "Assets/_Res";

        if (Directory.Exists(resDir))
        {
            GetDirInfo(resDir);
        }
    }

    static void GetDirInfo(string path)
    {
        DirectoryInfo dir = new DirectoryInfo(path);
        DirectoryInfo[] dirChilds = dir.GetDirectories();

        if (dirChilds.Length == 0) return;

        for (int i = 0; i < dirChilds.Length; i++)
        {
            MarkAssetBundle(dirChilds[i].FullName, dirChilds[i].Name);

            GetDirInfo(dirChilds[i].FullName);
        }
    }

    static void MarkAssetBundle(string abPath,string abName)
    {
        AssetImporter assetImporter = AssetImporter.GetAtPath(GetBundlePath(abPath));

        assetImporter.assetBundleName = abName;

        assetImporter.assetBundleVariant = "ab";
    }

    public static string GetBundlePath(string AbPathName)
    {
        int index = AbPathName.IndexOf("Assets");
        //截取Assets之后的路径
        //AssetImporter.GetAtPath必须是unity工程的相对路径
        //所以要Assets开头
        string Path = AbPathName.Substring(index);
        return Path;
    }

编辑器下执行命令,标记完成。如果需要对文件打标签,上面改一下就好勒,把遍历文件夹的地方加上一个遍历文件的循环 OK搞定。

3.备注
unity 文件打AB包,目前只支持文件夹,图片,音效,预制体等资源,是不支持代码打AB包的,所以我们也无法为代码打上AB标签。

AssetBundle打包

  [MenuItem("AssetBundle/Bulid AB")]
    static void BuildAllAssetBundles()
    {
        string dir = "../AssetBundles";

        ClearDirectory(dir);

        //BuildTarget 选择build出来的平台  
        BuildPipeline.BuildAssetBundles(dir,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
    }

    static void ClearDirectory(string dir)
    {
        if (Directory.Exists(dir))
            Directory.Delete(dir, true);
        Directory.CreateDirectory(dir);
    }

BuildPipeline.BuildAssetBundles 方法的参数意思

outputPath:
输出AB的路径,如果需要上传到云端,自己去写一个上传的代码啦,把资源文件夹直接传输上去就好了。
BuildAssetBundleOptions:
None:使用LZMA算法压缩,压缩的包更小,但是加载时间更长。使用之前需要整体解压。一旦被解压,这个包会使用LZ4重新压缩。使用资源的时候不需要整体解压。在下载的时候可以使用LZMA算法,一旦它被下载了之后,它会使用LZ4算法保存到本地上。
UncompressedAssetBundle:不压缩,包大,加载快
ChunkBasedCompression:使用LZ4压缩,压缩率没有LZMA高,但是我们可以加载指定资源而不用解压全部。
注意:使用LZ4压缩,可以获得可以跟不压缩想媲美的加载速度,而且比不压缩文件要小。
依赖打包
将需要同时加载的资源放在同一个包里,各个包之间会保存相互依赖的信息
BuildTarget :
就是悬着输出AB包的平台,名字很明显,直接看就好了

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值