Android 打包AAB+PAD(Unity篇)

支持的格式及支持相应格式设备所占的百分比:

使用 API 配置资产包


你可以通过编辑器脚本配置资产交付,这些脚本可以作为自动构建系统的一部分运行。

使用 AssetPackConfig 该类来定义要包含在 Android App Bundle 构建中的资产,以及模式。这些资产包不需要包含 AssetBundle。

public void ConfigureAssetPacks {

// Creates an AssetPackConfig with a single asset pack, named

// examplePackName, containing all the files in path/to/exampleFolder.

var assetPackConfig = new AssetPackConfig();

assetPackConfig.AddAssetsFolder(“examplePackName”,

“path/to/exampleFolder”,

AssetPackDeliveryMode.OnDemand);

// Configures the build system to use the newly created assetPackConfig when

// calling Google > Build and Run or Google > Build Android App Bundle.

AssetPackConfigSerializer.SaveConfig(assetPackConfig);

// Alternatively, use BundleTool.BuildBundle to build an App Bundle from script.

BuildBundle(new buildPlayerOptions(), assetPackConfig);

}

你还可以使用类中的静态 BuildBundle 方法Bundletool生成带有资产包的 Android App Bundle,给定 「BuildPlayerOptions」「AssetPackConfig」

Play Asset Delivery Unity API 集成

================================

「Play Asset Delivery Unity API」 提供了请求资产包,下载管理,和访问资源的功能。**「确保」**首先将 Unity 插件添加到你的项目中。

你在 API 中使用的函数取决于你创建资产包的方式。

  • 如果你使用 UI 配置 AssetBundles,请**「选择插件配置的资产包」**。

  • 如果你使用 API 配置资产包,请选**「择API 配置的资产包」**。

你可以根据要访问的资产包的交付类型实施 API。这些步骤显示在以下流程图中。

检索 AssetBundles


导入 Play Asset Delivery API 并调用该 RetrieveAssetBundleAsync()方法来检索 AssetBundle。

using Google.Play.AssetDelivery;

// Loads the AssetBundle from disk, downloading the asset pack containing it if necessary.

PlayAssetBundleRequest bundleRequest = PlayAssetDelivery.RetrieveAssetBundleAsync(asset-bundle-name);

安装时交货


资产包配置为**「install-time」**在应用程序启动时立即可用。你可以使用以下命令从 AssetBundle 加载场景:

AssetBundle assetBundle = bundleRequest.AssetBundle;

// You may choose to load scenes from the AssetBundle. For example:

string[] scenePaths = assetBundle.GetAllScenePaths();

SceneManager.LoadScene(scenePaths[path-index]);

快速跟进和按需交付


这些部分适用于**「fast-follow」「on-demand」**资产包。

检查状态


每个资产包都存储在应用程序内部存储的单独文件夹中。使用该 「isDownloaded()」 方法确定是否已下载资产包。

监控下载


查询PlayAssetBundleRequest 监控请求状态的 对象:

// Download progress of request, between 0.0f and 1.0f. The value will always be

// 1.0 for assets delivered as install-time.

// NOTE: A value of 1.0 will only signify the download is complete. It will still need to be loaded.

float progress = bundleRequest.DownloadProgress;

// Returns true if:

//   * it had either completed the download, installing, and loading of the AssetBundle,

//   * OR if it has encountered an error.

bool done = bundleRequest.IsDone;

// Returns status of retrieval request.

AssetDeliveryStatus status = bundleRequest.Status;

switch(status) {

case AssetDeliveryStatus.Pending:

// Asset pack download is pending - N/A for install-time assets.

case AssetDeliveryStatus.Retrieving:

// Asset pack is being downloaded and transferred to app storage.

// N/A for install-time assets.

case AssetDeliveryStatus.Available:

// Asset pack is downloaded on disk but NOT loaded into memory.

// For PlayAssetPackRequest(), this indicates that the request is complete.

case AssetDeliveryStatus.Loading:

// Asset pack is being loaded.

case AssetDeliveryStatus.Loaded:

// Asset pack has finished loading, assets can now be loaded.

// For PlayAssetBundleRequest(), this indicates that the request is complete.

case AssetDeliveryStatus.Failed:

// Asset pack retrieval has failed.

case AssetDeliveryStatus.WaitingForWifi:

// Asset pack retrieval paused until either the device connects via Wi-Fi,

// or the user accepts the PlayAssetDelivery.ShowCellularDataConfirmation dialog.

default:

break;

}

大量下载


大于 150MB 的资产包可以自动下载,但只能通过 Wi-Fi 下载。如果用户未连接到 Wi-Fi,则PlayAssetBundleRequest状态设置为 AssetDeliveryStatus.WaitingForWifi 并暂停下载。在这种情况下,要么等待设备连接到 Wi-Fi,然后继续下载,要么提示用户批准通过蜂窝连接下载包。

if(bundleRequest.Status == AssetDeliveryStatus.WaitingForWifi) {

var userConfirmationOperation = PlayAssetDelivery.ShowCellularDataConfirmation();

yield return userConfirmationOperation;

switch(userConfirmationOperation.GetResult()) {

case ConfirmationDialogResult.Unknown:

// userConfirmationOperation finished with an error. Something went

// wrong when displaying the prompt to the user, and they weren’t

// able to interact with the dialog. In this case, we recommend

// developers wait for Wi-Fi before attempting to download again.

// You can get more info by calling GetError() on the operation.

case ConfirmationDialogResult.Accepted:

// User accepted the confirmation dialog - download will start

// automatically (no action needed).

case ConfirmationDialogResult.Declined:

// User canceled or declined the dialog. Await Wi-Fi connection, or

// re-prompt the user.

default:

break;

}

}

取消请求(仅限按需)


如果需要在 AssetBundles 加载到内存之前取消请求,请调用 对象AttemptCancel() 上的方法 PlayAssetBundleRequest:

// Will only attempt if the status is Pending, Retrieving, or Available - otherwise

// it will be a no-op.

bundleRequest.AttemptCancel();

// Check to see if the request was successful by checking if the error code is Canceled.

if(bundleRequest.Error == AssetDeliveryErrorCode.Canceled) {

// Request was successfully canceled.

}

异步请求资产包


在大多数情况下,你应该使用 Coroutines异步请求资产包并监控进度,如下所示:

private IEnumerator LoadAssetBundleCoroutine(string assetBundleName) {

PlayAssetBundleRequest bundleRequest =

PlayAssetDelivery.RetrieveAssetBundleAsync(assetBundleName);

while (!bundleRequest.IsDone) {

if(bundleRequest.Status == AssetDeliveryStatus.WaitingForWifi) {

var userConfirmationOperation = PlayAssetDelivery.ShowCellularDataConfirmation();

// 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(() => bundleRequest.Status != AssetDeliveryStatus.WaitingForWifi);

}

// Use bundleRequest.DownloadProgress to track download progress.

// Use bundleRequest.Status to track the status of request.

yield return null;

}

if (bundleRequest.Error != AssetDeliveryErrorCode.NoError) {

// There was an error retrieving the bundle. 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.

yield return null;

}

// Request was successful. Retrieve AssetBundle from request.AssetBundle.

AssetBundle assetBundle = bundleRequest.AssetBundle;

其他 Play Core API 方法


以下是你可能希望在应用中使用的一些其他 API 方法。

检查下载大小

通过对 Google Play 进行异步调用并设置操作完成时的回调方法来检查 AssetBundle 的大小:

public IEnumerator GetDownloadSize() {

PlayAsyncOperation getSizeOperation =

PlayAssetDelivery.GetDownloadSize(assetPackName);

yield return getSizeOperation;

if(operation.Error != AssetDeliveryErrorCode.NoError) {

// Error while retrieving download size.

} else {

// Download size is given in bytes.

long downloadSize = operation.GetResult();

}

}

移除 AssetBundles

你可以删除当前未加载到内存中的快速关注和按需 AssetBundle。进行以下异步调用并设置完成时的回调方法:

PlayAsyncOperation removeOperation = PlayAssetDelivery.RemoveAssetPack(assetBundleName);

removeOperation.Completed += (operation) =>

{

if(operation.Error != AssetDeliveryErrorCode.NoError) {

// Error while attempting to remove AssetBundles.

最后

一线互联网Android面试题含详解(初级到高级专题)

这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率

如果设置门槛,很多开发者朋友会因此错过这套高级架构资料,错过提升成为架构师的可能。这就失去了我们的初衷;让更多人都能通过高效高质量的学习,提升自己的技术和格局,升职加薪。

最后送给大家一句话,望共勉,永远不要放弃自己的梦想和追求;

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

互联网Android面试题含详解(初级到高级专题)**

这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率

[外链图片转存中…(img-d3kVB8aW-1714321016166)]

如果设置门槛,很多开发者朋友会因此错过这套高级架构资料,错过提升成为架构师的可能。这就失去了我们的初衷;让更多人都能通过高效高质量的学习,提升自己的技术和格局,升职加薪。

最后送给大家一句话,望共勉,永远不要放弃自己的梦想和追求;

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

React Hooks 是 React 16.8 中新增的特性,它可以让你在函数组件中使用 state、生命周期钩子等 React 特性。使用 Hooks 可以让你写出更简洁、可复用且易于测试的代码。 React Hooks 提供了一系列的 Hook 函数,包括 useState、useEffect、useContext、useReducer、useCallback、useMemo、useRef、useImperativeHandle、useLayoutEffect 和 useDebugValue。每个 Hook 都有特定的用途,可以帮助你处理不同的问题。 下面是 React Hooks 的一些常用 Hook 函数: 1. useState useState 是最常用的 Hook 之一,它可以让你在函数组件中使用 state。useState 接受一个初始状态值,并返回一个数组,数组的第一个值是当前 state 值,第二个值是更新 state 值的函数。 ``` const [count, setCount] = useState(0); ``` 2. useEffect useEffect 可以让你在组件渲染后执行一些副作用操作,比如订阅事件、异步请求数据等。useEffect 接受两个参数,第一个参数是一个回调函数,第二个参数是一个数组,用于控制 useEffect 的执行时机。 ``` useEffect(() => { // 这里可以执行副作用操作 }, [dependencies]); ``` 3. useContext useContext 可以让你在组件树中获取 context 的值。它接受一个 context 对象,并返回该 context 的当前值。 ``` const value = useContext(MyContext); ``` 4. useRef useRef 可以让你在组件之间共享一个可变的引用。它返回一个对象,该对象的 current 属性可以存储任何值,并在组件的生命周期中保持不变。 ``` const ref = useRef(initialValue); ref.current = value; ``` 5. useCallback useCallback 可以让你缓存一个函数,以避免在每次渲染时都创建一个新的函数实例。它接受一个回调函数和一个依赖数组,并返回一个 memoized 的回调函数。 ``` const memoizedCallback = useCallback(() => { // 这里是回调函数的逻辑 }, [dependencies]); ``` 6. useMemo useMemo 可以让你缓存一个计算结果,以避免在每次渲染时都重新计算。它接受一个计算函数和一个依赖数组,并返回一个 memoized 的计算结果。 ``` const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]); ``` 以上就是 React Hooks 的一些常用 Hook 函数,它们可以帮助你更好地处理组件状态、副作用、上下文和性能优化等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值