JLoader.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { EventDispatcher } from "../events/EventDispatcher";
  2. export default class JLoader extends EventDispatcher
  3. {
  4. public static EVENT_PROGRESS:string="JLoader.EventProgress";
  5. public static EVENT_ERROR:string="JLoader.EventError";
  6. public static EVENT_COMPLETE:string="JLoader.EventComplete";
  7. private packageName:string;
  8. private assetPath:string;
  9. private bundle:cc.AssetManager.Bundle;
  10. constructor(){
  11. super();
  12. }
  13. Load(packageName:string,assetPath:string):void{
  14. this.packageName=packageName;
  15. this.assetPath=assetPath;
  16. this.bundle=null;
  17. this.bundle=cc.assetManager.getBundle(this.packageName);
  18. if(this.bundle!=null){
  19. this.LoadAsset();
  20. return;
  21. }
  22. cc.assetManager.loadBundle(this.packageName,this.AssetBundleLoaded.bind(this));
  23. }
  24. GetContent(type:typeof cc.Asset):cc.Asset{
  25. return this.bundle.get(this.assetPath,type);
  26. }
  27. private AssetBundleLoaded(err:Error,bundle:cc.AssetManager.Bundle):void{
  28. if(err){
  29. cc.error(err);
  30. this.DispatchEvent(JLoader.EVENT_ERROR,err);
  31. }
  32. this.bundle=bundle;
  33. this.LoadAsset();
  34. }
  35. private LoadAsset():void{
  36. this.bundle.load(this.assetPath,this.AssetLoaded.bind(this));
  37. }
  38. private AssetLoaded(err:Error,asset:cc.Asset):void{
  39. if(err){
  40. cc.error(err);
  41. this.DispatchEvent(JLoader.EVENT_ERROR,err);
  42. }
  43. this.DispatchEvent(JLoader.EVENT_COMPLETE);
  44. }
  45. }