cocos Creator 3.2 关于 NodePool 对象池的应用- (弹出框)
1:NodePool 作用
NodePool 是用于管理节点对象的对象缓存池。它可以帮助您提高游戏性能,适用于优化对象的反复创建和销毁 以前 cocos2d-x 中的 pool 和新的节点事件注册系统不兼容,因此请使用 `NodePool` 来代替。
新的 NodePool 需要实例化之后才能使用,每种不同的节点对象池需要一个不同的对象池实例,这里的种类对应于游戏中的节点设计,一个 prefab 相当于一个种类的节点。
在创建缓冲池时,可以传入一个包含 unuse, reuse 函数的组件类型用于节点的回收和复用逻辑。
2: 一些常见的用例
1.在游戏中的子弹(死亡很快,频繁创建,对其他对象无副作用)
2.糖果粉碎传奇中的木块(频繁创建)。
等等....
3 :应用
以Prefab为例 首先了解instantiate的作用:创建的一个perfab资源 并不是我要的Node 节点 ,想要perfab应用到场景中必须通过instantiate实例化返回一个节点
/*** @zh 从 Prefab 实例化出新节点。* @en Instantiate a node from the Prefab.* @param prefab The prefab.* @returns The instantiated node.* @example* ```ts* import { instantiate, director } from 'cc';* // Instantiate node from prefab.* const node = instantiate(prefabAsset);* node.parent = director.getScene();* ```*/export function instantiate(prefab: Prefab): Node;
动态获取资源 动态获取到resources/p_perfabs 下面的所有 perfab
代码如下:
getPerfabList() {this.StorePerfab = new NodePool();let that: any = this;resources.loadDir("p_perfabs", function (err, assets) {if (err) returnassets.map((t: any) => that.StorePerfab.put(instantiate(t)))});}
接下来讲一个使用场景:
在游戏中有很多的弹出框,例如:游戏设置,游戏结束 ,游戏gameover ,通关等。 弹出框无处不在,如何管理这些弹出框呢。 就用到了NodePool 节点缓存,
逻辑如下图:
1 创建 一个弹窗perfab 起名为 GameOverUI;
2 创建gameOverUI.ts , gameOverUI加载组件gameover.ts;
如图
代码如下:gameOverUI.ts
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;@ccclass('gameOverUI')
export class gameOverUI extends Component {start() {// [3]}close() {this.node.active = false;}show() {this.node.active = true;}}
3:创建UIManage.ts
import { _decorator, Component, Node } from 'cc';
import PerfabLoop from './PerfabLoop'
const { ccclass, property } = _decorator;@ccclass('UIManage')
export class UIManage extends Component {PerfabLoopObj?: any = PerfabLoop.instance;static _instance: UIManage;start() {console.log(PerfabLoop.instance.StorePerfab)// [3]}/*** 全局定义唯一实例*/static get instance() {if (this._instance) {return this._instance;}this._instance = new UIManage();// this._instance.getPerfabList()return this._instance;}/*** 显示弹窗* @param resUrl 资源路径* @param compontentName 组件名称* @param e 当前this*/showDialog(resUrl: any, compontentName: string, e: any) {this.PerfabLoopObj.getNode({ resUrl, compontentName, e })}/*** 隐藏弹窗*/hideDialog(resUrl: any, compontentName: string, e: any) {this.PerfabLoopObj.closeNode({ resUrl, compontentName, e })}
}
4:创建PerfabLoop.ts :节点池
import { _decorator, Component, Node, NodePool, resources, Prefab, instantiate } from 'cc';
const { ccclass, property } = _decorator;
interface Props {resUrl: string;compontentName: string;e: any;
}
@ccclass('PerfabLoop')
export default class PerfabLoop extends Component {public StorePerfab = new NodePool();static _instance: PerfabLoop;private props: Props = {resUrl: "",compontentName: "",e: null}/*** 全局定义唯一实例*/static get instance() {if (this._instance) {return this._instance;}this._instance = new PerfabLoop();return this._instance;}getNode(props: Props) {this.props = props;const { resUrl, compontentName, e } = props;this.isCheckNode(resUrl, compontentName, e)}closeNode(props: Props) {this.props = props;const { compontentName, e } = props;e.getComponentInChildren(compontentName)?.hide();}/*** 检查Node节点是否被加载过* @param resUrl * @param compontentName * @param e */isCheckNode(resUrl: any, compontentName: string, e: any) {console.log('====this.StorePerfab===', this.StorePerfab)const Flag = this.StorePerfab?._pool.filter((t: any) => t.name == compontentName).length > 0 ? true : falseif (Flag) {this.showNode(e)} else {this.getResource(resUrl, e)}}/*** 显示弹窗*/showNode(e: any) {const { compontentName } = this.props;e.getComponentInChildren(compontentName).show();}hideNode(e: any) {const { compontentName } = this.props;e.getComponentInChildren(compontentName).hide();}/*** @param resUrl 资源路径* @param callback 加载成功之后的回掉*/getResource(resUrl: string, e: any) {let that: any = this;resources.load(resUrl, Prefab, (err, prefab) => {if (!prefab) {return}const newNode = instantiate(prefab);that.StorePerfab.put(newNode)e.node.addChild(newNode)});}
}
5:应用弹出层
/*** 显示弹窗* @param resUrl 资源路径* @param compontentName 组件名称* @param this 当前this*/ this.UIManageStr.showDialog('dialogPerfab/gameOverUI', 'gameOverUI', this)
cocos Creator 3.2 关于 NodePool 对象池的应用- (弹出框)相关推荐
- bootstrap中轮播图、模态框、提示框/弹出框、滚动监听、弹性布局、响应式flex、多媒体对象
轮播图: bootstrap封装了轮播图的功能,其具体如下: 类名 描述 .carousel 创建一个轮播图块的容器,实质是做布局用:且此容器应该有一个di属性,其属性值提供给下面左右按钮href锚点 ...
- 5弹出阴影遮罩_千文详述Cocos Creator弹出式对话框实现技术,着实硬核
正文 在Cocos Creator游戏开发中,经常需要使用到弹出式对话框,下面我们就一起来封装下自己的弹出式对话框. 一.弹出式对话框原理解析 1:对话框的结构: 1. `根节点 -->`2. ...
- 基于cocos creator画六维图
这个仅仅是一个代码例子. 1.cocos creator的左下方资源管理器,点右键,弹出菜单,创建一个Scene,默认名称就可以. 2.同样在再创建一个JavaScript,名称为drawsix 3. ...
- Cocos Creator 3D 材质系统:曲面效果如何实现?
引言 前不久发布的 Cocos Creator 1.0.2 版本中正式加入了对 OPPO 小游戏.vivo 小游戏以及华为快游戏平台的支持,在诸多 Creator 3D 制作的小游戏案例中,<猪 ...
- 小游戏开发上手体验 - Cocos Creator
微信群里最大的骚扰源有两种: 一是转发#吱口令#~!@#¥%--&*,长按复制此消息领红包之类的 另一种就是各种小程序和小游戏的分享 前天有同学无意间把一个小游戏分享到了答疑群中,我看了一下, ...
- C++内存池、对象池
使用C/C++实现内存池技术 使用C/C++实现内存池技术 内存管理技术是开发多媒体应用和服务的很重要的知识.DMSP应用中会有频繁的缓冲区的创建和释放操作,这些操作会降低程序的运行效率和运行时间.本 ...
- Unity3D中角色撞击物体弹出提示框或显示对象效果
角色撞击物体弹出提示框或显示对象效果 刚开始使用的是调用SetActive方法 例: 在Start()函数中设置对象的SetActive属性为false,在函数 void start() {gameo ...
- Cocos Creator 3.3中的NodePool(节点池)
引擎版本:3.3 不知为何,我在官方的3.3版文档和3.3版API里都没有找到NodePool的介绍. 进入cc.d.ts声明文件,可以看到NodePool类的大致描述. `NodePool` 是用于 ...
- Cocos Creator 性能优化——对象池
对于游戏开发人员来说,性能优化是一个永远绕不过的话题,极致的性能是我们毕生的追求,今天就来带大家学习一下性能优化方法之一--「对象池」. 为什么要使用对象池? 在开始之前要先弄明白为什么要使用对象池 ...
最新文章
- 百度与华为重磅合作!李彦宏:技术是百度的信仰
- nodejs TCP server和TCP client如何进行数据交互
- 太阳能板清洗机器人科沃斯_太阳能电池板清洁机器人
- 如何在Ubuntu中屏蔽一个网站
- bzoj1639[Usaco2007 Mar]Monthly Expense 月度开支*
- 小米12系列旗舰最新爆料:内藏5000mAh电池但机身更薄
- 阿里的爱心助农“生意”:严把质量关 一场多业务线的联动大练兵
- 早期TikTok运营者经验之谈?
- vue-router各个属性的作用及用法
- 实时查看Linux IO复用情况
- 28款GitHub最流行的开源机器学习项目
- 活动回顾 I 《传奇动物园》项目团队沙盘演练圆满结束!
- H7-TOOL发布固件V2.12,正式发布脱机烧录1拖16支持,脱机烧录增加NXP的MKL系列支持,更新串口助手,CAN助手等(2022-01-09)
- hdoj 瞬间移动 5698(逆元)
- 18 | 需求管理:太多人给你安排任务,怎么办?
- 通过L0phtcrack 7进行账号口令破解
- 干货|6个牛逼的基于Vue.js的后台控制面板,接私活必备
- cpptraj的常用命令
- 博客文章导航(嵌入式宝藏站)(2023.2.20更新)
- 银行业务部门名称中英对照
热门文章
- 创新之路 纪录片观后感
- vue+ElementUI 实现管理端照片墙(或广告位)效果
- Windows 安装 Windows 版 iCloud 之后我的电脑里多了个 iCloud 照片图标
- 微服务下的链路追踪(Sleuth+Zipkin)
- 梦龙物联卡冻结_四川梦龙科技物联卡哪个划算
- 怎么设置微信公众号自动回复蓝色字体,点击蓝色字体自动发送出去
- 无法搜出共享打印机的计算机名,Win10搜不到共享打印机怎么回事?Win10搜不到共享打印机的处理方法...
- 用xmind做读书笔记的几个层次
- 单词接龙acwing
- spark-3.1.2兼容多版本hive