/// <summary> /// 生成一个指定路径的加载器并加载资源 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url">资源唯一路径</param> /// <param name="completeHandler">加载完成回调</param> /// <param name="loadModel">加载模式(同步/异步) default=异步</param> /// <param name="loadAssetPath">加载资源路径模式(外部/Resources/StreamAsset ) default=ResourcesPath</param> protected static ByteLoader LoadAssetSync(string url, System.Action <BaseAbstracResourceLoader> onCompleteAct) { if (string.IsNullOrEmpty(url)) { Debug.LogError(string.Format("Url Can't Be Null , TypeLoader={0}", typeof(ByteLoader))); return(null); } bool isLoaderExit = false; ByteLoader byteLoader = ResourcesLoaderMgr.GetOrCreateLoaderInstance <ByteLoader>(url, ref isLoaderExit); byteLoader.m_OnCompleteAct.Add(onCompleteAct); if (isLoaderExit && byteLoader.IsCompleted) { byteLoader.LoadassetModel = LoadAssetModel.Sync; //这里貌似没必要 byteLoader.OnCompleteLoad(byteLoader.IsError, byteLoader.Description, byteLoader.ResultObj, true); //如果当前加载器已经完成加载 则手动触发事件 return(byteLoader); }//如果当前加载器存在 且已经加载完成已经完成加载 则手动触发事件 if (byteLoader.LoadassetModel == LoadAssetModel.Async) { Debug.LogEditorInfor(string.Format("有资源({0})正在异步加载过程中,同时有同步加载请求 ,则之前的加载逻辑作废!! ", url)); byteLoader.ForceBreakLoaderProcess(); }//有一个正在异步加载的加载器运行着 byteLoader.LoadassetModel = LoadAssetModel.Sync; byteLoader.LoadByteAssetSync(url); return(byteLoader); }
/// <summary> /// 卸载资源 /// </summary> /// <param name="url"></param> public static void UnLoadAsset(string url, bool isForceDelete = false) { ByteLoader byteLoader = ResourcesLoaderMgr.GetExitLoaderInstance <ByteLoader>(url); if (byteLoader == null) { //Debug.LogError("无法获取指定类型的加载器 " + typeof(ByteLoader)); return; } byteLoader.ReduceReference(byteLoader, isForceDelete); }
/// <summary> /// (异步)生成一个指定路径的加载器并加载资源 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url">资源唯一路径</param> /// <param name="completeHandler">加载完成回调</param> /// <param name="loadModel">加载模式(同步/异步) default=异步</param> /// <param name="loadAssetPath">加载资源路径模式(外部/Resources/StreamAsset ) default=ResourcesPath</param> protected static ByteLoader LoadAssetAsync(string url, System.Action <BaseAbstracResourceLoader> onCompleteAct) { if (string.IsNullOrEmpty(url)) { Debug.LogError(string.Format("Url Can't Be Null , TypeLoader={0}", typeof(ByteLoader))); return(null); } bool isLoaderExit = false; ByteLoader byteLoader = ResourcesLoaderMgr.GetOrCreateLoaderInstance <ByteLoader>(url, ref isLoaderExit); byteLoader.m_OnCompleteAct.Add(onCompleteAct); byteLoader.LoadassetModel = LoadAssetModel.Async; //这里貌似没必要(由于异步加载时候同步加载必定完成了) if (isLoaderExit) { if (byteLoader.IsCompleted) { byteLoader.OnCompleteLoad(byteLoader.IsError, byteLoader.Description, byteLoader.ResultObj, true); //如果当前加载器已经完成加载 则手动触发事件 } return(byteLoader); //如果已经存在 且当前加载器还在加载中,则只需要等待加载完成则回调用回调 } byteLoader.LoadByteAssetASync(url); return(byteLoader); }
/// <summary> /// 根据配置的路径加载优先级 选择合适的加载器加载资源 (可能存在加载不到资源的情况,目前只处理LoadAssetPathEnum.PersistentDataPath和PersistentDataPath.ResourcesPath) /// </summary> /// <param name="bridgeLoader"></param> /// <param name="url"></param> /// <param name="isloadScene"> 如果加载的是场景 则这里必须填true ,否则false</param> /// <returns></returns> private IEnumerator LoadAssetByPriorityAsync(string url, BridgeLoader bridgeLoader, bool isloadSceneAsset) { LoadAssetPathEnum curLoadAssetPathEnum = ApplicationConfig.Instance.GetFirstPriortyAssetPathEnum(); //加载的优先级 string fileName = System.IO.Path.GetFileNameWithoutExtension(url); do { if (curLoadAssetPathEnum == LoadAssetPathEnum.PersistentDataPath) { AssetBundleExitState assetBundleExitState; string newUrl = HotUpdate.AssetBundleMgr.Instance.CheckIfAssetBundleAsset(url.ToLower(), out assetBundleExitState); if (assetBundleExitState != AssetBundleExitState.None) { Debug.Log("[AssetBundler ]加载外部资源,且以AssetBundle 加载"); bridgeLoader.m_ConnectLoader = NewAssetBundleLoader3.LoadAssetBundleAsset(newUrl, fileName, LoadAssetModel.Async, assetBundleExitState, isloadSceneAsset); //整体打包的资源 } else { if (isloadSceneAsset == false) { Debug.Log("[byteLoader ]优先加载外部资源,但是不是AssetBundle 资源,则以Byte[] 尝试 加载"); string path = url; if (url.StartsWith(ConstDefine.S_AssetBundleTopPath) == false) { path = ConstDefine.S_AssetBundleTopPath + url; } bridgeLoader.m_ConnectLoader = ByteLoader.LoadAsset(path, LoadAssetModel.Async, null); } else { //***场景资源不通过这种方式 } } } else if (curLoadAssetPathEnum == LoadAssetPathEnum.ResourcesPath) { Debug.Log("[RecourcsLoader ] 加载Resources 资源 " + url); bridgeLoader.m_ConnectLoader = ResourcesLoader.LoadResourcesAsset(url, LoadAssetModel.Async, null, isloadSceneAsset); } while (bridgeLoader.m_ConnectLoader.IsCompleted == false) { yield return(null); } if (curLoadAssetPathEnum == LoadAssetPathEnum.PersistentDataPath && (bridgeLoader.m_ConnectLoader.GetType() == typeof(NewAssetBundleLoader3))) { NewAssetBundleLoader3 assetBundleLoader = bridgeLoader.m_ConnectLoader as NewAssetBundleLoader3; if (assetBundleLoader.AssetLoaderResultInfor.GetAssetByUrl(assetBundleLoader.m_ResourcesUrl, fileName) != null) { yield break; } } else { if (bridgeLoader.m_ConnectLoader.ResultObj != null) { yield break; } } // bridgeLoader.m_ConnectLoader.ReduceReference(bridgeLoader.m_ConnectLoader, false); //卸载这个加载器 ApplicationConfig.Instance.GetNextLoadAssetPath(ref curLoadAssetPathEnum); continue; //如果加载得到则返回否则继续尝试其他的加载方式 } while (curLoadAssetPathEnum != LoadAssetPathEnum.None); Debug.LogError("如果加载成功不会执行到这里" + url); bridgeLoader.m_ConnectLoader = null; //如果加载成功不会执行到这里 }