import ResHelper from "./ResHelper"; import UIBase from "../ui/base/UIBase"; /** * 资源加载, 针对的是Panel * 首先将资源分为两类 * 一种是在编辑器时将其拖上去图片, 这里将其称为静态图片, * 一种是在代码中使用cc.loader加载的图片, 这里将其称为动态图片 * * 对于静态资源 * 1, 加载 在加载prefab时, cocos会将其依赖的图片一并加载, 所有不需要我们担心 * 2, 释放 这里采用的引用计数的管理方法, 只需要调用destoryPanel即可 */ export default class ResManager { private static instance: ResManager = null; public static get inst() { if(this.instance === null) { this.instance = new ResManager(); } return this.instance; } /** * 采用计数管理的办法, 管理form所依赖的资源 */ private staticDepends:{[key: string]: cc.Asset} = cc.js.createMap(); private dynamicDepends: {[key: string]: cc.Asset} = cc.js.createMap(); private _addTmpStaticDepends(completedCount: number, totalCount: number, item: any) { } /** 加载窗体 */ public async loadPrefab(formName: string) { if(formName == "" || formName == null){ return ; } let pre = await ResHelper.loadResAsync(formName, cc.Prefab, this._addTmpStaticDepends.bind(this)); if(!pre) { cc.warn(`${formName} 资源加载失败, 请确认路径是否正确`); return ; } let node: cc.Node = cc.instantiate(pre); let baseUI = node.getComponent(UIBase); if(baseUI == null) { cc.warn(`${formName} 没有绑定UIBase的Component`); return ; } baseUI.uid = formName; this.staticDepends[formName] = pre if(this.staticDepends[formName]) this.staticDepends[formName].addRef() return node; } /** 销毁窗体 */ public destoryPrefab(com: UIBase) { if(!com) { cc.log("只支持销毁继承了UIBase的窗体!"); return; } if(this.staticDepends[com.uid]) this.staticDepends[com.uid].decRef() com.node.destroy(); } /** 动态资源管理, 通过tag标记当前资源, 统一释放 */ public async loadDynamicRes(url: string, type: typeof cc.Asset) { let sources = await ResHelper.loadResAsync(url, type); if(!this.dynamicDepends[url]) { this.dynamicDepends[url] = sources; } this.dynamicDepends[url].addRef() return sources; } /** 销毁动态资源 没有做引用计数的处理 */ public destoryDynamicRes(url: string) { if(!this.dynamicDepends[url]) { // 销毁 return false; } this.dynamicDepends[url].decRef() return true; } }