Unity WebGL 转换成快游戏时,首包资源大小对启动耗时影响极大,推荐按需加载资源以提升运行效率和流畅性。
Unload
)Addressables 基于 AssetBundle,提供了更方便的资源管理,包括打包、加载、释放与热更新等操作,大大简化了对原生 AssetBundle 的操作。
当使用 Addressables 并将项目打包为 WebGL 时,将生成 StreamingAssets
目录,包括 bundle 资源和 catalog/settings 配置文件:
└── StreamingAssets
├── catalog.json
├── settings.json
└── WebGL
└── *.bundle
需将整个 StreamingAssets 目录上传到服务器,并在 env.conf
文件中配置:
{
"wasmUrl": "https://vassets.vvstc.com/vassets/og2pg/o/webdemotest.zip",
"streamingAssetsUrl": "http://localhost:8080/StreamingAssets" //代表 StreamingAssets 文件夹的网络资源地址。
}
快游戏环境不支持 AssetBundle 本地加载,需使用网络方式。
已提供实用的打包工具,代码位于 QGGameTools.AssetBundleBuild
,支持修改打包路径与参数。
BuildAssetBundleOptions.AppendHashToAssetBundleName
BuildAssetBundleOptions.ChunkBasedCompression
(推荐使用 LZ4 压缩方式,加载速度和包体大小更均衡。)BuildAssetBundleOptions.DisableWriteTypeTree
public static void AssetBundleBuild()
{
string dst = Application.streamingAssetsPath + "/AssetBundles";
if (!Directory.Exists(dst))
{
Directory.CreateDirectory(dst);
}
BuildPipeline.BuildAssetBundles(dst,
BuildAssetBundleOptions.AppendHashToAssetBundleName |
BuildAssetBundleOptions.ChunkBasedCompression |
BuildAssetBundleOptions.DisableWriteTypeTree,
BuildTarget.WebGL);
}
推荐使用 UnityWebRequestAssetBundle
:
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle("http://xxxxx/AssetBundles/asset1.bundle");
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success) {
Debug.LogError(request.error);
} else {
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
// 后续进行资源加载
}