imageItem 缓存池
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { NodePool, instantiate, Node, isValid } from "cc";
|
||||
import { DetailImageItem } from "../uiitems/DetailImageItem";
|
||||
|
||||
export class DetailImageItemPool {
|
||||
private static _instance: DetailImageItemPool = null;
|
||||
|
||||
private _pool: NodePool;
|
||||
private _template: Node = null;
|
||||
|
||||
private constructor() {
|
||||
this._pool = new NodePool();
|
||||
}
|
||||
|
||||
public static get Instance(): DetailImageItemPool {
|
||||
if (!this._instance) {
|
||||
this._instance = new DetailImageItemPool();
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
public setTemplate(template: Node): void {
|
||||
this._template = template;
|
||||
}
|
||||
|
||||
public get(): Node {
|
||||
let node: Node = null;
|
||||
|
||||
if (this._pool.size() > 0) {
|
||||
node = this._pool.get();
|
||||
} else {
|
||||
if (this._template) {
|
||||
node = instantiate(this._template);
|
||||
}
|
||||
}
|
||||
|
||||
if (node) {
|
||||
const item = node.getComponent(DetailImageItem);
|
||||
if (item && item.reset) {
|
||||
item.reset();
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
public put(node: Node): void {
|
||||
if (node && isValid(node)) {
|
||||
node.removeFromParent();
|
||||
this._pool.put(node);
|
||||
}
|
||||
}
|
||||
|
||||
public clear(): void {
|
||||
this._pool.clear();
|
||||
}
|
||||
|
||||
public getPoolSize(): number {
|
||||
return this._pool.size();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "da0fc97b-079e-4f2f-b288-779e5729c21b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "c651522c-4a0f-45c1-85ac-118560c04f90",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
Node,
|
||||
Sprite,
|
||||
VideoPlayer,
|
||||
instantiate,
|
||||
UITransform,
|
||||
Size,
|
||||
Vec3,
|
||||
@@ -14,6 +13,7 @@ import li_BaseView from "db://assets/Scripts/Main/Common/li_BaseView";
|
||||
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
|
||||
import { DetailImageItem } from "../../uiitems/DetailImageItem";
|
||||
import { NavigationManager, PanelType } from "../../manager/NavigationManager";
|
||||
import { DetailImageItemPool } from "../../manager/DetailImageItemPool";
|
||||
import { ConfigManager } from "../../manager/ConfigManager";
|
||||
import LanguageUtils from "../../../Main/Common/LanguageUtils";
|
||||
import Utils from "../../../Main/Common/Utils";
|
||||
@@ -82,6 +82,8 @@ export class GirlDetailPanel extends li_BaseView {
|
||||
|
||||
//this.refresh();
|
||||
|
||||
DetailImageItemPool.Instance.setTemplate(this.imgItemInst.node);
|
||||
|
||||
Utils.addInnerEL(InnerMsgCode.LanguageChange, this, () => {
|
||||
this.girlName.string = LanguageUtils.getText(this.nameKey);
|
||||
let desc = "";
|
||||
@@ -279,35 +281,22 @@ export class GirlDetailPanel extends li_BaseView {
|
||||
resIds: number[]
|
||||
) {
|
||||
for (let i = this.imgsLayout.children.length - 1; i >= 0; i--) {
|
||||
this.imgsLayout.children[i].destroy();
|
||||
DetailImageItemPool.Instance.put(this.imgsLayout.children[i]);
|
||||
}
|
||||
|
||||
let count = resIds.length;
|
||||
for (let i = 0; i < count; i++) {
|
||||
let newNode = instantiate(this.imgItemInst.node);
|
||||
let item = newNode.getComponent(DetailImageItem);
|
||||
let resId = resIds[i];
|
||||
item.refresh(this, type, category, id, resId);
|
||||
newNode.active = true;
|
||||
this.imgsLayout.addChild(newNode);
|
||||
let newNode = DetailImageItemPool.Instance.get();
|
||||
if (newNode) {
|
||||
let item = newNode.getComponent(DetailImageItem);
|
||||
let resId = resIds[i];
|
||||
item.refresh(this, type, category, id, resId);
|
||||
newNode.active = true;
|
||||
this.imgsLayout.addChild(newNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化视频位置和属性
|
||||
*/
|
||||
private initVideoPosition() {
|
||||
if (!this.videoLayer || !this.videoArea) return;
|
||||
|
||||
// 使用 videoArea 的世界坐标位置
|
||||
const videoAreaWorldPos = this.videoArea.node.getWorldPosition();
|
||||
// 视频位置和缩放已在 playVideo 中处理
|
||||
//this.videoLayer.setVideoScale(1);
|
||||
console.log(
|
||||
`GirlDetailPanel: 初始化视频世界位置到 (${videoAreaWorldPos.x}, ${videoAreaWorldPos.y})`
|
||||
);
|
||||
}
|
||||
|
||||
OnClickChatBtn() {
|
||||
// 使用带过渡动画的导航方法
|
||||
NavigationManager.Instance.switchToPanel(PanelType.CHAT);
|
||||
@@ -340,6 +329,11 @@ export class GirlDetailPanel extends li_BaseView {
|
||||
//console.log("GirlDetailPanel: 销毁时停止视频播放");
|
||||
}
|
||||
|
||||
// 回收所有使用中的节点到对象池
|
||||
for (let i = this.imgsLayout.children.length - 1; i >= 0; i--) {
|
||||
DetailImageItemPool.Instance.put(this.imgsLayout.children[i]);
|
||||
}
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,4 +222,25 @@ export class DetailImageItem extends Component {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.base = null;
|
||||
this.url = "";
|
||||
this.isImage = false;
|
||||
this.category = 0;
|
||||
this.girlId = 0;
|
||||
this.resId = 0;
|
||||
this.type = null;
|
||||
this.isVisible = false;
|
||||
|
||||
this.question.active = true;
|
||||
this.video.enabled = false;
|
||||
this.priceFrame.active = false;
|
||||
this.loading.active = false;
|
||||
this.videoPrice.string = "";
|
||||
|
||||
if (this.img && this.img.spriteFrame) {
|
||||
this.img.spriteFrame = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user