打包说明
如果apk包和资源包都很小,可以看我上一篇文章,直接用Unity自带的工具即可
一、导入sdk
导入sdk com.google.play.assetdelivery-1.9.1.unitypackage
下载package包导入unity,即可使用
https://developers.google.com/unity/packages?hl=zh-cn#google_play
检查-菜单栏Google-Asset Delivery Settings 能正常打开即可
二、Unity 设置
2.1勾选方面
2.2 记得删除Streaming 里的文件,因为aab打包也会打出一份,多了会重复,造成包体的增大
三、AAB 资源制作
1.已有Assetbundle
1.1 直接添加文件夹,挨个修改(很麻烦,文件多了很卡,文件多了请无视)
直接点击 AddFolder,选择你的AssetBundle 文件夹,比如我的是xxx\client\AssetBundles\Android
(文件夹名字里要包含Android)
比如我想用分包的文件夹位置 xxx\client\AssetBundles\fb1就不行
这样就可以 xxx\client\AssetBundles\fb1\Android
然后修改安装方式即可,我这里用的是第一种方式 Install Time
1.2手动添加文件夹的引用
string AssetFilesPackName = "xx";
var assetPackConfig = new Google.Android.AppBundle.Editor.AssetPackConfig();
assetPackConfig.AddAssetsFolder(AssetFilesPackName, AssetBundleDirectory, AssetPackDeliveryMode.InstallTime); Google.Android.AppBundle.Editor.AssetPacks.AssetPackConfigSerializer.SaveConfig(assetPackConfig);
生成这个工具后,会在xx\client\Library\PlayAssetPackConfig.json 生成一份json配置
2.还没打AssetBundle
AssetBundleDirectory->打出后Bundle的路径
builds-> AssetBundle列表,包含文件名字和这个文件的路径
AssetBundleBuilder.BuildAssetBundles(AssetBundleDirectory, builds, AssetPackDeliveryMode.OnDemand,
null, BuildAssetBundleOptions.UncompressedAssetBundle, true);
四、编写代码解压到你指定的AssetBundle文件夹下
4.1 记录Bundle列表
我这里使用的是Unity的.asset来记录我的文件数量和文件列表,可以参考
4.2 解压你的资源
PlayAssetPackRequest request;
string assetPackName = "melojam";
request = PlayAssetDelivery.RetrieveAssetPackAsync(assetPackName);
while (!request.IsDone)
{
if (request.Status == AssetDeliveryStatus.WaitingForWifi)
{
var userConfirmationOperation = PlayAssetDelivery.ShowConfirmationDialog();
// Wait for confirmation dialog action.
yield return userConfirmationOperation;
if ((userConfirmationOperation.Error != AssetDeliveryErrorCode.NoError) ||
(userConfirmationOperation.GetResult() != ConfirmationDialogResult.Accepted))
{
// The user did not accept the confirmation. Handle as needed.
}
// Wait for Wi-Fi connection OR confirmation dialog acceptance before moving on.
yield return new WaitUntil(() => request.Status != AssetDeliveryStatus.WaitingForWifi);
}
// Use request.DownloadProgress to track download progress.
//sliderInfo.text = string.Format("copy progress: ", request.DownloadProgress);
// Use request.Status to track the status of request.
yield return null;
}
if (request.Error != AssetDeliveryErrorCode.NoError)
{
// There was an error retrieving the pack. For error codes NetworkError
// and InsufficientStorage, you may prompt the user to check their
// connection settings or check their storage space, respectively, then
// try again.
AssetDeliveryErrorCode.NoError ");
yield return null;
}
//拿出你的资源,到指定位置
var assetLocation = request.GetAssetLocation(fileName);
var assetFileStream = File.OpenRead(assetLocation.Path);
var buffer = new byte[assetLocation.Size];
assetFileStream.Seek((long)assetLocation.Offset, SeekOrigin.Begin);
assetFileStream.Read(buffer, /* offset= */ 0, buffer.Length);
using (var fsDes = File.Create(des))
{
fsDes.Write(buffer, 0, buffer.Length);
fsDes.Flush();
fsDes.Close();
}
五、Build AAB
1.Google->Build Android App Bundle
会生成一个aab
2.Google -> Build and Run
生成aab并直接安装到你的手机,省的你再解包成apks,再安装了
报错说明
Assembly 'Assets/ExternalDependencyManager/Editor/1.2.181/Google.IOSResolver.dll' will not be loaded due to errors:
这个报错请无视,不影响
测试说明
如果需要安装到手机进行测试的,可以执行下面的命令(手机需要开启Usb调试)
//生成apks
java -jar C:\apk\MTP_2.7.1.win_x64\creataab\bundletool-all-1.15.6.jar build-apks --bundle=C:\apk\MTP_2.7.1.win_x64\apkPath\testaab1.aab --output=C:\apk\MTP_2.7.1.win_x64\apkPath\testaab1.apks --ks=C:\Trunk2024\client\keystore\EntaroAudios.jks --ks-pass=pass:xxxx --ks-key-alias=EntaroAudios --key-pass=pass:xxx
//安装
adb devices
java -jar C:\apk\MTP_2.7.1.win_x64\creataab\bundletool-all-1.15.6.jar install-apks --apks=C:\apk\MTP_2.7.1.win_x64\apkPath\testaab1.apks
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/w1920097159/article/details/140402236
其他:小包处理方案
unity 制作aab包,apk空包150M以内,资源包(StreamingAssets)500M以内应该都可以-CSDN博客