| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- import { _decorator, Component, Node, director, assetManager, ProgressBar, Label, Sprite, SpriteFrame, resources } from 'cc';
- const { ccclass, property } = _decorator;
- // 微信小游戏API类型声明
- declare global {
- interface Window {
- wx?: {
- loadSubpackage?: (options: {
- name: string;
- success?: () => void;
- fail?: (err: any) => void;
- }) => any;
- };
- }
- const wx: {
- loadSubpackage?: (options: {
- name: string;
- success?: () => void;
- fail?: (err: any) => void;
- }) => any;
- } | undefined;
- }
- @ccclass('LaunchScreen')
- export class LaunchScreen extends Component {
- @property(ProgressBar)
- progressBar: ProgressBar = null;
- @property(Label)
- progressLabel: Label = null;
- @property(Sprite)
- backgroundSprite: Sprite = null;
- @property(SpriteFrame)
- launchBackground: SpriteFrame = null;
- private totalAssets = 0;
- private loadedAssets = 0;
- start() {
- console.log('[LaunchScreen] 启动页开始初始化');
-
- // 设置启动页背景
- this.setupBackground();
-
- // 开始预加载游戏资源
- this.preloadGameAssets();
- }
- /**
- * 设置启动页背景
- */
- private setupBackground() {
- if (this.backgroundSprite && this.launchBackground) {
- this.backgroundSprite.spriteFrame = this.launchBackground;
- console.log('[LaunchScreen] 启动页背景设置完成');
- }
- }
- /**
- * 预加载游戏资源
- */
- private async preloadGameAssets() {
- try {
- console.log('[LaunchScreen] 开始预加载游戏资源');
-
- // 更新进度显示
- this.updateProgress(0, '正在加载游戏资源...');
- // 预加载game-level分包
- await this.loadSubpackage('game-level');
-
- // 预加载完成,跳转到游戏场景
- this.loadGameScene();
-
- } catch (error) {
- console.error('[LaunchScreen] 资源预加载失败:', error);
- this.updateProgress(0, '资源加载失败,请重试');
- }
- }
- /**
- * 加载分包
- */
- private loadSubpackage(bundleName: string): Promise<void> {
- return new Promise((resolve, reject) => {
- console.log(`[LaunchScreen] 开始加载分包: ${bundleName}`);
-
- // 检查是否在微信小游戏环境
- if (typeof wx !== 'undefined' && wx && wx.loadSubpackage) {
- // 微信小游戏分包加载
- const loadTask = wx.loadSubpackage({
- name: bundleName,
- success: () => {
- console.log(`[LaunchScreen] 微信分包 ${bundleName} 加载成功`);
- // 分包加载成功后,再加载bundle
- this.loadBundleAfterSubpackage(bundleName, resolve, reject);
- },
- fail: (err) => {
- console.error(`[LaunchScreen] 微信分包 ${bundleName} 加载失败:`, err);
- // 微信分包加载失败时,尝试直接加载bundle作为降级方案
- console.log(`[LaunchScreen] 尝试直接加载bundle作为降级方案`);
- this.loadBundleAfterSubpackage(bundleName, resolve, reject);
- }
- });
-
- // 监听分包加载进度
- if (loadTask && loadTask.onProgressUpdate) {
- loadTask.onProgressUpdate((res) => {
- const progress = res.progress || 0;
- this.updateProgress(progress, `正在下载分包... ${progress}%`);
- });
- }
- } else {
- // 非微信环境,直接加载bundle
- console.log(`[LaunchScreen] 非微信环境,直接加载bundle`);
- this.loadBundleAfterSubpackage(bundleName, resolve, reject);
- }
- });
- }
-
- /**
- * 分包下载完成后加载bundle
- */
- private loadBundleAfterSubpackage(bundleName: string, resolve: Function, reject: Function) {
- assetManager.loadBundle(bundleName, (err, bundle) => {
- if (err) {
- console.error(`[LaunchScreen] Bundle ${bundleName} 加载失败:`, err);
- // 如果bundle加载失败,尝试跳过分包直接进入主场景
- console.log(`[LaunchScreen] Bundle加载失败,尝试跳过分包加载`);
- this.skipSubpackageAndLoadMainScene(resolve, reject);
- return;
- }
-
- console.log(`[LaunchScreen] Bundle ${bundleName} 加载成功`);
-
- // 预加载分包中的场景
- bundle.preloadScene('GameLevel', (finished: number, total: number) => {
- const progress = finished / total;
- this.updateProgress(progress * 100, `正在加载游戏场景... ${Math.floor(progress * 100)}%`);
- }, (err) => {
- if (err) {
- console.error('[LaunchScreen] GameLevel场景预加载失败:', err);
- // 场景预加载失败时,也尝试跳过分包
- this.skipSubpackageAndLoadMainScene(resolve, reject);
- return;
- }
-
- console.log('[LaunchScreen] GameLevel场景预加载完成');
- this.updateProgress(100, '加载完成!');
- resolve();
- });
- });
- }
-
- /**
- * 更新加载进度
- */
- private updateProgress(progress: number, message: string) {
- if (this.progressBar) {
- this.progressBar.progress = progress / 100;
- }
-
- if (this.progressLabel) {
- this.progressLabel.string = message;
- }
-
- console.log(`[LaunchScreen] ${message} (${progress.toFixed(1)}%)`);
- }
- /**
- * 跳过分包加载,直接进入主场景的降级方案
- */
- private skipSubpackageAndLoadMainScene(resolve: Function, reject: Function) {
- console.log('[LaunchScreen] 执行降级方案:跳过分包,尝试加载主场景');
-
- // 尝试加载主场景(如果存在的话)
- director.loadScene('First', (err) => {
- if (err) {
- console.error('[LaunchScreen] 主场景加载也失败:', err);
- this.updateProgress(0, '游戏加载失败,请刷新重试');
- reject(err);
- } else {
- console.log('[LaunchScreen] 成功加载主场景作为降级方案');
- this.updateProgress(100, '加载完成(降级模式)');
- resolve();
- }
- });
- }
- /**
- * 跳转到游戏场景
- */
- private loadGameScene() {
- console.log('[LaunchScreen] 准备跳转到GameLevel场景');
-
- // 延迟一秒后跳转,让用户看到加载完成的提示
- this.scheduleOnce(() => {
- // 使用分包中的场景
- const bundle = assetManager.getBundle('game-level');
- if (bundle) {
- bundle.loadScene('GameLevel', (err, scene) => {
- if (err) {
- console.error('[LaunchScreen] GameLevel场景加载失败:', err);
- // 如果分包场景加载失败,尝试加载主场景
- this.loadFallbackScene();
- return;
- }
-
- console.log('[LaunchScreen] 正在切换到GameLevel场景');
- director.runScene(scene);
- });
- } else {
- console.error('[LaunchScreen] game-level分包未找到,尝试加载主场景');
- this.loadFallbackScene();
- }
- }, 1.0);
- }
- /**
- * 加载降级场景
- */
- private loadFallbackScene() {
- console.log('[LaunchScreen] 尝试加载降级场景');
- director.loadScene('First', (err) => {
- if (err) {
- console.error('[LaunchScreen] 降级场景加载失败:', err);
- this.updateProgress(0, '游戏启动失败,请刷新页面重试');
- } else {
- console.log('[LaunchScreen] 成功加载降级场景');
- }
- });
- }
- }
|