UILoading.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Widget, _decorator, ProgressBar, math, macro } from 'cc';
  2. import { UIBase } from '../scriptBase/UIBase';
  3. import { Bundle } from '../enum/Bundle';
  4. import { ResMgr } from '../manager/ResMgr';
  5. import { UI } from '../enum/UI';
  6. import { Setting } from '../Setting';
  7. import { ItemMgr } from '../manager/ItemMgr';
  8. import { AdMgr } from '../manager/AdMgr';
  9. const { ccclass, property, requireComponent } = _decorator;
  10. @ccclass('UI/UILoading')
  11. @requireComponent(Widget)
  12. export class UILoading extends UIBase {
  13. private progressBar: ProgressBar = null
  14. private loaded: boolean = false
  15. protected async onLoad() {
  16. this.progressBar = this.getComponentInChildren(ProgressBar)
  17. }
  18. protected async start() {
  19. this.schedule(this.updateProgress, 0.25, macro.REPEAT_FOREVER)
  20. await this.loadBundle()
  21. await this.loadRes()
  22. this.loaded = true
  23. }
  24. private init() {
  25. Setting.init()
  26. ItemMgr.init()
  27. AdMgr.init()
  28. }
  29. public onOpen(data?: any): void {
  30. }
  31. public onClose(data?: any): void {
  32. }
  33. private loadBundle() {
  34. return Promise.all([
  35. ResMgr.loadBundle(Bundle.Icon),
  36. ResMgr.loadBundle(Bundle.Audio),
  37. ResMgr.loadBundle(Bundle.Game),
  38. ResMgr.loadBundle(Bundle.UI),
  39. ])
  40. }
  41. private loadRes() {
  42. return Promise.all([
  43. ResMgr.loadDir(Bundle.Icon),
  44. ResMgr.loadDir(Bundle.Audio),
  45. ResMgr.loadDir(Bundle.Game),
  46. ResMgr.loadDir(Bundle.UI),
  47. ])
  48. }
  49. protected updateProgress(): void {
  50. let progressAdd: number = math.randomRange(0.1, 0.3)
  51. this.progressBar.progress = Math.min(this.progressBar.progress + progressAdd, 0.9)
  52. if (this.loaded) {
  53. this.progressBar.progress = 1.0
  54. this.unschedule(this.updateProgress)
  55. this.onLoadCompleted()
  56. }
  57. }
  58. private onLoadCompleted(): void {
  59. this.init()
  60. this.scheduleOnce(() => {
  61. this.close()
  62. this.open(UI.Main)
  63. }, 0.1)
  64. }
  65. }