From f51f77d6d0896f9adc73671fbbe2a34cf9fbd3bc Mon Sep 17 00:00:00 2001 From: dgflash Date: Fri, 27 Sep 2024 20:47:10 +0800 Subject: [PATCH] =?UTF-8?q?1.=20ViewUtil.createPrefabNode=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=87=E5=AE=9A=E5=8C=85=E8=8E=B7=E5=8F=96=E8=B5=84?= =?UTF-8?q?=E6=BA=90=202.=20ViewUtil.createPrefabNodeAsync=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=87=E5=AE=9A=E5=8C=85=E8=8E=B7=E5=8F=96=E8=B5=84?= =?UTF-8?q?=E6=BA=90=203.=20GameComponent.createPrefabNode=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=87=E5=AE=9A=E5=8C=85=E8=8E=B7=E5=8F=96=E8=B5=84?= =?UTF-8?q?=E6=BA=90=204.=20GameComponent.createPrefabNodeAsync=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9A=E5=8C=85=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/core/common/audio/AudioEffect.ts | 2 +- assets/core/utils/ViewUtil.ts | 30 +++++++++++++++---------- assets/module/common/GameComponent.ts | 21 ++++------------- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/assets/core/common/audio/AudioEffect.ts b/assets/core/common/audio/AudioEffect.ts index 5234bad..e132819 100644 --- a/assets/core/common/audio/AudioEffect.ts +++ b/assets/core/common/audio/AudioEffect.ts @@ -116,7 +116,7 @@ export class AudioEffect extends AudioSource { const rr = this.effects.get(key); if (rr) { this.effects.delete(key); - oops.res.release(rr.path!, rr.bundle!); + rr.ac.decRef(); } } } diff --git a/assets/core/utils/ViewUtil.ts b/assets/core/utils/ViewUtil.ts index a02b7a7..b75eb85 100644 --- a/assets/core/utils/ViewUtil.ts +++ b/assets/core/utils/ViewUtil.ts @@ -4,8 +4,9 @@ * @LastEditors: dgflash * @LastEditTime: 2023-01-19 14:52:12 */ -import {Animation, AnimationClip, EventTouch, instantiate, Node, Prefab, Size, UITransform, v3, Vec3} from "cc"; -import {resLoader} from "../common/loader/ResLoader"; +import { Animation, AnimationClip, EventTouch, instantiate, Node, Prefab, Size, UITransform, v3, Vec3 } from "cc"; +import { resLoader } from "../common/loader/ResLoader"; +import { oops } from "../Oops"; /** 显示对象工具 */ export class ViewUtil { @@ -86,25 +87,30 @@ export class ViewUtil { /** * 从资源缓存中找到预制资源名并创建一个显示对象(建议使用GameComponent里的同名方法,能自动管理内存施放) - * @param path 资源路径 + * @param path 资源路径 + * @param bundleName 资源包名 */ - static createPrefabNode(path: string): Node { - const p: Prefab = resLoader.get(path, Prefab)!; - return instantiate(p); + static createPrefabNode(path: string, bundleName: string = oops.res.defaultBundleName): Node { + const p = resLoader.get(path, Prefab, bundleName); + if (p) { + return instantiate(p); + } + return null!; } /** * 加载预制并创建预制节点(建议使用GameComponent里的同名方法,能自动管理内存施放) - * @param path 资源路径 + * @param path 资源路径 + * @param bundleName 资源包名 */ - static createPrefabNodeAsync(path: string): Promise { + static createPrefabNodeAsync(path: string, bundleName: string = oops.res.defaultBundleName): Promise { return new Promise(async (resolve, reject) => { - const prefab = await resLoader.loadAsync(path, Prefab); - if (prefab) { - const node = this.createPrefabNode(path); - resolve(node); + const p = await resLoader.loadAsync(bundleName, path, Prefab); + if (p) { + resolve(instantiate(p)); } else { + console.error(`名为【${path}】的资源加载失败`); resolve(null!); } }); diff --git a/assets/module/common/GameComponent.ts b/assets/module/common/GameComponent.ts index 0cb1d65..36ff44c 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, __private, _decorator, input, instantiate } from "cc"; +import { Asset, Button, Component, EventHandler, EventKeyboard, EventTouch, Input, Node, __private, _decorator, input } from "cc"; import { oops } from "../../core/Oops"; import { EventDispatcher } from "../../core/common/event/EventDispatcher"; import { EventMessage, ListenerFunc } from "../../core/common/event/EventMessage"; @@ -92,10 +92,8 @@ export class GameComponent extends Component { * 从资源缓存中找到预制资源名并创建一个显示对象 * @param path 资源路径 */ - createPrefabNode(path: string): Node { - var p: Prefab = oops.res.get(path, Prefab)!; - var n = instantiate(p); - return n; + createPrefabNode(path: string, bundleName: string = oops.res.defaultBundleName): Node { + return ViewUtil.createPrefabNode(path, bundleName); } /** @@ -104,18 +102,7 @@ export class GameComponent extends Component { * @param bundleName 资源包名 */ createPrefabNodeAsync(path: string, bundleName: string = oops.res.defaultBundleName): Promise { - return new Promise((resolve, reject) => { - this.load(bundleName, path, Prefab, (err: Error | null, content: Prefab) => { - if (err) { - console.error(`名为【${path}】的资源加载失败`); - resolve(null!); - } - else { - var node = instantiate(content); - resolve(node); - } - }); - }); + return ViewUtil.createPrefabNodeAsync(path, bundleName); } //#endregion