【Unity开发】避免重复加载场景资产AB(AssetBundle)包的优化
@[TOC](【Unity开发】避免重复加载场景资产AB(AssetBundle)包的优化)
项目需要远程加载场景AB包,并加载场景。
1、 初始方案
1.1 异步加载场景资产(获取AB包数据)
public IEnumerator LoadAssetFromAB(string url, Action<AssetBundle> assetAB){yield return new WaitForSeconds(0.16f);using (UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(url)){yield return webRequest.SendWebRequest();if (webRequest.result == UnityWebRequest.Result.Success){var bundle = DownloadHandlerAssetBundle.GetContent(webRequest);assetAB?.Invoke(bundle);}}}
1.2 异步加载场景(进入场景)
// 异步加载远程AB场景public void LoadSceneFromAB(AssetBundle assetAB, string sceneName){// 启动异步线程加载场景StartCoroutine(AssetToScene(assetAB, sceneName));