美文网首页
Addressables基础知识

Addressables基础知识

作者: SeanLink | 来源:发表于2023-07-23 17:46 被阅读0次

安装

image.png

查看AA文件

image.png

创建资源组


image.png

打包资源


image.png

打包到的路径


image.png

具体在Library下面的com.unity......./aa文件夹里面


image.png

手动生成物体

using UnityEngine.AddressableAssets;
public AssetReference rePrefab;
Addressables.InstantiateAsync(rePrefab, Random.insideUnitSphere * 3, Quaternion.identity);

资源路径简化

image.png

简化后


image.png

根据名字加载指定资源(先简化资源)

Addressables.InstantiateAsync("remote", Random.insideUnitSphere * 3, Quaternion.identity);

更改资源组加载策略

远程加载目录配置 注意端口号


image.png

资源配置加载方式


image.png

远程打包,远程加载

加载方式更改

image.png

制作补丁包

image.png
image.png

需要先设定工程


image.png

资源加载完毕后回调

Addressables.InstantiateAsync("remote", Random.insideUnitSphere * 3, Quaternion.identity).Completed += LoadCompleted;

private void LoadCompleted(UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<GameObject> obj)
{

}

await关键字

GameObject go = await Addressables.InstantiateAsync("remote", Random.insideUnitSphere * 3, Quaternion.identity).Task;

资源加载与释放
本地服务器测试


image.png

将RemoteLoadPath改成http://[PrivateIpAddress]:[HostingServicePort]

Addressables.InitializeAsync() - 初始化Addressables

Addressables.LoadAssetAsync() - 加载资源

Addressables.LoadAssetsAsync() - 加载多个资源

Addressables.InstantiateAsync() - 实例化资源

Addressables.Release() -释放资源

Addressables.ClearDependencyCacheAsync() - 清除依赖缓存

Addressables.ClearResourceLocators() - 清除资源定位器

Addressables.DownloadDependenciesAsync() - 下载依赖资源

Addressables.GetDownloadSizeAsync() - 获取需要下载的资源大小

Addressables.GetDownloadSizeAsync() - 获取需要下载的资源大小

Addressables.GetResourceLocationsAsync() - 获取资源的定位器

Addressables.GetWebUri() - 获取Web URI

Addressables.UpdateCatalogsAsync() - 更新目录

Addressables.UpdateResourceLocatorsAsync() - 更新资源定位器

Addressables.MergeDependenciesAsync() - 合并依赖

Addressables.Preload() - 预加载

Addressables.UnloadSceneAsync() - 卸载场景

Addressables.UnloadAssetAsync() - 卸载资源

Addressables.UnloadAllAssets() - 卸载所有资源

Addressables.UnloadUnusedAssets() - 卸载未使用的资源

Addressables.ClearCache() - 清除缓存

Addressables.GetDownloadSizeAsync() - 获取需要下载的资源大小

Addressables.GetResourceLocationsAsync() - 获取资源的定位器

Addressables.GetWebUri() - 获取Web URI

Addressables.InitializeAsync() - 初始化Addressables

Addressables.LoadAssetAsync() - 加载资源

Addressables.LoadAssetsAsync() - 加载多个资源

Addressables.InstantiateAsync() - 实例化资源

Addressables.Release() -释放资源

Addressables.ClearDependencyCacheAsync() - 清除依赖缓存

Addressables.ClearResourceLocators() - 清除资源定位器

Addressables.DownloadDependenciesAsync() - 下载依赖资源

Addressables.GetDownloadSizeAsync() - 获取需要下载的资源大小

Addressables.GetResourceLocationsAsync() - 获取资源的定位器

Addressables.GetWebUri() - 获取Web URI

Addressables.UpdateCatalogsAsync() - 更新目录

Addressables.UpdateResourceLocatorsAsync() - 更新资源定位器

Addressables.MergeDependenciesAsync() - 合并依赖

Addressables.Preload() - 预加载

Addressables.UnloadSceneAsync() - 卸载场景

Addressables.UnloadAssetAsync() - 卸载资源

Addressables.UnloadAllAssets() - 卸载所有资源

Addressables.UnloadUnusedAssets() - 卸载未使用的资源

Addressables.ClearCache() - 清除缓存

Settings

2.1 AssetGroup

Group提供三种压缩方式:无压缩、LZ4、LZMA。LZ4包体大小中等,解压较快,可以只解压部分资源;LZMA包体较小,但解压资源较慢,只能将资源全部解压。通常选择LZ4。


image.png
  • Include in Build是指构建时是否包括此Group。
  • Force Unique Provider自定义的Provider实现,则需要勾选,自定义Provider(不在组间共享)。
  • Use Asset Bundle Cache表示是否缓存下载的AB包,不缓存的话每次都要下载。
  • Asset Bundle CRC表示加载资源时是否进行CRC校验文件准确性。
  • Use UnityWebRequest - 表示使用UnityWebRequestAssetBundle.GetAssetBundle接口代替AssetBundle.LoadFromFileAsync接口去加载本地资源。不建议勾选
image.png
  • Request Timeout:链接超时后,进行Abort操作。此处表示超时的second
  • Use Http Chunked Transfer(在Unity2019.3+已经被废弃,不建议勾选)
  • Http Redirect Limit:使用UnityWebRequest下载资源时重定向的次数
  • Retry Count:下载失败后重新尝试下载的次数
image.png

这三个选项表示打包时,是否将资源的Address、GUID、Label写入包体中。对于有分包更新需求的情况下,建议都勾选上。


image.png

Internal Asset Naming Mode:如何为内部asset命名

  • Full Path:用完整路径
  • Filename:资源的文件名
  • GUID:用GUID串
  • Dynamic:Addressables根据组内的资产选择最小的内部命名
  • Internal Bundle Id Mode(如何为内部bundle命名):
  • Cached Clear Behaviour:空间不足时清理Cache;有新版本资源时,清理Cache
  • Bundle Mode:打资源的策略
    • Pack Together:根据Group打包
    • Pack Separately:每个文件打一个包
    • Pack Together By Label:根据Lable进行分包
  • Bundle Naming Mode:如何创建bundle的名字

Asset Load Mode、Asset Provider、Asset Bundle Provider通常不用修改,这三个选项用于控制asset的加载方式、AB包的获取方式。如果存在自定义的处理方式,可自行选择。


image.png

2.2 AddressableAssetSettings

Profile定义了各种路径参数,切换不同的路径以灵活映射到的路径。(修改路径后需要重新Build资源后才能生效,因为locator的下载路径是在Build时序列化到本地的。体验上不太舒服~~)


image.png
  • Send Profiler Eevet:向Event View 窗口发送事件,以监听资源加载状态
  • Log Runtime Exceptions:发生错误时,Addressable内部会抛出异常
image.png
  • Player Version Override:默认Catalog的命名是Catalog_<timestamp>.json,如果配置了该项可能会影响catalog名称。若发布客户端后,修改此项发布资源,则原客户端不会检测到存在资源更新。
  • Compress Local Catalog\Optimize Catalog Size:优化Catlalog文件大小,通常不会勾选,除非此文件影响流程体验。
  • 当需要发布远端资源时,勾选Build Remote Catalog
  • 当需要手动更新远端资源时,需要勾选Build Remote Path & Only update catalog manually(不勾选的情况下,也不是每次都下载hash文件进行对比,保险还是手动下载)
image.png
  • Player Version Override:资源版本id
  • Check for Update Issues : 点击Addressables Groups/Tools/Check for Content Update Restriction的处理方式。(将静态资源修改放到一个新包中,通常用于对Local资源的修改)
  • Content State Build Path:Addressables第一生成资源时,会生成addressables_content_state.bin文件,用于对比记录后续资源更新时的内容。该文件默认存储于AddressableAssetsData/Windows
image.png
  • Custom certificate handler:自定义证书验证,继承UnityEngine.Networking.CertificateHandler.
  • Max Concurrent Web Requests:最大的Web请求数量
  • Catalog Downloaded Timeout:下载catalog文件超时时间
image.png
  • Ignore Invalid / Unsupported Files in Build:忽略打包时的无效资源(测试.bin属于无效文件)
  • Unique Bundle ID:使用此设置可以在资源已经被加载进内存之后依然支持动态更新资源。其代价是在更新资源时,引用修改的资源也会被更新。不建议勾选。
  • Contiguous Bundles:优化打包时的资源时序,提升资源加载速度。(Addressables 1.12.1以前的版本想要减小包体,不要勾选)
  • Non-Recursive Dependency Calculation:开启该选项可以提高构建速度同时减少运行内存的占用,但资源存在循环依赖时会导致加载失败。不建议开启。
  • Shader Bundle Naming Prefix:Unity内置Shader Bundle命名,建议Project Name Hash
  • MonoScript Bundle Naming Prefix:MonoScripts生成的Bundle命名,建议Project Name Hash
  • Strip Unity Version From AssetBundles:是否移除Bundle包头的Unity版本信息(Unity版本不发生变化的情况下,可以勾选)
  • Disable Visible Sub Asset Representations:在不使用Sub Asset(Sprites、sub-meshes)情况下可以提升打包时间
image.png

Addressables提供了三种Editor下资源加载方式:Use the Asset database(Asset database加载)、Simulate groups(模拟Group处理)、Use existing build(加载实际包体资源)


image.png
  • New Build/Default Build Script:默认方式构建资源
  • Update a Previous Build:增量式更新资源
  • Clear Build Cache:清除构建的资源缓存(只是清除了link,实际目录下的资源还在)
image.png

相关文章

网友评论

      本文标题:Addressables基础知识

      本文链接:https://www.haomeiwen.com/subject/htzfpdtx.html