/// <summary> /// 创建请求信息 /// </summary> /// <param name="onCreateAssetBundle">请求回调 多次请求对应多个回调</param> /// <returns></returns> public int CreateBundleRequest(int requestId, OnCreateAssetBundle onCreateAssetBundle) { if (requestId == 0) { requestId = ResourceUtils.GetRequestId(); } LoadBundleRequestInfo loadBundleRequestInfo = GetTempLoadBundleRequestInfo(); loadBundleRequestInfo.requestId = requestId; loadBundleRequestInfo.onCreateAssetBundle = onCreateAssetBundle; loadBundleRequestInfoList.Add(loadBundleRequestInfo); return(requestId); }
/// <summary> /// 异步加载bundle /// </summary> /// <param name="bundleName">bundle路径</param> /// <param name="onCreateAssetBundle">加载成功后的回调 取消加载后将回调将失效</param> /// <returns></returns> public static int CreateAssetBundleAsync(int requestId, string bundleName, OnCreateAssetBundle onCreateAssetBundle) { // 回调不存在不允加载 if (onCreateAssetBundle == null) { return(0); } LoadBundleInfo loadBundleInfo = GetLoadBundleInfo(bundleName); if (loadBundleInfo == null) { loadBundleInfo = GetTempLoadBundleInfo(); _loadBundleInfoList.Add(bundleName, loadBundleInfo); loadBundleInfo.bundleName = bundleName; EnterLoadWait(loadBundleInfo); } requestId = loadBundleInfo.CreateBundleRequest(requestId, onCreateAssetBundle); // 未请求 if (loadBundleInfo.step == LoadBundleStep.LoadWait) { // 1 获取依赖列表 List <string> dependencies = BundleDependencies.GetAssetBundleDependencies(bundleName); // 2 递归请求加载 if (dependencies != null && dependencies.Count > 0) { foreach (string tempBundleName in dependencies) { CreateAssetBundleAsync(requestId, tempBundleName, (string tBundleName, AssetBundle tAssetBundle, int tRequestId) => { // 回调返回会增加引用 所以这里直接释放 ReferenceBundle.ReleaseBundle(tBundleName); }); } } // 3 正加载列表 List <LoadBundleInfo> loadingBundleInfoList = GetLoadBundleStateList(LoadBundleStep.Loading); if (loadingBundleInfoList.Count < _frameLoadMaxCount) { LoadAssetBundle(loadBundleInfo); } } return(requestId); }