From f2dae55173df53802a0e2d137b381b849cffa14a Mon Sep 17 00:00:00 2001 From: dgflash Date: Tue, 23 Sep 2025 22:32:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DGameComponent.playEffect?= =?UTF-8?q?=E6=92=AD=E6=94=BE=E9=9F=B3=E6=95=88=E6=9C=AA=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E4=BC=A0=E5=8F=82=E6=95=B0=E6=97=B6=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/module/common/GameComponent.ts | 64 ++++++++++++++++++--------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/assets/module/common/GameComponent.ts b/assets/module/common/GameComponent.ts index c9ad13c..8ed4677 100644 --- a/assets/module/common/GameComponent.ts +++ b/assets/module/common/GameComponent.ts @@ -4,7 +4,7 @@ * @LastEditors: dgflash * @LastEditTime: 2022-12-13 11:36:00 */ -import { Asset, Button, Component, EventHandler, EventKeyboard, EventTouch, Input, Node, Prefab, Sprite, SpriteFrame, __private, _decorator, input, instantiate, isValid } from "cc"; +import { Asset, Button, Component, EventHandler, EventKeyboard, EventTouch, Input, Node, Prefab, Sprite, SpriteFrame, __private, _decorator, input, isValid } from "cc"; import { oops } from "../../core/Oops"; import { AudioEffect } from "../../core/common/audio/AudioEffect"; import { IAudioParams } from "../../core/common/audio/IAudio"; @@ -99,10 +99,19 @@ export class GameComponent extends Component { * 从资源缓存中找到预制资源名并创建一个显示对象 * @param path 资源路径 */ - createPrefabNode(path: string, bundleName: string = oops.res.defaultBundleName): Promise { + createPrefabNode(path: string, bundleName: string = oops.res.defaultBundleName): Node { + return ViewUtil.createPrefabNode(path, bundleName); + } + + /** + * 加载预制并创建预制节点 + * @param path 资源路径 + * @param bundleName 资源包名 + */ + createPrefabNodeAsync(path: string, bundleName: string = oops.res.defaultBundleName): Promise { return new Promise(async (resolve, reject) => { - const prefab = await this.load(bundleName, path, Prefab); - const node = instantiate(prefab); + await this.loadAsync(bundleName, path, Prefab); + let node = ViewUtil.createPrefabNode(path, bundleName); resolve(node); }); } @@ -188,27 +197,42 @@ export class GameComponent extends Component { * @param paths 资源路径 * @param type 资源类型 * @param onProgress 加载进度回调 + * @param onComplete 加载完成回调 */ - load(bundleName: string, paths: Paths | AssetType, type?: AssetType) { + load(bundleName: string, paths: Paths, type: AssetType, onProgress: ProgressCallback, onComplete: CompleteCallback): void; + load(bundleName: string, paths: Paths, onProgress: ProgressCallback, onComplete: CompleteCallback): void; + load(bundleName: string, paths: Paths, onComplete?: CompleteCallback): void; + load(bundleName: string, paths: Paths, type: AssetType, onComplete?: CompleteCallback): void; + load(paths: Paths, type: AssetType, onProgress: ProgressCallback, onComplete: CompleteCallback): void; + load(paths: Paths, onProgress: ProgressCallback, onComplete: CompleteCallback): void; + load(paths: Paths, onComplete?: CompleteCallback): void; + load(paths: Paths, type: AssetType, onComplete?: CompleteCallback): void; + load( + bundleName: string, + paths?: Paths | AssetType | ProgressCallback | CompleteCallback, + type?: AssetType | ProgressCallback | CompleteCallback, + onProgress?: ProgressCallback | CompleteCallback, + onComplete?: CompleteCallback, + ) { this.addPathToRecord(ResType.Load, bundleName, paths); - return oops.res.load(bundleName, paths, type); + oops.res.load(bundleName, paths, type, onProgress, onComplete); } /** - * 加载指定资源包中的多个任意类型资源 + * 异步加载一个资源 * @param bundleName 远程包名 * @param paths 资源路径 - * @param onProgress 加载进度回调 - * @param onComplete 加载完成回调 + * @param type 资源类型 */ - loadAny(bundleName: string | string[], paths: string[] | ProgressCallback, onProgress?: ProgressCallback | CompleteCallback, onComplete?: CompleteCallback): void { - if (typeof bundleName === "string" && paths instanceof Array) { - this.addPathToRecord(ResType.Load, bundleName, paths); - } - else { - this.addPathToRecord(ResType.Load, resLoader.defaultBundleName, bundleName); - } - oops.res.loadAny(bundleName, paths, onProgress, onComplete); + loadAsync(bundleName: string, paths: Paths, type: AssetType): Promise; + loadAsync(bundleName: string, paths: Paths): Promise; + loadAsync(paths: Paths, type: AssetType): Promise; + loadAsync(paths: Paths): Promise; + loadAsync(bundleName: string, + paths?: Paths | AssetType | ProgressCallback | CompleteCallback, + type?: AssetType | ProgressCallback | CompleteCallback): Promise { + this.addPathToRecord(ResType.Load, bundleName, paths); + return oops.res.loadAsync(bundleName, paths, type); } /** @@ -283,7 +307,7 @@ export class GameComponent extends Component { * @param bundle 资源包名 */ async setSprite(target: Sprite, path: string, bundle: string = resLoader.defaultBundleName) { - const spriteFrame = await this.load(bundle, path, SpriteFrame); + const spriteFrame = await this.loadAsync(bundle, path, SpriteFrame); if (!spriteFrame || !isValid(target)) { const rps = this.resPaths.get(ResType.Load); if (rps) { @@ -313,7 +337,7 @@ export class GameComponent extends Component { * @param url 资源地址 * @param params 音效播放参数 */ - playEffect(url: string, params?: IAudioParams): Promise { + async playEffect(url: string, params?: IAudioParams): Promise { return new Promise(async (resolve, reject) => { // 音效播放完,关闭正在播放状态的音乐效果 if (params == null) { @@ -324,7 +348,7 @@ export class GameComponent extends Component { } let ae = await oops.audio.playEffect(url, params); if (ae) { - this.addPathToRecord(ResType.Load, ae.params.bundle!, url); + this.addPathToRecord(ResType.Load, ae.params!.bundle!, url); resolve(ae); } else {