/// <summary> /// 异步加载资源 /// </summary> /// <typeparam name="T">资源类型</typeparam> /// <param name="assetName">资源名称(完整名称,加路径及后缀名)</param> /// <param name="callback">回调函数</param> /// <returns>加载进度</returns> public IProgress LoadAsync <T>(string assetName, Action <T> callback) where T : UnityEngine.Object { var temp = Utility.Text.SplitPathName(assetName); DynamicMultiProgress progress = new DynamicMultiProgress(2, 0.9f, 0.1f); var abProgress = LoadAssetBundleAsync(temp[0], (ab) => { var request = ab.LoadAssetAsync(temp[1]); SingleTask task = new SingleTask(() => { return(request.isDone); }); task.Then(() => { callback(request.asset as T); return(true); }); task.Start(); SingleResProgress resProgress = new SingleResProgress(request); progress.Add(resProgress); }); progress.Add(abProgress); return(progress); }
public IProgress LoadAsync <T>(string assetName, System.Action <T> callback) where T : Object { var temp = Utility.Text.SplitPathName(assetName); DynamicMultiProgress progress = new DynamicMultiProgress(2); var abProgress = GetAssetBundleAsync(temp[0], (ab) => { var request = ab.LoadAssetAsync(temp[1]); SingleTask task = new SingleTask(() => { return(request.isDone); }); task.Then(() => { callback(request.asset as T); return(true); }); GameEntry.GetModule <TaskManager>().StartTask(task); SingleResProgress resProgress = new SingleResProgress(request); progress.Add(resProgress); }); progress.Add(abProgress); return(progress); }
/// <summary> /// 同步加载一组资源 /// </summary> /// <typeparam name="T">资源类型</typeparam> /// <param name="path">资源路径</param> /// <param name="isTopOnly">是否是仅加载本层级的资源</param> /// <returns>资源</returns> public IProgress LoadAllSync <T>(string path, bool isTopOnly, Action <T[]> callback) where T : UnityEngine.Object { if (isTopOnly) { return(LoadAssetBundleAsync(path, (ab) => { AssetBundleRequest request = ab.LoadAllAssetsAsync <T>(); SingleTask task = new SingleTask(() => { return request.isDone; }); task.Then(() => { callback(request.allAssets.Convert <T>()); return true; }); })); } else { List <T> assets = new List <T>(); DynamicMultiProgress dynamicProgress = new DynamicMultiProgress(2); var assetBundlesProgress = LoadAssetBundlesAsync(path, OnAssetBundleLoadComplate); return(dynamicProgress); void OnAssetBundleLoadComplate(IEnumerable <AssetBundle> assetBundles) { List <IProgress> progresses = new List <IProgress>(); foreach (var ab in assetBundles) { var request = ab.LoadAllAssetsAsync <T>(); SingleTask task = new SingleTask(() => { return(request.isDone); }); task.Then(() => { assets.AddRange(request.allAssets.Convert <T>()); return(true); }); progresses.Add(new SingleResProgress(request)); } dynamicProgress.Add(new MultiProgress(progresses.ToArray())); } } }