internal ResourceLoadedParameters(string path, Il2CppSystem.Type type, ResourceLoadType loadType) #endif { Path = path; Type = type; LoadType = loadType; }
public DataObject GetData(ResourceID id, ResourceLoadType loadType) { DataObject obj; if (dataObjects.ContainsKey(id)) { obj = dataObjects[id]; } else if (loadType == ResourceLoadType.Now) { IDataProcessor processor = GetDataProcessor(id.Format); engine.IncreaseLoadingCount(); obj = processor.Process(id, this); Log.Debug("load \"" + id + "\""); obj.resourceManager = this; obj.DataName = id; obj.PostProcess(); engine.DecreaseLoadingCount(); dataObjects.Add(id, obj); } else { obj = new NullDataObject(id, this); } return(obj); }
/// <summary> /// 真正执行资源卸载指定类型不再使用的资源接口 /// </summary> /// <param name="resourceloadtype">资源加载类型</param> protected override void doUnloadSpecificLoadTypeUnsedResource(ResourceLoadType resourceloadtype) { // 递归判定卸载所有不再可用的正常加载AB bool hasunsedab = true; while (hasunsedab) { // 检查回收不再使用正常已加载的AB foreach (var loadedab in mAllLoadedResourceInfoMap[resourceloadtype]) { if (loadedab.Value.IsUnsed) { mUnsedABInfoList.Add(loadedab.Value); } } if (mUnsedABInfoList.Count == 0) { //不再有可卸载的AB hasunsedab = false; } else { // 有可卸载的AB for (int i = 0; i < mUnsedABInfoList.Count; i++) { mAllLoadedResourceInfoMap[resourceloadtype].Remove(mUnsedABInfoList[i].AssetBundleName); mUnsedABInfoList[i].dispose(); } mUnsedABInfoList.Clear(); } } }
/// <summary> /// 设置TImage指定图片(单图的时候) /// </summary> /// <param name="timg">TImage组件</param> /// <param name="spritepath">Sprite路径</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> /// <returns></returns> public void setTImageSingleSprite(TImage timg, string spritepath, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { DIYLog.Assert(timg == null, "setTImageSingleSprite!"); ResourceModuleManager.Singleton.requstResource(spritepath, (abi) => { // 清除老的资源引用 if (timg.ABI != null && !string.IsNullOrEmpty(timg.AtlasPath)) { timg.ABI.releaseOwner(timg); } if (abi != null) { // Sprite统一为小写 var spritename = Path.GetFileNameWithoutExtension(spritepath).ToLower(); var sprite = abi.getAsset <Sprite>(timg, spritename); timg.sprite = sprite; timg.ABI = abi; timg.AtlasPath = spritepath; timg.SpriteName = spritename; } else { timg.ABI = null; timg.AtlasPath = string.Empty; timg.SpriteName = string.Empty; } }, loadtype, loadmethod); }
/// <summary> /// 强制卸载指定资源(不管AB加载后属于哪一种类型,强制卸载) /// </summary> /// <param name="abname"></param> public void forceUnloadSpecificResource(string abname) { ResourceLoadType resourceloadtype = ResourceLoadType.NormalLoad; AbstractResourceInfo arinfo = null; foreach (var loadedabinfomap in mAllLoadedResourceInfoMap) { if (arinfo != null) { break; } foreach (var loadedabinfo in loadedabinfomap.Value) { if (loadedabinfo.Key.Equals(abname)) { arinfo = loadedabinfo.Value; resourceloadtype = loadedabinfomap.Key; break; } } } if (arinfo != null) { mAllLoadedResourceInfoMap[resourceloadtype].Remove(arinfo.AssetBundleName); arinfo.dispose(); ResourceLogger.log(string.Format("AB资源 : {0}已强制卸载!", abname)); } else { ResourceLogger.logErr(string.Format("AB资源 : {0}未被加载,无法强制卸载!", abname)); } }
/// <summary> /// 卸载指定类型不再使用的资源(Note:不支持卸载常驻资源类型) /// </summary> /// <param name="resourceloadtype">资源加载类型</param> protected void unloadSpecificLoadTypeUnsedResource(ResourceLoadType resourceloadtype) { if (resourceloadtype == ResourceLoadType.PermanentLoad) { ResourceLogger.logErr("不允许卸载常驻AB资源!"); return; } doUnloadSpecificLoadTypeUnsedResource(resourceloadtype); }
/// <summary> /// 设置Image指定图片 /// </summary> /// <param name="img">Image组件</param> /// <param name="atlasname">图集名</param> /// <param name="spritename">图片名</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> /// <returns></returns> public void setImageSprite(Image img, string atlasname, string spritename, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { ResourceModuleManager.Singleton.requstResource(atlasname, (abi) => { var sprite = abi.getAsset <Sprite>(img, spritename); img.sprite = sprite; }, loadtype, loadmethod); }
/// <summary> /// 获取指定加载类型的已加载资源信息映射Map /// </summary> /// <param name="loadtype">资源加载类型</param> /// <returns></returns> public Dictionary <string, AbstractResourceInfo> getSpecificLoadTypeARIMap(ResourceLoadType loadtype) { if (mAllLoadedResourceInfoMap.ContainsKey(loadtype)) { return(mAllLoadedResourceInfoMap[loadtype]); } else { ResourceLogger.logErr(string.Format("找不到资源类型 : {0}的已加载AB信息!", loadtype)); return(null); } }
public void CopyTo(Sprite Sprite) { Sprite.resMan = resMan; Sprite.id = id; Sprite.internalFrames = internalFrames; Sprite.LoadType = LoadType; if (ani != null) { Sprite.ani = new SpriteAnimation(ani.FrameCount); } Sprite.Resolution = Resolution; }
/// <summary> /// 加载指定图集 /// </summary> /// <param name="atlasname">图集名</param> /// <param name="callback">资源回调</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> public void loadAtlas(string atlasname, Action callback, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { ResourceModuleManager.Singleton.requstResource( atlasname, (abi) => { abi.loadAllAsset <Sprite>(); callback?.Invoke(); }, loadtype, loadmethod); }
/// <summary> /// 获取模型实例对象 /// </summary> /// <param name="resname"></param> /// <param name="callback"></param> /// <param name="loadtype"></param> /// <param name="loadmethod"></param> public void getModelInstance(string resname, Action <GameObject> callback, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { ResourceModuleManager.Singleton.requstResource( resname, (abi) => { var modelinstance = abi.instantiateAsset(resname); #if UNITY_EDITOR ResourceUtility.FindMeshRenderShaderBack(modelinstance); #endif callback(modelinstance); }); }
/// <summary> /// 更新已加载AB的加载类型 /// </summary> /// <param name="resname">资源名</param> /// <param name="oldloadtype">旧的加载类型</param> /// <param name="newloadtype">新的家在类型</param> protected void updateLoadedResourceInfoLoadType(string resname, ResourceLoadType oldloadtype, ResourceLoadType newloadtype) { if (mAllLoadedResourceInfoMap[oldloadtype].ContainsKey(resname)) { var abi = mAllLoadedResourceInfoMap[oldloadtype][resname]; mAllLoadedResourceInfoMap[newloadtype].Add(resname, abi); mAllLoadedResourceInfoMap[oldloadtype].Remove(resname); ResourceLogger.log(string.Format("已加载的资源 : {0}从资源类型 : {1}更新到资源类型 : {2}!", resname, oldloadtype, newloadtype)); } else { ResourceLogger.logErr(string.Format("资源类型 : {0}里找不到已加载的资源 : {1},无法更新该资源的加载类型!", oldloadtype, resname)); } }
/// <summary> /// 设置Image指定图片(单图的时候) /// </summary> /// <param name="img">Image组件</param> /// <param name="spritepath">Sprite路径</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> /// <returns></returns> public void setImageSingleSprite(Image img, string spritepath, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { DIYLog.Assert(img == null, "setImageSingleSprite!"); ResourceModuleManager.Singleton.requstResource(spritepath, (abi) => { // Sprite统一为小写 var spritename = Path.GetFileNameWithoutExtension(spritepath).ToLower(); var sprite = abi.getAsset <Sprite>(img, spritename); img.sprite = sprite; }, loadtype, loadmethod); }
/// <summary> /// 加载所有Shader /// </summary> /// <param name="respath">资源路径</param> /// <param name="callback">资源会动啊</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> public void loadAllShader(string respath, Action callback, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { ResourceModuleManager.Singleton.requstResource( respath, (abi) => { var svc = abi.loadAsset <ShaderVariantCollection>(ResourceConstData.ShaderVariantsAssetName); // Shader通过预加载ShaderVariantsCollection里指定的Shader来进行预编译 svc?.WarmUp(); // SVC的WarmUp就会触发相关Shader的预编译,触发预编译之后再加载Shader Asset即可 abi.loadAllAsset <Shader>(); callback?.Invoke(); }, loadtype, loadmethod); }
/// <summary> /// 真正执行递归卸载指定类型不再使用的资源接口 /// </summary> /// <param name="resourceloadtype">资源加载类型</param> protected override void doUnloadSpecificLoadTypeUnsedResource(ResourceLoadType resourceloadtype) { // 递归判定卸载所有不再可用的正常加载资源 bool iscomplete = false; bool hasunsedres = false; while (!iscomplete) { // 检查回收不再使用正常已加载的AB foreach (var loadedab in mAllLoadedResourceInfoMap[resourceloadtype]) { if (loadedab.Value.IsUnsed) { mUnsedResourceInfoList.Add(loadedab.Value); } } if (mUnsedResourceInfoList.Count == 0) { //不再有可卸载的资源 iscomplete = true; } else { hasunsedres = true; // 有可卸载的组他吧 for (int i = 0; i < mUnsedResourceInfoList.Count; i++) { mAllLoadedResourceInfoMap[resourceloadtype].Remove(mUnsedResourceInfoList[i].AssetBundlePath); mUnsedResourceInfoList[i].dispose(); } mUnsedResourceInfoList.Clear(); } } if (iscomplete && hasunsedres) { //Resources.UnloadAsset()只是标记该资源能被卸载,并不能真正卸载资源 //同时AssetDatabase模式下,没有依赖资源的概念, //为了确保不再使用的资源被正确卸载(模拟AssetBundle模式的加载管理环境) //通过调用Resources.UnloadUnusedAssets()方法来测试卸载资源 Resources.UnloadUnusedAssets(); } }
/// <summary> /// 设置Image指定图片 /// </summary> /// <param name="trawimg">Image组件</param> /// <param name="texturename">纹理名</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> /// <returns></returns> public void setRawImage(TRawImage trawimg, string texturename, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { DIYLog.Assert(trawimg == null, "setRawImage不允许传空TRawImage!"); ResourceModuleManager.Singleton.requstResource(texturename, (abi) => { // 清除老的资源引用 if (trawimg.ABI != null && !string.IsNullOrEmpty(trawimg.TextureName)) { trawimg.ABI.releaseOwner(trawimg); } if (abi != null) { var texture = abi.getAsset <Texture>(trawimg, texturename); trawimg.texture = texture; } trawimg.ABI = abi; trawimg.TextureName = texturename; }, loadtype, loadmethod); }
/// <summary> /// 设置Image指定图片 /// </summary> /// <param name="timg">Image组件</param> /// <param name="atlasname">图集名</param> /// <param name="spritename">图片名</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> /// <returns></returns> public void setImageSprite(TImage timg, string atlasname, string spritename, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { DIYLog.Assert(timg == null, "setImageSprite不允许传空TImage!"); ResourceModuleManager.Singleton.requstResource(atlasname, (abi) => { // 清除老的资源引用 if (timg.ABI != null) { timg.ABI.releaseOwner(timg); } if (abi != null) { var sprite = abi.getAsset <Sprite>(timg, spritename); timg.sprite = sprite; } timg.ABI = abi; timg.AtlasName = atlasname; timg.SpriteName = spritename; }, loadtype, loadmethod); }
/// <summary> /// 获取一个实例资源对象 /// </summary> /// <param name="respath">资源路径</param> /// <param name="callback">资源回调</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> /// <returns></returns> public void getPrefabInstance(string respath, Action <GameObject> callback = null, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { ResourceModuleManager.Singleton.requstResource(respath, (abi) => { var assetname = Path.GetFileName(respath); var prefabinstance = abi.instantiateAsset(assetname); #if UNITY_EDITOR ResourceUtility.FindMeshRenderShaderBack(prefabinstance); #endif callback?.Invoke(prefabinstance); }, loadtype, loadmethod); }
/// <summary> /// 获取指定音效 /// </summary> /// <param name="owner"></param> /// <param name="respath"></param> /// <param name="callback"></param> /// <param name="loadtype"></param> /// <param name="loadmethod"></param> public void getAudioClip(UnityEngine.Object owner, string respath, Action <AudioClip> callback = null, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { ResourceModuleManager.Singleton.requstResource(respath, (ari) => { var assetname = Path.GetFileName(respath); var clip = ari.getAsset <AudioClip>(owner, assetname); callback?.Invoke(clip); }, loadtype, loadmethod); }
/// <summary> /// 请求资源 /// 资源加载统一入口 /// </summary> /// <param name="resname">资源AB名</param> /// <param name="completehandler">加载完成上层回调</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> public void requstResource(string resname, AbstractResourceModule.LoadResourceCompleteHandler completehandler, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { CurrentResourceModule.requstResource(resname, completehandler, loadtype, loadmethod); }
/// <summary> /// 真正的请求资源(由不同的资源模块去实现) /// </summary> /// <param name="resname">资源AB名</param> /// <param name="completehandler">加载完成上层回调</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> protected abstract void realRequestResource(string resname, LoadResourceCompleteHandler completehandler, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync);
/// <summary> /// 真正的请求资源 /// </summary> /// <param name="respath">资源AB路径</param> /// <param name="completehandler">加载完成上层回调</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> protected override void realRequestResource(string respath, LoadResourceCompleteHandler completehandler, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { // 如果资源已经加载完成,直接返回 if (mAllLoadedResourceInfoMap[ResourceLoadType.NormalLoad].ContainsKey(respath)) { completehandler(mAllLoadedResourceInfoMap[ResourceLoadType.NormalLoad][respath]); if (loadtype > ResourceLoadType.NormalLoad) { updateLoadedResourceInfoLoadType(respath, ResourceLoadType.NormalLoad, loadtype); } } else if (mAllLoadedResourceInfoMap[ResourceLoadType.Preload].ContainsKey(respath)) { completehandler(mAllLoadedResourceInfoMap[ResourceLoadType.Preload][respath]); if (loadtype > ResourceLoadType.Preload) { updateLoadedResourceInfoLoadType(respath, ResourceLoadType.Preload, loadtype); } } else if (mAllLoadedResourceInfoMap[ResourceLoadType.PermanentLoad].ContainsKey(respath)) { completehandler(mAllLoadedResourceInfoMap[ResourceLoadType.PermanentLoad][respath]); } else { AssetDatabaseLoader adloader = createADLoader(respath); //暂时默认都当同步加载,不支持异步模拟 adloader.LoadMethod = loadmethod; adloader.LoadType = loadtype; adloader.LoadResourceCompleteCallBack = completehandler; adloader.LoadSelfResourceCompleteNotifier = onResourceLoadCompleteNotifier; mResourceRequestTaskMap.Add(respath, adloader); adloader.startLoad(); } }
internal void Reload(Sprite Sprite, ResourceLoadType LoadType) { if (Sprite.internalFrames != null) { Sprite.internalFrames[0].loading = true; } if (LoadType == ResourceLoadType.Delayed) { delayLoader.Enqueue(Sprite); return; } for (int i = 0; i < Sprite.internalFrames.Length; i++) { if (Sprite.internalFrames[i].HasSystemCopy) { SlimDX.Direct3D9.Texture tex = engine.Device.CreateTexture(MakePowerOfTwo(Sprite.internalFrames[i].size.x), MakePowerOfTwo(Sprite.internalFrames[i].size.y)); SlimDX.Direct3D9.SurfaceDescription desc = tex.GetLevelDescription(0); MemoryUsage += desc.Width * desc.Height * 4; Sprite.internalFrames[i].texture = tex; Sprite.internalFrames[i].RestoreFromSystemCopy(); Sprite.internalFrames[i].loading = false; Sprite.internalFrames[i].loaded = true; } } ResourceID id = Sprite.ID; if (id.File == "" || !spriteProcessors.ContainsKey(id.Format)) { return; } ResourceID realid = (string)id; // check replacements if (replacement != null) { ResourceID newid = GetReplacementID(id); if (newid != null) { if (CheckReplacementID(newid)) { id = newid; } } } engine.IncreaseLoadingCount(); ISpriteProcessor loader = GetSpriteProcessor(id, true); if (loader is ISpriteAnimationProcessor) { ISpriteAnimationProcessor loaderAni = (ISpriteAnimationProcessor)loader; loader.Process(id); Log.Debug("reload \"" + id + "\""); for (int i = 0; i < loaderAni.FrameCount; i++) { if (!loaderAni.SetFrame(i)) { SpriteFrame[] frames = new SpriteFrame[i]; for (int j = 0; j < i; j++) { frames[j] = Sprite.Frames[j]; } Sprite.internalFrames = frames; Sprite.ani.frameCount = i; break; } SlimDX.Direct3D9.Texture tex = engine.Device.CreateTexture(MakePowerOfTwo(loaderAni.FrameSize.x), MakePowerOfTwo(loaderAni.FrameSize.y)); SlimDX.Direct3D9.SurfaceDescription desc = tex.GetLevelDescription(0); MemoryUsage += desc.Width * desc.Height * 4; SlimDX.DataRectangle data = tex.LockRectangle(0, SlimDX.Direct3D9.LockFlags.Discard); loader.Render(data.Data, data.Pitch); tex.UnlockRectangle(0); Sprite.Frames[i].Texture = tex; Sprite.Frames[i].Width = loaderAni.FrameSize.x; Sprite.Frames[i].Height = loaderAni.FrameSize.y; Sprite.Frames[i].TimeStamp = System.Diagnostics.Stopwatch.GetTimestamp(); } } else { loader.Process(id); Log.Debug("reload \"" + id + "\""); SlimDX.Direct3D9.Texture tex = engine.Device.CreateTexture(MakePowerOfTwo(loader.Size.x), MakePowerOfTwo(loader.Size.y)); SlimDX.Direct3D9.SurfaceDescription desc = tex.GetLevelDescription(0); MemoryUsage += desc.Width * desc.Height * 4; SlimDX.DataRectangle data = tex.LockRectangle(0, SlimDX.Direct3D9.LockFlags.Discard); loader.Render(data.Data, data.Pitch); tex.UnlockRectangle(0); Sprite.Frame.Texture = tex; Sprite.Frame.Width = loader.Size.x; Sprite.Frame.Height = loader.Size.y; Sprite.Frame.TimeStamp = System.Diagnostics.Stopwatch.GetTimestamp(); } if (Sprite.internalFrames != null) { Sprite.internalFrames[0].loading = false; Sprite.internalFrames[0].loaded = true; } engine.DecreaseLoadingCount(); }
/// <summary> /// 真正执行资源卸载指定类型不再使用的资源接口 /// </summary> /// <param name="resourceloadtype">资源加载类型</param> protected abstract void doUnloadSpecificLoadTypeUnsedResource(ResourceLoadType resourceloadtype);
/// <summary> /// 真正的请求资源 /// </summary> /// <param name="respath">资源AB路径</param> /// <param name="completehandler">加载完成上层回调</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> protected override void realRequestResource(string respath, LoadResourceCompleteHandler completehandler, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { // AB运行时统一转成小写,避免和AB打包那方输出的信息不一致 respath = respath.ToLower(); var abpath = string.Empty; // 因为依赖AB加载也是走统一入口,所以要区分是AB路径还是Asset路径 if (!mAssetBundleBuildInfo.isABPath(respath)) { // AB依赖信息文件和AB打包 abpath = getAssetPathAssetBundleName(respath); } else { abpath = respath; } // 如果资源已经加载完成,直接返回 if (mAllLoadedResourceInfoMap[ResourceLoadType.NormalLoad].ContainsKey(abpath)) { completehandler(mAllLoadedResourceInfoMap[ResourceLoadType.NormalLoad][abpath]); if (loadtype > ResourceLoadType.NormalLoad) { updateLoadedResourceInfoLoadType(abpath, ResourceLoadType.NormalLoad, loadtype); } } else if (mAllLoadedResourceInfoMap[ResourceLoadType.Preload].ContainsKey(abpath)) { completehandler(mAllLoadedResourceInfoMap[ResourceLoadType.Preload][abpath]); if (loadtype > ResourceLoadType.Preload) { updateLoadedResourceInfoLoadType(abpath, ResourceLoadType.Preload, loadtype); } } else if (mAllLoadedResourceInfoMap[ResourceLoadType.PermanentLoad].ContainsKey(abpath)) { completehandler(mAllLoadedResourceInfoMap[ResourceLoadType.PermanentLoad][abpath]); } else { // 确保同一个资源加载的Loader是同一个 // 保证同一个资源加载完成时上层所有加载该资源的回调正确 AssetBundleLoader abloader = null; if (mABRequestTaskMap.ContainsKey(abpath)) { abloader = mABRequestTaskMap[abpath]; // 之前有请求resname资源,但还未完成 // 比如先异步请求resname,在异步完成前来了一个同步请求resname // 修改加载方式并添加回调,调用同步加载方式,异步加载会在同步加载完成时一起回调 abloader.LoadMethod = loadmethod; abloader.LoadType = loadtype; abloader.LoadABCompleteCallBack += completehandler; abloader.LoadSelfABCompleteNotifier = onABLoadCompleteNotifier; if (loadmethod == ResourceLoadMethod.Sync) { ResourceLogger.log(string.Format("请求同步加载一个异步加载状态:{0}的资源 : {1}", abloader.LoadState.ToString(), abloader.AssetBundlePath)); //重置AB加载状态,走同步加载模式 abloader.LoadState = ResourceLoadState.None; abloader.startLoad(); } } else { abloader = createABLoader(abpath); abloader.LoadMethod = loadmethod; abloader.LoadType = loadtype; abloader.LoadABCompleteCallBack = completehandler; abloader.LoadSelfABCompleteNotifier = onABLoadCompleteNotifier; mABRequestTaskMap.Add(abpath, abloader); abloader.startLoad(); } } }
/// <summary> /// 获取一个材质 /// </summary> /// <param name="owner">资源绑定对象</param> /// <param name="respath">资源路径</param> /// <param name="callback">资源回调</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> /// <returns></returns> public void getMaterial(UnityEngine.Object owner, string respath, Action <Material> callback = null, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { ResourceModuleManager.Singleton.requstResource(respath, (abi) => { var assetname = Path.GetFileName(respath); var material = abi.getAsset <Material>(owner, assetname); #if UNITY_EDITOR ResourceUtility.FindMaterialShaderBack(material); #endif callback?.Invoke(material); }, loadtype, loadmethod); }
/// <summary> /// 真正的请求资源 /// </summary> /// <param name="resname">资源AB名</param> /// <param name="completehandler">加载完成上层回调</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> protected override void realRequestResource(string resname, LoadResourceCompleteHandler completehandler, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { // 如果资源已经加载完成,直接返回 if (mAllLoadedResourceInfoMap[ResourceLoadType.NormalLoad].ContainsKey(resname)) { completehandler(mAllLoadedResourceInfoMap[ResourceLoadType.NormalLoad][resname]); if (loadtype > ResourceLoadType.NormalLoad) { updateLoadedResourceInfoLoadType(resname, ResourceLoadType.NormalLoad, loadtype); } } else if (mAllLoadedResourceInfoMap[ResourceLoadType.Preload].ContainsKey(resname)) { completehandler(mAllLoadedResourceInfoMap[ResourceLoadType.Preload][resname]); if (loadtype > ResourceLoadType.Preload) { updateLoadedResourceInfoLoadType(resname, ResourceLoadType.Preload, loadtype); } } else if (mAllLoadedResourceInfoMap[ResourceLoadType.PermanentLoad].ContainsKey(resname)) { completehandler(mAllLoadedResourceInfoMap[ResourceLoadType.PermanentLoad][resname]); } else { // 确保同一个资源加载的Loader是同一个 // 保证同一个资源加载完成时上层所有加载该资源的回调正确 AssetBundleLoader abloader = null; if (mABRequestTaskMap.ContainsKey(resname)) { abloader = mABRequestTaskMap[resname]; // 之前有请求resname资源,但还未完成 // 比如先异步请求resname,在异步完成前来了一个同步请求resname // 修改加载方式并添加回调,调用同步加载方式,异步加载会在同步加载完成时一起回调 abloader.LoadMethod = loadmethod; abloader.LoadType = loadtype; abloader.LoadABCompleteCallBack += completehandler; abloader.LoadSelfABCompleteNotifier = onABLoadCompleteNotifier; if (loadmethod == ResourceLoadMethod.Sync) { ResourceLogger.log(string.Format("请求同步加载一个正在异步加载的资源 : {0}", abloader.AssetBundleName)); //重置AB加载状态,走同步加载模式 abloader.LoadState = ResourceLoadState.None; abloader.startLoad(); } } else { abloader = createABLoader(resname); abloader.LoadMethod = loadmethod; abloader.LoadType = loadtype; abloader.LoadABCompleteCallBack = completehandler; abloader.LoadSelfABCompleteNotifier = onABLoadCompleteNotifier; mABRequestTaskMap.Add(resname, abloader); abloader.startLoad(); } } }
/// <summary> /// 获取一个UI实例资源对象 /// </summary> /// <param name="resname">资源名</param> /// <param name="assetname">Asset名</param> /// <param name="callback">资源回调</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> /// <returns></returns> public void getUIInstance(string resname, string assetname = null, Action <GameObject> callback = null, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { if (string.IsNullOrEmpty(assetname)) { assetname = resname; } ResourceModuleManager.Singleton.requstResource(resname, (abi) => { var uiinstance = abi.instantiateAsset(assetname); #if UNITY_EDITOR ResourceUtility.FindMeshRenderShaderBack(uiinstance); #endif callback?.Invoke(uiinstance); }, loadtype, loadmethod); }
/// <summary> /// 获取一个共享的材质 /// </summary> /// <param name="owner">资源绑定对象</param> /// <param name="resname">资源名</param> /// <param name="assetname">Asset名</param> /// <param name="callback">资源回调</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> /// <returns></returns> public void getShareMaterial(UnityEngine.Object owner, string resname, string assetname = null, Action <Material> callback = null, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { if (string.IsNullOrEmpty(assetname)) { assetname = resname; } ResourceModuleManager.Singleton.requstResource(resname, (abi) => { var material = abi.getAsset <Material>(owner, assetname); #if UNITY_EDITOR ResourceUtility.FindMaterialShaderBack(material); #endif callback?.Invoke(material); }, loadtype, loadmethod); }
/// <summary> /// 请求资源 /// 上层资源加载统一入口 /// </summary> /// <param name="resname">资源AB名</param> /// <param name="completehandler">加载完成上层回调</param> /// <param name="loadtype">资源加载类型</param> /// <param name="loadmethod">资源加载方式</param> public void requstResource(string resname, LoadResourceCompleteHandler completehandler, ResourceLoadType loadtype = ResourceLoadType.NormalLoad, ResourceLoadMethod loadmethod = ResourceLoadMethod.Sync) { // 在白名单里的资源一律以预加载形式加载, // 避免因为上层逻辑错误加载后被频繁加载卸载 if (mResourceWhileListMap.ContainsKey(resname)) { loadtype = ResourceLoadType.Preload; } realRequestResource(resname, completehandler, loadtype, loadmethod); }