feat: 新增资源分包与启动场景,支持音效播放
This commit is contained in:
111
assets/scripts/Start.ts
Normal file
111
assets/scripts/Start.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { _decorator, Component, Node, assetManager, AssetManager, director, Scene } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('Start')
|
||||
export class Start extends Component {
|
||||
// 需要加载的 bundle 列表 - 可以根据实际情况修改
|
||||
@property({ type: [String], tooltip: '需要加载的 bundle 名称列表' })
|
||||
private bundlesToLoad: string[] = ['bundle1'];
|
||||
|
||||
// 目标场景名称
|
||||
@property({ type: String, tooltip: '加载完成后跳转的场景名称' })
|
||||
private targetScene: string = 'main';
|
||||
|
||||
// 已加载的 bundle 数量
|
||||
private loadedBundleCount: number = 0;
|
||||
|
||||
// 存储已加载的 bundle
|
||||
private loadedBundles: Map<string, AssetManager.Bundle> = new Map();
|
||||
|
||||
protected onLoad(): void {
|
||||
// 加载所有 bundle
|
||||
this.loadAllBundles();
|
||||
}
|
||||
|
||||
start() {
|
||||
|
||||
}
|
||||
|
||||
update(deltaTime: number) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载所有 bundle
|
||||
*/
|
||||
private loadAllBundles(): void {
|
||||
if (this.bundlesToLoad.length === 0) {
|
||||
console.log('没有需要加载的 bundle,直接跳转到目标场景');
|
||||
this.loadTargetScene();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`开始加载 ${this.bundlesToLoad.length} 个 bundle:`, this.bundlesToLoad);
|
||||
|
||||
for (const bundleName of this.bundlesToLoad) {
|
||||
this.loadBundle(bundleName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载单个 bundle
|
||||
*/
|
||||
private loadBundle(bundleName: string): void {
|
||||
assetManager.loadBundle(bundleName, (err: Error | null, bundle: AssetManager.Bundle | null) => {
|
||||
if (err) {
|
||||
console.error(`加载 ${bundleName} 分包失败:`, err);
|
||||
// 即使加载失败也继续,确保不会因为一个 bundle 失败而卡住
|
||||
this.onBundleLoadComplete();
|
||||
return;
|
||||
}
|
||||
|
||||
if (bundle) {
|
||||
console.log(`${bundleName} 分包加载成功`);
|
||||
this.loadedBundles.set(bundleName, bundle);
|
||||
this.onBundleLoadComplete();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* bundle 加载完成后的处理
|
||||
*/
|
||||
private onBundleLoadComplete(): void {
|
||||
this.loadedBundleCount++;
|
||||
console.log(`已加载 ${this.loadedBundleCount}/${this.bundlesToLoad.length} 个 bundle`);
|
||||
|
||||
// 检查是否所有 bundle 都已加载完成
|
||||
if (this.loadedBundleCount >= this.bundlesToLoad.length) {
|
||||
this.onAllBundlesLoaded();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 所有 bundle 加载完成后的回调
|
||||
*/
|
||||
private onAllBundlesLoaded(): void {
|
||||
console.log('所有 bundle 加载完成!');
|
||||
console.log('成功加载的 bundles:', Array.from(this.loadedBundles.keys()));
|
||||
|
||||
// 所有 bundle 加载完成后,跳转到目标场景
|
||||
this.loadTargetScene();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载目标场景
|
||||
*/
|
||||
private loadTargetScene(): void {
|
||||
console.log(`开始加载 ${this.targetScene} 场景...`);
|
||||
|
||||
director.loadScene(this.targetScene, (err: Error | null, scene: Scene | null) => {
|
||||
if (err) {
|
||||
console.error(`加载 ${this.targetScene} 场景失败:`, err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (scene) {
|
||||
console.log(`${this.targetScene} 场景加载成功!`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user