1. ViewUtil.createPrefabNode 支持指定包获取资源

2. ViewUtil.createPrefabNodeAsync 支持指定包获取资源
3. GameComponent.createPrefabNode 支持指定包获取资源
4. GameComponent.createPrefabNodeAsync 支持指定包获取资源
This commit is contained in:
dgflash
2024-09-27 20:47:10 +08:00
parent b760ce3458
commit f51f77d6d0
3 changed files with 23 additions and 30 deletions

View File

@@ -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();
}
}
}

View File

@@ -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<Node> {
static createPrefabNodeAsync(path: string, bundleName: string = oops.res.defaultBundleName): Promise<Node> {
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!);
}
});

View File

@@ -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<Node> {
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