LoadRes.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { _decorator, Component, Node, Prefab, Material, assetManager, AudioClip } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. import { config, IdToName } from './CubeConfig';
  4. const m_bundle = [
  5. { name: 'CubePrefab', url: 'Cube', type: 'Prefab', variable: 'cubePrefab' },
  6. { name: 'Materials', url: '', type: 'Material', variable: 'materials' },
  7. { name: 'GUI', url: 'MainCanvas', type: 'Prefab', variable: 'mainCanvas' },
  8. { name: 'GUI', url: 'UI_Show', type: 'Prefab', variable: 'ui_Show' },
  9. { name: 'GUI', url: 'SettingsPanel', type: 'Prefab', variable: 'settingsPanel' },
  10. { name: 'GUI', url: 'RankPanel', type: 'Prefab', variable: 'rankPanel' },
  11. { name: 'GUI', url: 'Item', type: 'Prefab', variable: 'item' },
  12. { name: 'GUI', url: 'InforPanel', type: 'Prefab', variable: 'inforPanel' },
  13. { name: 'GUI', url: 'AdPanel', type: 'Prefab', variable: 'adPanel' },
  14. { name: 'GUI', url: 'ResultPanel', type: 'Prefab', variable: 'resultPanel' },
  15. { name: 'GUI', url: 'SignPanel', type: 'Prefab', variable: 'signPanel' },
  16. { name: 'Container', url: 'Container', type: 'Prefab', variable: 'container' },
  17. { name: 'Audio', url: '', type: 'AudioClip', variable: '' },
  18. ]
  19. @ccclass('LoadRes')
  20. export class LoadRes {
  21. private static instance: LoadRes = null;
  22. private resourcesLoadedCount: number = 0;
  23. private onResourceLoaded: () => void = null;
  24. @property(Prefab)
  25. public cubePrefab: Prefab = null;// 方块
  26. @property(Material)
  27. public materials: Material[] = [];// 材质
  28. @property(Prefab)
  29. public container: Prefab = null;// 容器
  30. @property(Prefab)
  31. public mainCanvas: Prefab = null;// 主界面
  32. @property(Prefab)
  33. public settingsPanel: Prefab = null;// 设置界面
  34. @property(Prefab)
  35. public rankPanel: Prefab = null;// 排行榜界面
  36. @property(Prefab)
  37. public inforPanel: Prefab = null;// 个人信息界面
  38. @property(Prefab)
  39. public ui_Show: Prefab = null;// UI界面
  40. @property(Prefab)
  41. public adPanel: Prefab = null;//广告界面
  42. @property(Prefab)
  43. public resultPanel: Prefab = null;//结算界面
  44. @property(Prefab)
  45. public signPanel: Prefab = null;
  46. @property(Prefab)
  47. public item: Prefab = null;
  48. private constructor() {
  49. // 私有化构造函数以防止外部实例化
  50. }
  51. public static getInstance(): LoadRes {
  52. if (LoadRes.instance === null) {
  53. LoadRes.instance = new LoadRes();
  54. }
  55. return LoadRes.instance;
  56. }
  57. setOnResourceLoaded(callback: () => void) {
  58. this.onResourceLoaded = callback;
  59. }
  60. StartLoadRes() {
  61. for (let i = 0; i < m_bundle.length; i++) {
  62. if (m_bundle[i].type === 'Prefab') {
  63. this.preloadPrefab(i);
  64. }
  65. else if (m_bundle[i].type === 'Material') {
  66. this.preloadMaterial(i);
  67. }
  68. else if (m_bundle[i].type === 'AudioClip') {
  69. this.preloadAudio(i);
  70. }
  71. else if (m_bundle[i].type === 'Font') {
  72. this.preloadFont(i);
  73. }
  74. }
  75. }
  76. update(deltaTime: number) {
  77. }
  78. preloadPrefab(index: number) {
  79. assetManager.loadBundle(m_bundle[index].name, (err, bundle) => {
  80. bundle.load(m_bundle[index].url, Prefab, (err, prefab) => {
  81. if (err) {
  82. console.error(err);
  83. return;
  84. }
  85. this[m_bundle[index].variable] = prefab;
  86. if (this.onResourceLoaded) {
  87. this.onResourceLoaded();
  88. }
  89. this.checkAllResourcesLoaded();
  90. });
  91. });
  92. }
  93. preloadMaterial(index: number) {
  94. const materialPromises: Promise<void>[] = [];
  95. for (let i = 0; i < 5; i++) {
  96. materialPromises.push(new Promise<void>((resolve, reject) => {
  97. assetManager.loadBundle(m_bundle[index].name, (err, bundle) => {
  98. if (err) {
  99. console.error(err);
  100. return;
  101. }
  102. bundle.load(config[IdToName[i]].effectName, Material, (err, material) => {
  103. if (err) {
  104. console.error(err);
  105. reject(err);
  106. } else {
  107. this[m_bundle[index].variable][i] = material;
  108. resolve();
  109. }
  110. });
  111. });
  112. }));
  113. }
  114. Promise.all(materialPromises).then(() => {
  115. this[m_bundle[index].variable].forEach((material, index) => {
  116. material.initialize({
  117. effectName: config[IdToName[index]].effectName,
  118. defines: {
  119. USE_RGBE_CUBEMAP: true
  120. }
  121. });
  122. if (this.onResourceLoaded) {
  123. this.onResourceLoaded();
  124. }
  125. });
  126. this.checkAllResourcesLoaded();
  127. }).catch(error => {
  128. console.error('Error loading materials:', error);
  129. });
  130. }
  131. preloadAudio(index: number) {
  132. assetManager.loadBundle(m_bundle[index].name, (err, bundle) => {
  133. if (this.onResourceLoaded) {
  134. this.onResourceLoaded();
  135. }
  136. this.checkAllResourcesLoaded();
  137. });
  138. }
  139. preloadFont(index: number) {
  140. assetManager.loadBundle(m_bundle[index].name, (err, bundle) => {
  141. if (this.onResourceLoaded) {
  142. this.onResourceLoaded();
  143. }
  144. this.checkAllResourcesLoaded();
  145. });
  146. }
  147. private checkAllResourcesLoaded(): void {
  148. this.resourcesLoadedCount++;
  149. if (this.resourcesLoadedCount === 4) {
  150. //资源都加载好了
  151. }
  152. }
  153. }