import { _decorator, assetManager, AssetManager, director, instantiate, Label, Node, Prefab, sp, Sprite, SpriteFrame, tween, Vec3 } from 'cc'; import { BundleName } from '../Config/EnumCfg'; const { ccclass, property } = _decorator; @ccclass('LayerMgr') export class LayerMgr { private static _layerMgr: LayerMgr = null; private _dialogList: any = null; public static get instance(): LayerMgr { if (this._layerMgr == null) { this._layerMgr = new LayerMgr() } return this._layerMgr; } private constructor() { this._dialogList = {} } loadBundle(bundleName: string, callback?: Function) { if (!bundleName) return; let bundle: AssetManager.Bundle = this.getBundle(bundleName) if (bundle) { // 关卡包先预加载动画和声音资源 if (bundleName.indexOf("Unit") > -1) { this.preloadDir(bundle, "Animation") this.preloadDir(bundle, "Audio") } if (callback) callback(bundle) return; } AssetManager.instance.loadBundle(bundleName, AssetManager.Bundle, (err, res) => { if (err) { console.log(`load ${bundleName} bundle err: `, err) return; } console.log(`load ${bundleName} complete`, bundleName.indexOf("Unit")) // 关卡包先预加载动画和声音资源 if (bundleName.indexOf("Unit") > -1) { this.preloadDir(res, "Animation") this.preloadDir(res, "Audio") } if (callback) callback(res) }) } changeScene(bundleName: string, sceneName: string, callback?: any) { let bundle: AssetManager.Bundle = this.getBundle(bundleName) if (bundle) { this.loadScene(bundle, sceneName, (res) => { director.loadScene(sceneName, callback); }); } else { this.loadBundle(bundleName, (res) => { this.loadScene(res, sceneName, (scene) => { director.loadScene(sceneName, callback); }) }) } } preloadDir(bundle: AssetManager.Bundle, dir: string) { console.log("start preloaddir: ", bundle.name, dir) if (!bundle || !dir) return bundle.preloadDir(dir, (err, res) => { if (err) console.log("preloaddir err") else console.log("preloaddir complete: ", bundle.name, dir) }) } private loadScene(bundle: AssetManager.Bundle, sceneName: string, callback?: Function) { if (!bundle || !sceneName) return; bundle.loadScene(sceneName, (err, res) => { if (err) { console.error(`load ${sceneName} scene err: `) return; } if (callback) callback(res) }) } showMsgTips(str: string, time?) { let msgTips = null; let bundle: AssetManager.Bundle = this.getBundle(BundleName.Res) if (!bundle) return; bundle.load("Prefab/msgTips", Prefab, (err, res) => { if (err) { console.error("load msgTips err", err); return; } msgTips = instantiate(res); msgTips.getChildByName("label").getComponent(Label).string = str // msgTips.scale = 1; director.getScene().getChildByName("Canvas").addChild(msgTips); console.log("show msgTips: ", str) msgTips.scale = Vec3.ZERO tween(msgTips).to(0.3, { scale: Vec3.ONE }, { easing: "sineIn" }).delay(1).to(0.3, { opacity: 0 }, { easing: "fade" }).delay(time ? time : 0).call(() => { msgTips.removeFromParent() msgTips.destroy() }).start() }) } ShowPrefab(bundleName: BundleName, prefab: string, call?: Function) { let bundle: AssetManager.Bundle = this.getBundle(bundleName) if (bundle) { bundle.load(prefab, Prefab, (err, res) => { if (err) { console.log(`load ${prefab} err: `, bundleName); return } let node: Node = instantiate(res as Prefab); director.getScene().getChildByName('Canvas').addChild(node); if (call) call(node) }) } } ShowPrefab2(bundleName: BundleName, prefab: string, call?: Function) { let bundle: AssetManager.Bundle = this.getBundle(bundleName) if (bundle) { bundle.load(prefab, Prefab, (err, res) => { if (err) { console.log(`load ${prefab} err: `, bundleName); return } let node: Node = instantiate(res as Prefab); director.getScene().getChildByName('Canvas').addChild(node); if (call) call(node) }) } } ShowSpine(bundlename: string, url: string, spine: sp.Skeleton, cb: Function = null) { let subres = assetManager.getBundle(bundlename) if (subres) { subres.load(url, sp.SkeletonData, (error: Error, sp: sp.SkeletonData) => { if (sp != null) { if (cb) { spine.skeletonData = sp cb(sp); } else { spine.skeletonData = sp } } else { alert("err:" + error); } }) } } ShowSprite(bundleName: BundleName | string, spritepath: string, changeSpriteFrameNode: Node, call?: Function) { let bundle: AssetManager.Bundle = this.getBundle(bundleName) if (bundle) { bundle.load(spritepath + "/spriteFrame", SpriteFrame, (err, res) => { if (err) { console.error(err) console.log(`load ${spritepath} err: `, bundleName); return } let spr: SpriteFrame = (res as SpriteFrame); changeSpriteFrameNode.getComponent(Sprite).spriteFrame = spr; call && call(spr) }) } } ShowSprite2(bundleName: BundleName | string, spritepath: string, spriter: Sprite, call?: Function) { let bundle: AssetManager.Bundle = this.getBundle(bundleName) if (bundle) { bundle.load(spritepath + "/spriteFrame", SpriteFrame, (err, res) => { if (err) { console.error(err) console.log(`load ${spritepath} err: `, bundleName); return } let spr: SpriteFrame = (res as SpriteFrame); spriter.spriteFrame = spr; call && call(spr) }) } } getBundle(bundleName: string): AssetManager.Bundle { if (bundleName == BundleName.Res) return assetManager.resources return assetManager.getBundle(bundleName) } }