AssetBundleInitiator.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { EventDispatcher } from "../events/EventDispatcher";
  2. import IInitiator from "./IInitiator";
  3. import InitiatorEvent from "./InitiatorEvent";
  4. /**
  5. * 资源包初始化
  6. */
  7. export default class AssetBundleInitiator extends EventDispatcher implements IInitiator{
  8. private __assetBundles:string[];
  9. private __assetBundleTotal:number=0;
  10. constructor(assetBundles:string[]){
  11. super();
  12. this.__assetBundles=assetBundles;
  13. }
  14. /**
  15. * 名称
  16. */
  17. GetName(): string {
  18. return "AssetBundleInitiator";
  19. }
  20. Start(): void {
  21. if(this.__assetBundles==null||this.__assetBundles.length==0){
  22. this.DispatchEvent(InitiatorEvent.EVENT_COMPLETE);
  23. }else{
  24. this.__assetBundleTotal=this.__assetBundles.length;
  25. this.TryNextAssetBundle();
  26. }
  27. }
  28. private TryNextAssetBundle():void{
  29. if(this.__assetBundles.length>0){
  30. let assetBundleName:string=this.__assetBundles.shift();
  31. cc.assetManager.loadBundle(assetBundleName,null,this.LoadAssetBundleHandler.bind(this));
  32. }else{
  33. this.DispatchEvent(InitiatorEvent.EVENT_COMPLETE);
  34. }
  35. }
  36. private LoadAssetBundleHandler(err: Error, bundle: cc.AssetManager.Bundle):void{
  37. if(err){
  38. console.error(err);
  39. return;
  40. }
  41. let progresss:number=(this.__assetBundleTotal-this.__assetBundles.length)/this.__assetBundleTotal;
  42. this.DispatchEvent(InitiatorEvent.EVENT_PROGRESS,progresss)
  43. this.TryNextAssetBundle();
  44. }
  45. Destroy(): void {
  46. this.__assetBundles=null;
  47. this.__assetBundleTotal=0;
  48. }
  49. }