在使用 AutoStreaming 按需加载方案时,小游戏由多种类型的文件组成。这些文件根据其特性和用途,有不同的上传和管理方式。
AutoStreaming 文件: 由 AutoStreaming Editor 工具抽取出来的资源文件,运行时引擎自动下载使用;
小游戏首包数据文件: 打包小游戏时生成,主要包括引擎默认资源,首场景,Il2cpp metadata,小游戏启动时由平台下载使用;
StreamingAssets: 工程目录的 Assets / StreamingAssets
文件夹内的所有文件;
AssetBundle 文件: 游戏中打包的所有 AB 文件;
Addressables 文件: Addressables 打包的所有文件;
其他文件: 其余游戏中使用到的文件,如视频,服务器配置文件等。
文件类型 | 强制使用 UOS CDN | 自动管理上传 |
---|---|---|
AutoStreaming 文件 | 是 | 是 |
小游戏首包数据文件 | 否 | 是 |
StreamingAssets | 否 | 是 |
AssetBundle 文件 | 否 | 否 |
Addressables 文件 | 否 | 否 |
其他文件 | 否 | 否 |
对于 AssetBundle 文件、 Addressables 文件 、以及启动配置文件等等,引擎在工程的根目录提供了一个 CustomCloudAssets
目录。
开发者在打包小游戏前,应将这些文件拷贝到 CustomCloudAssets
目录下,它们随后会一起上传到 UOS CDN。
该目录下的文件将在点击 Auto Streaming -> Cfg & Publish -> Upload to UOS CDN 时随其他资源文件一起上传到 UOS CDN,文件的下载地址为:
AutoStreaming.CustomCloudAssetsRoot
字段,可以选择手动拼接URL,拼接规则为
{ Cfg & Publish 页面可复制的 Auto Streaming Path } + “/CUS/” + {自定义文件名}。为了自动化这个过程,开发者可以在打包 AssetBundle 的逻辑之后添加代码,或者使用 IPostprocessBuildWithReport
接口的 OnPostprocessBuild()
方法来自动将文件拷贝到 CustomCloudAssets
目录。
建议只拷贝需要的文件,忽略 .meta 和 .manifest 等文件。 过多的文件数量会使得上传等待时间明显变长,影响工作效率。
AutoStreaming 默认 AB 放在 CustomCloudAssets/CustomAB
目录下上传。如放置在其他文件夹,在微信小游戏平台,需要修改 Bundle Path Identifier
属性中添加值以保证微信下载缓存有效。
平台相关的资源缓存规则,可前往各小游戏平台官网了解细节:
CustomCloudAssets
目录同时也支持 Addressables ,可以将 Addressables 打包出来的文件直接放到 CustomCloudAssets
或其子目录下,并将 Addressables 的 RemoteLoadPath 设置为:
{ Cfg & Publish 页面可复制的 Auto Streaming Path } + “/CUS/” + {自定义子目录}。
UOS CDN 支持以下形式的资源下载链接格式:
https://a.unity.cn/client_api/v1/buckets/{bucketId}/release_by_badge/{badgeName}/entry_by_path/content/?path={filepath}
https://a.unity.cn/client_api/v1/buckets/{bucketId}/release_by_badge/{badgeName}/content/{filepath}
https://a.unity.cn/client_api/v1/buckets/{bucketId}/releases/{releaseId}/entry_by_path/content/?path={filepath}
https://a.unity.cn/client_api/v1/buckets/{bucketId}/releases/{releaseId}/content/{filepath}
由于引擎以及小游戏平台在缓存下载文件,计算缓存 id 时会把 “?” 之后的部分丢弃,因此建议使用不带 “?” 的 url 格式访问 UOS CDN 的文件,如果需要使用其他 CDN,也需要注意这一点。
在 C# 代码中,我们提供了字段 AutoStreaming.CustomCloudAssetsRoot
,用于拼接下载 url。该字段对应于对应上传文件的 CustomCloudAssets
目录,其值为:
{ 打包小游戏时 Cfg & Publish 页面的 Auto Streaming Path } + “/CUS”。
在小游戏中,Application.StreamingAssets = { 小游戏转换面板-游戏资源CDN } + “/StreamingAssets”。
使用 AutoStreaming 和 UOS CDN 时,可以继续使用 Application.StreamingAssets
作为根路径来访问 StreamingAssets 文件夹下的资源。如果使用其他 CDN,或 StreamingAssets 文件的上传位置不在设置的游戏资源 CDN 下,则需要手动修改下载 URL。
使用 AutoStreaming 和 UOS CDN 的情况下,C# 代码中下载 AB 的 URL 根路径为:
AutoStreaming.CustomCloudAssetsRoot+ { 自定义子目录/CustomAB } + { 带Hash的AB文件名 }。
其他情况下,需要用户自行管理下载地址。
由 Addressables package 自行管理,需要在更换 RemoteLoadPath 后重新打包才能生效。