LoadingViewComp.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * @Author: dgflash
  3. * @Date: 2021-07-03 16:13:17
  4. * @LastEditors: bansomin
  5. * @LastEditTime: 2024-03-31 01:17:02
  6. */
  7. import { _decorator } from "cc";
  8. import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
  9. import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
  10. import { CCVMParentComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCVMParentComp";
  11. import { ModuleUtil } from "../../../../../extensions/oops-plugin-framework/assets/module/common/ModuleUtil";
  12. import { DemoViewComp } from "../../account/view/DemoViewComp";
  13. import { smc } from "../../common/SingletonModuleComp";
  14. import { UIID } from "../../common/config/GameUIConfig";
  15. const { ccclass, property } = _decorator;
  16. /** 游戏资源加载 */
  17. @ccclass('LoadingViewComp')
  18. @ecs.register('LoadingView', false)
  19. export class LoadingViewComp extends CCVMParentComp {
  20. /** VM 组件绑定数据 */
  21. data: any = {
  22. /** 加载资源当前进度 */
  23. finished: 0,
  24. /** 加载资源最大进度 */
  25. total: 0,
  26. /** 加载资源进度比例值 */
  27. progress: "0",
  28. /** 加载流程中提示文本 */
  29. prompt: ""
  30. };
  31. private progress: number = 0;
  32. start() {
  33. this.enter();
  34. }
  35. enter() {
  36. this.loadRes();
  37. }
  38. /** 加载资源 */
  39. private async loadRes() {
  40. this.data.progress = 0;
  41. await this.loadCustom();
  42. this.loadGameRes();
  43. }
  44. /** 加载游戏本地JSON数据(自定义内容) */
  45. private loadCustom() {
  46. // 加载游戏本地JSON数据的多语言提示文本
  47. this.data.prompt = oops.language.getLangByID("loading_load_json");
  48. }
  49. /** 加载初始游戏内容资源 */
  50. private loadGameRes() {
  51. // 加载初始游戏内容资源的多语言提示文本
  52. this.data.prompt = oops.language.getLangByID("loading_load_game");
  53. oops.res.loadDir("game", this.onProgressCallback.bind(this), this.onCompleteCallback.bind(this));
  54. }
  55. /** 加载进度事件 */
  56. private onProgressCallback(finished: number, total: number, item: any) {
  57. this.data.finished = finished;
  58. this.data.total = total;
  59. var progress = finished / total;
  60. if (progress > this.progress) {
  61. this.progress = progress;
  62. this.data.progress = (progress * 100).toFixed(2);
  63. }
  64. }
  65. /** 加载完成事件 */
  66. private async onCompleteCallback() {
  67. // 获取用户信息的多语言提示文本
  68. this.data.prompt = oops.language.getLangByID("loading_load_player");
  69. await ModuleUtil.addViewUiAsync(smc.account, DemoViewComp, UIID.Demo);
  70. ModuleUtil.removeViewUi(this.ent, LoadingViewComp, UIID.Loading);
  71. }
  72. reset(): void { }
  73. }