import { EventDispatcher } from "../events/EventDispatcher"; import IInitiator from "./IInitiator"; import InitiatorEvent from "./InitiatorEvent"; /** * 资源包初始化 */ export default class AssetBundleInitiator extends EventDispatcher implements IInitiator{ private __assetBundles:string[]; private __assetBundleTotal:number=0; constructor(assetBundles:string[]){ super(); this.__assetBundles=assetBundles; } /** * 名称 */ GetName(): string { return "AssetBundleInitiator"; } Start(): void { if(this.__assetBundles==null||this.__assetBundles.length==0){ this.DispatchEvent(InitiatorEvent.EVENT_COMPLETE); }else{ this.__assetBundleTotal=this.__assetBundles.length; this.TryNextAssetBundle(); } } private TryNextAssetBundle():void{ if(this.__assetBundles.length>0){ let assetBundleName:string=this.__assetBundles.shift(); cc.assetManager.loadBundle(assetBundleName,null,this.LoadAssetBundleHandler.bind(this)); }else{ this.DispatchEvent(InitiatorEvent.EVENT_COMPLETE); } } private LoadAssetBundleHandler(err: Error, bundle: cc.AssetManager.Bundle):void{ if(err){ console.error(err); return; } let progresss:number=(this.__assetBundleTotal-this.__assetBundles.length)/this.__assetBundleTotal; this.DispatchEvent(InitiatorEvent.EVENT_PROGRESS,progresss) this.TryNextAssetBundle(); } Destroy(): void { this.__assetBundles=null; this.__assetBundleTotal=0; } }