找回密码
 立即注册
查看: 330|回复: 0

Unity AssetBundle 的使用

[复制链接]
发表于 2022-4-9 13:25 | 显示全部楼层 |阅读模式
比如要打包一个图片,选择这个图片,在Inspector视图的最下面AssetBundle 后选择New 输入一个名字,
编辑一个脚本放到Editor文件夹中
using System.IO;using UnityEditor;public class CreateAssetBundles{    [MenuItem("Assets/Build AssetBundles")]    static void BuildAllAssetBundles()    {        BuildPipeline.SetAssetBundleEncryptKey("0123456789abcdef");//16个加密字符        string assetBundleDirectory = UnityEngine.Application.streamingAssetsPath;        if (!Directory.Exists(assetBundleDirectory))        {            Directory.CreateDirectory(assetBundleDirectory);        }        BuildPipeline.BuildAssetBundles(assetBundleDirectory,                                        BuildAssetBundleOptions.ChunkBasedCompression|                                        BuildAssetBundleOptions.EnableProtection,                                        BuildTarget.StandaloneWindows);            }}
然后在Assets栏下点Build AssetBundles 按钮就会将图片打包到StreamingAssets文件夹下
使用方法:
//加载资源        UnityEngine.AssetBundle.SetAssetBundleDecryptKey("0123456789abcdef");//16个字符        var myLoadedAssetBundle            = AssetBundle.LoadFromFile(System.IO.Path.Combine(Application.streamingAssetsPath, "ab"));        if (myLoadedAssetBundle == null)        {            Debug.Log("Failed to load AssetBundle!");            return;        }        var jpg = myLoadedAssetBundle.LoadAsset<Texture>("abc");        this.GetComponent<RawImage>().texture = jpg;
常用的三种压缩方式及区别:
1.BuildAssetBundleOptions.None: 使用LZMA算法压缩,压缩的包更小,但是加载时间更长。使用之前需要整体解压。一旦被解压,这个包会使用LZ4算法重新压缩。使用资源的时候不需要整体解压。在下载的时候可以使用LZMA算法,一旦它被下载了之后,它会使用LZ4算法保存到本地上。
2.BuildAssetBundleOptions.UncompressedAssetBundle: 不压缩,包大,加载快。
3.BuildAssetBundleOptions.ChunkBasedCompression: 使用LZ4算法压缩,压缩率没有LZMA高,但是我们可以加载指定资源而不需要解压全部。
使用LZ4算法压缩,可以获得可以跟不压缩相媲美的加载速度,而且比不压缩文件要小。目前AB包加密需要LZ4压缩格式,也就是上面提到的ChunkBasedCompression方式。
加密方法(官方有相应的加密文档):
1.构建AB包时,先指定本次build所使用的加密秘钥,密钥长度为16为字符(128bit):
BuildPipeline.SetAssetBundleEncryptKey("0123456789abcdef");
然后指定BuildAssetBundleOptions 包含:ChunkBasedCompression和EnableProtection:
BuildPipeline.BuildAssetBundles(assetbundleBuildPath, BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.EnableProtection, EditorUserBuildSettings.activeBuildTarget);
通过指定密钥为null来清除秘钥:BuildPipeline.SetAssetBundleEncryptKey(null);

另外一种简单的加密方式:
使用offest加密
原理就是对AssetBundle的二进制文件添加一些字符,来达到加密的目的
public static void ABOffsetEncryption(string fileFile){byte[] oldData = File.ReadAllBytes(fileFile);int newOldLen = 8 + oldData.Length;//这个空字节数可以自己指定,示例指定8个字节 var newData = new byte[newOldLen];for (int tb = 0; tb < oldData.Length; tb++){newData[8+ tb] = oldData[tb];}FileStream fs = File.OpenWrite(fileFile);//打开写入进去fs.Write(newData, 0, newOldLen);fs.Close();}
解密:unity官方提供的AB包加载API,有提供根据Offset进行加载AB
AssetBundle abBundle = AssetBundle.LoadFromFile(assetBundleMapPath,0,8);
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-22 15:36 , Processed in 0.064783 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表