LaunchScreen.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { _decorator, Component, Node, director, assetManager, ProgressBar, Label, Sprite, SpriteFrame, resources } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('LaunchScreen')
  4. export class LaunchScreen extends Component {
  5. @property(ProgressBar)
  6. progressBar: ProgressBar = null;
  7. @property(Label)
  8. progressLabel: Label = null;
  9. @property(Sprite)
  10. backgroundSprite: Sprite = null;
  11. @property(SpriteFrame)
  12. launchBackground: SpriteFrame = null;
  13. private totalAssets = 0;
  14. private loadedAssets = 0;
  15. start() {
  16. console.log('[LaunchScreen] 启动页开始初始化');
  17. // 设置启动页背景
  18. this.setupBackground();
  19. // 开始预加载游戏资源
  20. this.preloadGameAssets();
  21. }
  22. /**
  23. * 设置启动页背景
  24. */
  25. private setupBackground() {
  26. if (this.backgroundSprite && this.launchBackground) {
  27. this.backgroundSprite.spriteFrame = this.launchBackground;
  28. console.log('[LaunchScreen] 启动页背景设置完成');
  29. }
  30. }
  31. /**
  32. * 预加载游戏资源
  33. */
  34. private async preloadGameAssets() {
  35. try {
  36. console.log('[LaunchScreen] 开始预加载游戏资源');
  37. // 更新进度显示
  38. this.updateProgress(0, '正在加载游戏资源...');
  39. // 预加载game-level分包
  40. await this.loadSubpackage('game-level');
  41. // 预加载完成,跳转到游戏场景
  42. this.loadGameScene();
  43. } catch (error) {
  44. console.error('[LaunchScreen] 资源预加载失败:', error);
  45. this.updateProgress(0, '资源加载失败,请重试');
  46. }
  47. }
  48. /**
  49. * 加载分包
  50. */
  51. private loadSubpackage(bundleName: string): Promise<void> {
  52. return new Promise((resolve, reject) => {
  53. console.log(`[LaunchScreen] 开始加载分包: ${bundleName}`);
  54. // 检查是否在微信小游戏环境
  55. if (typeof wx !== 'undefined' && wx && wx.loadSubpackage) {
  56. // 微信小游戏分包加载
  57. const loadTask = wx.loadSubpackage({
  58. name: bundleName,
  59. success: () => {
  60. console.log(`[LaunchScreen] 微信分包 ${bundleName} 加载成功`);
  61. // 分包加载成功后,再加载bundle
  62. this.loadBundleAfterSubpackage(bundleName, resolve, reject);
  63. },
  64. fail: (err) => {
  65. console.error(`[LaunchScreen] 微信分包 ${bundleName} 加载失败:`, err);
  66. // 微信分包加载失败时,尝试直接加载bundle作为降级方案
  67. console.log(`[LaunchScreen] 尝试直接加载bundle作为降级方案`);
  68. this.loadBundleAfterSubpackage(bundleName, resolve, reject);
  69. }
  70. });
  71. // 监听分包加载进度
  72. if (loadTask && loadTask.onProgressUpdate) {
  73. loadTask.onProgressUpdate((res) => {
  74. const progress = res.progress || 0;
  75. this.updateProgress(progress, `正在下载分包... ${progress}%`);
  76. });
  77. }
  78. } else {
  79. // 非微信环境,直接加载bundle
  80. console.log(`[LaunchScreen] 非微信环境,直接加载bundle`);
  81. this.loadBundleAfterSubpackage(bundleName, resolve, reject);
  82. }
  83. });
  84. }
  85. /**
  86. * 分包下载完成后加载bundle
  87. */
  88. private loadBundleAfterSubpackage(bundleName: string, resolve: Function, reject: Function) {
  89. assetManager.loadBundle(bundleName, (err, bundle) => {
  90. if (err) {
  91. console.error(`[LaunchScreen] Bundle ${bundleName} 加载失败:`, err);
  92. // 如果bundle加载失败,尝试跳过分包直接进入主场景
  93. console.log(`[LaunchScreen] Bundle加载失败,尝试跳过分包加载`);
  94. this.skipSubpackageAndLoadMainScene(resolve, reject);
  95. return;
  96. }
  97. console.log(`[LaunchScreen] Bundle ${bundleName} 加载成功`);
  98. // 预加载分包中的场景
  99. bundle.preloadScene('GameLevel', (finished: number, total: number) => {
  100. const progress = finished / total;
  101. this.updateProgress(progress * 100, `正在加载游戏场景... ${Math.floor(progress * 100)}%`);
  102. }, (err) => {
  103. if (err) {
  104. console.error('[LaunchScreen] GameLevel场景预加载失败:', err);
  105. // 场景预加载失败时,也尝试跳过分包
  106. this.skipSubpackageAndLoadMainScene(resolve, reject);
  107. return;
  108. }
  109. console.log('[LaunchScreen] GameLevel场景预加载完成');
  110. this.updateProgress(100, '加载完成!');
  111. resolve();
  112. });
  113. });
  114. }
  115. /**
  116. * 更新加载进度
  117. */
  118. private updateProgress(progress: number, message: string) {
  119. if (this.progressBar) {
  120. this.progressBar.progress = progress / 100;
  121. }
  122. if (this.progressLabel) {
  123. this.progressLabel.string = message;
  124. }
  125. console.log(`[LaunchScreen] ${message} (${progress.toFixed(1)}%)`);
  126. }
  127. /**
  128. * 跳过分包加载,直接进入主场景的降级方案
  129. */
  130. private skipSubpackageAndLoadMainScene(resolve: Function, reject: Function) {
  131. console.log('[LaunchScreen] 执行降级方案:跳过分包,尝试加载主场景');
  132. // 尝试加载主场景(如果存在的话)
  133. director.loadScene('First', (err) => {
  134. if (err) {
  135. console.error('[LaunchScreen] 主场景加载也失败:', err);
  136. this.updateProgress(0, '游戏加载失败,请刷新重试');
  137. reject(err);
  138. } else {
  139. console.log('[LaunchScreen] 成功加载主场景作为降级方案');
  140. this.updateProgress(100, '加载完成(降级模式)');
  141. resolve();
  142. }
  143. });
  144. }
  145. /**
  146. * 跳转到游戏场景
  147. */
  148. private loadGameScene() {
  149. console.log('[LaunchScreen] 准备跳转到GameLevel场景');
  150. // 延迟一秒后跳转,让用户看到加载完成的提示
  151. this.scheduleOnce(() => {
  152. // 使用分包中的场景
  153. const bundle = assetManager.getBundle('game-level');
  154. if (bundle) {
  155. bundle.loadScene('GameLevel', (err, scene) => {
  156. if (err) {
  157. console.error('[LaunchScreen] GameLevel场景加载失败:', err);
  158. // 如果分包场景加载失败,尝试加载主场景
  159. this.loadFallbackScene();
  160. return;
  161. }
  162. console.log('[LaunchScreen] 正在切换到GameLevel场景');
  163. director.runScene(scene);
  164. });
  165. } else {
  166. console.error('[LaunchScreen] game-level分包未找到,尝试加载主场景');
  167. this.loadFallbackScene();
  168. }
  169. }, 1.0);
  170. }
  171. /**
  172. * 加载降级场景
  173. */
  174. private loadFallbackScene() {
  175. console.log('[LaunchScreen] 尝试加载降级场景');
  176. director.loadScene('First', (err) => {
  177. if (err) {
  178. console.error('[LaunchScreen] 降级场景加载失败:', err);
  179. this.updateProgress(0, '游戏启动失败,请刷新页面重试');
  180. } else {
  181. console.log('[LaunchScreen] 成功加载降级场景');
  182. }
  183. });
  184. }
  185. }