start.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { _decorator, game, Label, Prefab, Node, Slider, ProgressBar, director, Sprite, SpriteFrame } from 'cc';
  2. import { ModuleDef } from '../Script/ModuleDef';
  3. import { SceneDef } from '../Script/SceneDef';
  4. import { ch_start_pack } from '../ch/start/ch_start_pack';
  5. import { gui } from '../core/ui/ui';
  6. import { ResUtil } from '../core/util/ResUtil'
  7. import { ch } from '../ch/ch';
  8. const { ccclass, property } = _decorator;
  9. //预加载模块
  10. const _preloadBundles = [ModuleDef.EXTRA,ModuleDef.BASIC,ModuleDef.GAME];
  11. //预加载资源
  12. const _preloadRes = [
  13. // { bundle: ModuleDef.EXTRA, url: 'ui/loading' },
  14. { bundle: ModuleDef.BASIC, url: 'ui_alert/UI_Alert' },
  15. { bundle: ModuleDef.BASIC, url: 'ui_waiting/UI_Waiting' },
  16. { bundle: ModuleDef.BASIC, url: 'ui_notify/UI_Notify' },
  17. { bundle: ModuleDef.GAME, url: 'ui/main/main' },
  18. { bundle: ModuleDef.GAME, url: 'ui/UI_Hall/Hall' },
  19. ];
  20. //
  21. const _loadingText = ['Loading.', 'Loading..', 'Loading...'];
  22. const _totalNum = _preloadBundles.length + _preloadRes.length + 1;
  23. @ccclass('Start')
  24. export class Start extends ch_start_pack {
  25. //skinManager = new SkinManager();
  26. @property(ProgressBar)
  27. loadingBar: ProgressBar;
  28. private _percent: string = '1';
  29. private _numCurrentLoaded = 0;
  30. public static packId: number;
  31. @property({ type: Prefab, displayName: 'ui初始预制体' })
  32. private ui_prefab: Prefab;
  33. isGameScenePreloaded = false;
  34. protected onLoad(): void {
  35. Start.packId = this.pid
  36. }
  37. async start() {
  38. console.log('Start');
  39. gui.init(this.ui_prefab);
  40. game.frameRate = 61;
  41. await this.try_init(3);
  42. this.preloadBundle(0);
  43. }
  44. onResLoaded() {
  45. this._numCurrentLoaded++;
  46. console.log(this._numCurrentLoaded);
  47. this._percent = ~~(this._numCurrentLoaded / _totalNum * 100) + '%';
  48. console.log(this._percent);
  49. }
  50. async preloadBundle(idx: number) {
  51. await ResUtil.loadBundle(_preloadBundles[idx])
  52. ch.log.info('module:<' + _preloadBundles[idx] + '>loaded.');
  53. idx++;
  54. this.onResLoaded();
  55. if (idx < _preloadBundles.length) {
  56. this.preloadBundle(idx);
  57. }
  58. else {
  59. this.preloadRes(0);
  60. }
  61. }
  62. async preloadRes(idx: number) {
  63. let res = _preloadRes[idx];
  64. await ResUtil.loadAsset<Prefab>(res.url, res.bundle);
  65. idx++;
  66. this.onResLoaded();
  67. if (idx < _preloadRes.length) {
  68. this.preloadRes(idx);
  69. } else {
  70. this.onPreloadingComplete();
  71. }
  72. }
  73. async onPreloadingComplete() {
  74. await ResUtil.loadScene(SceneDef.Hall, ModuleDef.GAME, true);
  75. console.log('jinru hall');
  76. this.onResLoaded();
  77. }
  78. update(deltaTime: number) {
  79. if (this._percent) {
  80. // this.txtLoading.string = 'Loading...' + this._percent;
  81. const sc = this._numCurrentLoaded / _totalNum;
  82. if (sc >= 1) {
  83. this.loadingBar.progress = sc;
  84. } else {
  85. this.loadingBar.progress = this._numCurrentLoaded / _totalNum;
  86. }
  87. }
  88. }
  89. }