去掉GameComponent非必要功能,减少内存

This commit is contained in:
donggang
2024-06-20 14:23:18 +08:00
parent bc22f630fd
commit 6d0dfb174d
2 changed files with 42 additions and 29 deletions

View File

@@ -88,7 +88,7 @@ export class ViewUtil {
}
/**
* 从资源缓存中找到预制资源名并创建一个显示对象
* 从资源缓存中找到预制资源名并创建一个显示对象建议使用GameComponent里的同名方法能自动管理内存施放
* @param path 资源路径
*/
static createPrefabNode(path: string): Node {
@@ -98,7 +98,7 @@ export class ViewUtil {
}
/**
* 加载预制并创建预制节点
* 加载预制并创建预制节点建议使用GameComponent里的同名方法能自动管理内存施放
* @param path 资源路径
*/
static createPrefabNodeAsync(path: string): Promise<Node> {
@@ -115,23 +115,6 @@ export class ViewUtil {
});
}
/**
* 加载预制节点
* @param path 资源路径
* @param callback 资源加载完成回调
*/
static loadPrefabNode(path: string, callback: Function) {
oops.res.load(path, Prefab, (err: Error | null, content: Prefab) => {
if (err) {
console.error(`名为【${path}】的资源加载失败`);
return;
}
var node = this.createPrefabNode(path);
callback(node);
});
}
/**
* 添加节点动画
* @param path 资源路径

View File

@@ -4,7 +4,7 @@
* @LastEditors: dgflash
* @LastEditTime: 2022-12-13 11:36:00
*/
import { Asset, Button, Component, EventHandler, EventKeyboard, EventTouch, Input, Node, __private, _decorator, input } from "cc";
import { Asset, Button, Component, EventHandler, EventKeyboard, EventTouch, Input, Node, Prefab, __private, _decorator, input, instantiate } from "cc";
import { oops } from "../../core/Oops";
import { EventDispatcher } from "../../core/common/event/EventDispatcher";
import { EventMessage, ListenerFunc } from "../../core/common/event/EventMessage";
@@ -58,17 +58,47 @@ export class GameComponent extends Component {
//#endregion
//#region 预制节点管理
/** 摊平的节点集合(不能重名) */
private nodes: Map<string, Node> = new Map();
/** 通过节点名获取预制上的节点,整个预制不能重名节点 */
getNode(name: string): Node | undefined {
return this.nodes.get(name);
// /** 摊平的节点集合(不能重名 */
// private nodes: Map<string, Node> = new Map();
// /** 通过节点名获取预制上的节点,整个预制不能有重名节点 */
// getNode(name: string): Node | undefined {
// return this.nodes.get(name);
// }
// /** 平摊所有节点存到Map<string, Node>中通过get(name: string)方法获取 */
// nodeTreeInfoLite() {
// ViewUtil.nodeTreeInfoLite(this.node, this.nodes);
// }
/**
* 从资源缓存中找到预制资源名并创建一个显示对象
* @param path 资源路径
*/
createPrefabNode(path: string): Node {
var p: Prefab = oops.res.get(path, Prefab)!;
var n = instantiate(p);
return n;
}
/** 平摊所有节点存到Map<string, Node>中通过get(name: string)方法获取 */
nodeTreeInfoLite() {
ViewUtil.nodeTreeInfoLite(this.node, this.nodes);
/**
* 加载预制并创建预制节点
* @param path 资源路径
*/
createPrefabNodeAsync(path: string): Promise<Node> {
return new Promise((resolve, reject) => {
this.load(path, Prefab, (err: Error | null, content: Prefab) => {
if (err) {
console.error(`名为【${path}】的资源加载失败`);
resolve(null!);
}
else {
var node = instantiate(content);
resolve(node);
}
});
});
}
//#endregion
@@ -358,7 +388,7 @@ export class GameComponent extends Component {
}
// 节点引用数据清除
this.nodes.clear();
// this.nodes.clear();
// 自动释放资源
this.releaseAudioEffect();