UILoading.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. console.log('zh:uiloading.ts onload')
  17. this.progressBar = this.getComponentInChildren(ProgressBar)
  18. }
  19. protected async start() {
  20. this.schedule(this.updateProgress, 0.25, macro.REPEAT_FOREVER)
  21. await this.loadBundle()
  22. await this.loadRes()
  23. this.loaded = true
  24. }
  25. private init() {
  26. Setting.init()
  27. ItemMgr.init()
  28. AdMgr.init()
  29. }
  30. public onOpen(data?: any): void {
  31. }
  32. public onClose(data?: any): void {
  33. }
  34. private loadBundle() {
  35. return Promise.all([
  36. ResMgr.loadBundle(Bundle.Icon),
  37. ResMgr.loadBundle(Bundle.Audio),
  38. ResMgr.loadBundle(Bundle.Game),
  39. ResMgr.loadBundle(Bundle.UI),
  40. ])
  41. }
  42. private loadRes() {
  43. return Promise.all([
  44. ResMgr.loadDir(Bundle.Icon),
  45. ResMgr.loadDir(Bundle.Audio),
  46. ResMgr.loadDir(Bundle.Game),
  47. ResMgr.loadDir(Bundle.UI),
  48. ])
  49. }
  50. protected updateProgress(): void {
  51. let progressAdd: number = math.randomRange(0.1, 0.3)
  52. this.progressBar.progress = Math.min(this.progressBar.progress + progressAdd, 0.9)
  53. if (this.loaded) {
  54. this.progressBar.progress = 1.0
  55. this.unschedule(this.updateProgress)
  56. this.onLoadCompleted()
  57. }
  58. }
  59. private onLoadCompleted(): void {
  60. this.init()
  61. this.scheduleOnce(() => {
  62. this.close()
  63. this.open(UI.Main)
  64. }, 0.1)
  65. }
  66. }