目录— 个文件
⌕
目录加载中…
…/core/utility/UtilityNodeHelper.ts
1import { Node, Toggle, instantiate } from 'cc';
2import { Framework } from "../../Framework";
3import { AudioFramePath } from "../music/AudioFramePath";
4
5const ClickEvent = "click";
6const ToggleEvent = "toggle";
7const SliderEvent = "slide";
8const DestroyEvent = "node-destroyed";
9
10/**
11 * 节点相关的实用工具
12 */
13export class UtilityNodeHelper {
14 /**
15 * 添加点击事件
16 * @param node 节点
17 * @param callback 回调
18 * @param context 上下文
19 */
20 public static AddClick(node: Node, callback: any, context: any, isPlaySound: boolean = true) {
21 if (!node)
22 return;
23
24 node.off(ClickEvent);
25 node.on(ClickEvent, () => {
26 //按钮点击间隔要延时150ms,避免重复点击-todo
27 if (isPlaySound)
28 Framework.Audio.PlaySound(AudioFramePath.BtnClick_1);
29 callback.call(context);
30 }, context);
31 }
32
33 /**
34 * 添加Toogle事件
35 * @param node 节点
36 * @param callback 回调
37 * @param context 上下文
38 */
39 public static AddToggle(node: Node, callback: (toogle: Toggle) => void, context: any) {
40 if (!node)
41 return;
42
43 node.off(ToggleEvent);
44 node.on(ToggleEvent, callback, context);
45 }
46
47 /**
48 * 添加触摸开始事件
49 * @param node 节点
50 * @param callback 回调
51 * @param context 上下文
52 */
53 public static AddTouchStart(node: Node, callback: any, context: any) {
54 if (!node)
55 return;
56
57 node.on(Node.EventType.TOUCH_START, callback, context);
58 }
59
60 /**
61 * 添加触摸移动事件
62 * @param node 节点
63 * @param callback 回调
64 * @param context 上下文
65 */
66 public static AddTouchMove(node: Node, callback: any, context: any) {
67 if (!node)
68 return;
69
70 node.on(Node.EventType.TOUCH_MOVE, callback, context);
71 }
72
73 /**
74 * 添加触摸结束事件
75 * @param node 节点
76 * @param callback 回调
77 * @param context 上下文
78 */
79 public static AddTouchEnd(node: Node, callback: any, context: any) {
80 if (!node)
81 return;
82
83 node.on(Node.EventType.TOUCH_END, callback, context);
84 }
85
86 /**
87 * 添加触摸取消事件
88 * @param node 节点
89 * @param callback 回调
90 * @param context 上下文
91 */
92 public static AddTouchCancel(node: Node, callback: any, context: any) {
93 if (!node)
94 return;
95
96 node.on(Node.EventType.TOUCH_CANCEL, callback, context);
97 }
98
99 /**
100 * 添加节点销毁事件
101 * @param node 节点
102 * @param callback 回调
103 * @param context 上下文
104 */
105 public static AddDestroy(node: Node, callback: any, context: any) {
106 if (!node)
107 return;
108
109 node.on(DestroyEvent, callback, context);
110 }
111
112 /**
113 * 添加滑块事件
114 * @param node 节点
115 * @param callback 回调
116 * @param context 上下文
117 */
118 public static AddSlider(node: Node, callback: any, context: any) {
119 if (!node)
120 return;
121
122 node.on(SliderEvent, callback, context);
123 }
124
125 /**
126 * 通用节点实例化复用方法
127 * @param parent 父节点
128 * @param prefab 预制体
129 * @param totalCount 总数量
130 * @param callback 每个节点创建/复用时的回调 (index: 索引, node: 节点)
131 * @param cleanAll 是否清理现有节点(true=销毁所有重建,false=复用现有)
132 */
133 public static InstantiateChildren(
134 parent: Node,
135 prefab: Node,
136 totalCount: number,
137 callback: (index: number, node: Node) => void,
138 cleanAll: boolean = false
139 ) {
140 // 1. 清理模式:销毁所有现有子节点
141 if (cleanAll) {
142 parent.destroyAllChildren();
143 }
144 // 2. 复用模式:先隐藏所有现有子节点
145 else {
146 parent.children.forEach(child => child.active = false);
147 }
148
149 // 3. 实例化/复用节点
150 for (let i = 0; i < totalCount; i++) {
151 let node: Node;
152 // 复用现有节点
153 if (i < parent.children.length && !cleanAll) {
154 node = parent.children[i];
155 node.active = true; // 激活复用节点
156 }
157 // 创建新节点
158 else {
159 if (!prefab) {
160 Framework.Log.Error("InstantiateChildren: 预制体为空");
161 return;
162 }
163 node = instantiate(prefab); // 实例化预制体
164 node.parent = parent; // 设置父节点
165 node.name = `item_${i}`; // 命名规范
166 node.active = true; // 激活节点
167 }
168
169 // 4. 触发回调(传递索引和节点给调用方处理)
170 callback(i, node);
171 }
172
173 // 5. 复用模式下:隐藏多余节点(超出totalCount的现有节点)
174 if (!cleanAll && parent.children.length > totalCount) {
175 for (let i = totalCount; i < parent.children.length; i++) {
176 parent.children[i].active = false;
177 }
178 }
179 }
180}
讲解181 行
节点事件与列表实例化工具人工通读
UtilityNodeHelper.ts
统一封装按钮点击、Toggle、触摸、滑块和节点销毁等事件绑定,并在点击时播放按钮音效。InstantiateChildren 负责按数量复用或新建子节点,是列表类界面的通用填充入口。
先看 AddClick 的去重与音效处理,再读 InstantiateChildren 的清理、复用、隐藏三步。
特殊处理与易错点
- 易错点AddClick 先 off 同名 click 事件再注册匿名闭包,重复调用只保留最后一次回调。L24
- 易错点DestroyEvent 是自定义字符串 node-destroyed,Cocos 不会自动派发,必须由业务手动 emit 才会触发。L109
- 设计cleanAll 为 true 时销毁全部子节点再重建;为 false 时先把已有子节点全部隐藏再按索引复用。L141
- 边界复用模式需要新建时若 prefab 为空,会打印错误并直接 return,后续索引不再创建。L160
- 边界复用模式下超出 totalCount 的已有节点只是被隐藏,不会被销毁。L174
13注释UtilityNodeHelper:节点相关的实用工具
20方法节点非空时清掉已有 click 监听,再绑定一个按需播放点击音效并调用回调的闭包。
27注释按钮点击间隔要延时150ms,避免重复点击-todo
39方法节点非空时先清空已有 toggle 监听,再绑定接收 Toggle 参数的回调。
53方法节点非空时绑定 TOUCH_START 事件,不做已有监听的清理。
66方法节点非空时绑定 TOUCH_MOVE 事件,不做已有监听的清理。
79方法节点非空时绑定 TOUCH_END 事件,不做已有监听的清理。
92方法节点非空时绑定 TOUCH_CANCEL 事件,不做已有监听的清理。
105方法节点非空时绑定自定义的 node-destroyed 事件,用于业务手动派发的销毁通知。
118方法节点非空时绑定名为 slide 的自定义事件,不是节点原生事件。
133方法按清理或复用策略准备子节点,逐个回调索引与节点,最后把多余节点隐藏。
138属性clean 全部(boolean):该模块的运行态字段。
141注释1. 清理模式:销毁所有现有子节点
145注释2. 复用模式:先隐藏所有现有子节点
150注释3. 实例化/复用节点
153注释复用现有节点
158注释创建新节点
170注释4. 触发回调(传递索引和节点给调用方处理)
174注释5. 复用模式下:隐藏多余节点(超出totalCount的现有节点)