/// <summary> /// 加载资源(异步) /// </summary> /// <typeparam name="T">资源类型</typeparam> /// <param name="info">资源信息标记</param> /// <param name="loadingAction">加载中事件</param> /// <param name="loadDoneAction">加载完成事件</param> /// <param name="isPrefab">是否是加载预制体</param> /// <param name="parent">预制体加载完成后的父级</param> /// <param name="isUI">是否是加载UI</param> /// <returns>加载协程迭代器</returns> public IEnumerator LoadAssetAsync <T>(ResourceInfoBase info, HTFAction <float> loadingAction, HTFAction <T> loadDoneAction, bool isPrefab, Transform parent, bool isUI) where T : UnityEngine.Object { DateTime beginTime = DateTime.Now; if (_isLoading) { yield return(_loadWait); } _isLoading = true; yield return(Main.Current.StartCoroutine(LoadDependenciesAssetBundleAsync(info.AssetBundleName))); DateTime waitTime = DateTime.Now; UnityEngine.Object asset = null; if (LoadMode == ResourceLoadMode.Resource) { ResourceRequest request = Resources.LoadAsync <T>(info.ResourcePath); while (!request.isDone) { loadingAction?.Invoke(request.progress); yield return(null); } asset = request.asset; if (asset) { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:Resources文件夹中不存在资源 " + info.ResourcePath + "!"); } } else { #if UNITY_EDITOR if (IsEditorMode) { loadingAction?.Invoke(1); yield return(null); asset = AssetDatabase.LoadAssetAtPath <T>(info.AssetPath); if (asset) { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:路径中不存在资源 " + info.AssetPath + "!"); } } else { if (AssetBundles.ContainsKey(info.AssetBundleName)) { loadingAction?.Invoke(1); yield return(null); asset = AssetBundles[info.AssetBundleName].LoadAsset <T>(info.AssetPath); if (asset) { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !"); } } else { using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(AssetBundleRootPath + info.AssetBundleName, GetAssetBundleHash(info.AssetBundleName))) { request.SendWebRequest(); while (!request.isDone) { loadingAction?.Invoke(request.downloadProgress); yield return(null); } if (!request.isNetworkError && !request.isHttpError) { AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request); if (bundle) { asset = bundle.LoadAsset <T>(info.AssetPath); if (asset) { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !"); } if (IsCacheAssetBundle) { if (!AssetBundles.ContainsKey(info.AssetBundleName)) { AssetBundles.Add(info.AssetBundleName, bundle); } } else { bundle.Unload(false); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "请求:" + request.url + " 未下载到AB包!"); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "请求:" + request.url + " 遇到网络错误:" + request.error + "!"); } } } } #else if (AssetBundles.ContainsKey(info.AssetBundleName)) { loadingAction?.Invoke(1); yield return(null); asset = AssetBundles[info.AssetBundleName].LoadAsset <T>(info.AssetPath); if (asset) { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !"); } } else { using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(AssetBundleRootPath + info.AssetBundleName, GetAssetBundleHash(info.AssetBundleName))) { request.SendWebRequest(); while (!request.isDone) { loadingAction?.Invoke(request.downloadProgress); yield return(null); } if (!request.isNetworkError && !request.isHttpError) { AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request); if (bundle) { asset = bundle.LoadAsset <T>(info.AssetPath); if (asset) { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !"); } if (IsCacheAssetBundle) { if (!AssetBundles.ContainsKey(info.AssetBundleName)) { AssetBundles.Add(info.AssetBundleName, bundle); } } else { bundle.Unload(false); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "请求:" + request.url + " 未下载到AB包!"); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "请求:" + request.url + " 遇到网络错误:" + request.error + "!"); } } } #endif } DateTime endTime = DateTime.Now; Log.Info(string.Format("异步加载资源{0}[{1}模式]:\r\n{2}\r\n等待耗时:{3}秒 加载耗时:{4}秒" , asset ? "成功" : "失败" , LoadMode.ToString() , LoadMode == ResourceLoadMode.Resource ? info.GetResourceFullPath() : info.GetAssetBundleFullPath(AssetBundleRootPath) , (waitTime - beginTime).TotalSeconds , (endTime - waitTime).TotalSeconds)); if (asset) { DataSetInfo dataSet = info as DataSetInfo; if (dataSet != null && dataSet.Data != null) { asset.Cast <DataSetBase>().Fill(dataSet.Data); } loadDoneAction?.Invoke(asset as T); } asset = null; _isLoading = false; }
/// <summary> /// 加载资源(异步) /// </summary> /// <typeparam name="T">资源类型</typeparam> /// <param name="info">资源信息标记</param> /// <param name="loadingAction">加载中事件</param> /// <param name="loadDoneAction">加载完成事件</param> /// <param name="isPrefab">是否是加载预制体</param> /// <param name="parent">预制体加载完成后的父级</param> /// <param name="isUI">是否是加载UI</param> /// <returns>加载协程迭代器</returns> public IEnumerator LoadAssetAsync <T>(ResourceInfoBase info, HTFAction <float> loadingAction, HTFAction <T> loadDoneAction, bool isPrefab, Transform parent, bool isUI) where T : UnityEngine.Object { float beginTime = Time.realtimeSinceStartup; //单线加载,如果其他地方在加载资源,则等待 if (_isLoading) { yield return(_loadWait); } //轮到本线路加载资源 _isLoading = true; //等待相关依赖资源的加载 yield return(LoadDependenciesAssetBundleAsync(info.AssetBundleName)); float waitTime = Time.realtimeSinceStartup; UnityEngine.Object asset = null; if (LoadMode == ResourceLoadMode.Resource) { ResourceRequest request = Resources.LoadAsync <T>(info.ResourcePath); while (!request.isDone) { loadingAction?.Invoke(request.progress); yield return(null); } asset = request.asset; if (asset) { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:Resources文件夹中不存在资源 " + info.ResourcePath + "!"); } } else { #if UNITY_EDITOR if (IsEditorMode) { loadingAction?.Invoke(1); yield return(null); asset = AssetDatabase.LoadAssetAtPath <T>(info.AssetPath); if (asset) { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:路径中不存在资源 " + info.AssetPath + "!"); } } else { yield return(LoadAssetBundleAsync(info.AssetBundleName, loadingAction)); if (AssetBundles.ContainsKey(info.AssetBundleName)) { asset = AssetBundles[info.AssetBundleName].LoadAsset <T>(info.AssetPath); if (asset) { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !"); } } } #else yield return(LoadAssetBundleAsync(info.AssetBundleName, loadingAction)); if (AssetBundles.ContainsKey(info.AssetBundleName)) { asset = AssetBundles[info.AssetBundleName].LoadAsset <T>(info.AssetPath); if (asset) { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } else { throw new HTFrameworkException(HTFrameworkModule.Resource, "加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath + " !"); } } #endif } float endTime = Time.realtimeSinceStartup; //Log.Info(string.Format("异步加载资源{0}[{1}模式]:\r\n{2}\r\n等待耗时:{3}秒 加载耗时:{4}秒" // , asset ? "成功" : "失败" // , LoadMode.ToString() // , LoadMode == ResourceLoadMode.Resource ? info.GetResourceFullPath() : info.GetAssetBundleFullPath(AssetBundleRootPath) // , waitTime - beginTime // , endTime - waitTime)); if (asset) { DataSetInfo dataSet = info as DataSetInfo; if (dataSet != null && dataSet.Data != null) { asset.Cast <DataSetBase>().Fill(dataSet.Data); } loadDoneAction?.Invoke(asset as T); } else { loadDoneAction?.Invoke(null); } asset = null; //本线路加载资源结束 _isLoading = false; }
private IEnumerator LoadCoroutine <T>(ResourceInfoBase info, HTFAction <float> loadingAction, HTFAction <T> loadDoneAction, bool isPrefab = false, Transform parent = null, bool isUI = false) where T : UnityEngine.Object { if (_isLoading) { yield return(_loadWait); } _isLoading = true; UnityEngine.Object asset = null; if (Mode == ResourceMode.Resource) { ResourceRequest request = Resources.LoadAsync <T>(info.ResourcePath); while (!request.isDone) { loadingAction?.Invoke(request.progress); yield return(null); } asset = request.asset; if (!asset) { GlobalTools.LogError("加载资源失败:Resources文件夹中不存在 " + typeof(T) + " 资源 " + info.ResourcePath); } else { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } } else { #if UNITY_EDITOR loadingAction?.Invoke(1); yield return(null); asset = AssetDatabase.LoadAssetAtPath <T>(info.AssetPath); if (!asset) { GlobalTools.LogError("加载资源失败:路径中不存在资源 " + info.AssetPath); } else { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } #else if (_assetBundles.ContainsKey(info.AssetBundleName)) { loadingAction?.Invoke(1); yield return(null); asset = _assetBundles[info.AssetBundleName].LoadAsset <T>(info.AssetPath); if (!asset) { GlobalTools.LogError("加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath); } else { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } } else { UnityWebRequest request = UnityWebRequest.Get(_assetBundlePath + info.AssetBundleName); DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(request.url, 0); request.downloadHandler = handler; request.SendWebRequest(); while (!request.isDone) { loadingAction?.Invoke(request.downloadProgress); yield return(null); } if (!request.isNetworkError && !request.isHttpError) { if (handler.assetBundle) { asset = handler.assetBundle.LoadAsset <T>(info.AssetPath); if (!asset) { GlobalTools.LogError("加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath); } else { if (isPrefab) { asset = ClonePrefab(asset as GameObject, parent, isUI); } } if (IsCacheAssetBundle) { if (!_assetBundles.ContainsKey(info.AssetBundleName)) { _assetBundles.Add(info.AssetBundleName, handler.assetBundle); } } else { handler.assetBundle.Unload(false); } } else { GlobalTools.LogError("请求:" + request.url + " 未下载到AB包!"); } } else { GlobalTools.LogError("请求:" + request.url + " 遇到网络错误:" + request.error); } request.Dispose(); handler.Dispose(); } #endif } if (asset) { DataSetInfo dataSet = info as DataSetInfo; if (dataSet != null && dataSet.Data != null) { (asset as DataSet).Fill(dataSet.Data); } loadDoneAction?.Invoke(asset as T); } asset = null; _isLoading = false; }