最近在做一个组件的时候,遇到了需要监听 node 上有新组件添加,删除,顺序改变消息的需求(只需要在编辑器中监听)。
找了下代码,找到了
/**
* !#en The event type for color change events.
* Performance note, this event will be triggered every time corresponding properties being changed,
* if the event callback have heavy logic it may have great performance impact, try to avoid such scenario.
* !#zh 当节点颜色改变时触发的事件。
* 性能警告:这个事件会在每次对应的属性被修改时触发,如果事件回调损耗较高,有可能对性能有很大的负面影响,请尽量避免这种情况。
* @property {String} COLOR_CHANGED
* @static
*/
COLOR_CHANGED: 'color-changed',
/**
* !#en The event type for new child added events.
* !#zh 当新的子节点被添加时触发的事件。
* @property {String} CHILD_ADDED
* @static
*/
CHILD_ADDED: 'child-added',
/**
* !#en The event type for child removed events.
* !#zh 当子节点被移除时触发的事件。
* @property {String} CHILD_REMOVED
* @static
*/
CHILD_REMOVED: 'child-removed',
/**
* !#en The event type for children reorder events.
* !#zh 当子节点顺序改变时触发的事件。
* @property {String} CHILD_REORDER
* @static
*/
CHILD_REORDER: 'child-reorder',
/**
* !#en The event type for node group changed events.
* !#zh 当节点归属群组发生变化时触发的事件。
* @property {String} GROUP_CHANGED
* @static
*/
GROUP_CHANGED: 'group-changed',
/**
* !#en The event type for node's sibling order changed.
* !#zh 当节点在兄弟节点中的顺序发生变化时触发的事件。
* @property {String} SIBLING_ORDER_CHANGED
* @static
*/
SIBLING_ORDER_CHANGED: 'sibling-order-changed',
但是这些是 node 变化时候的消息。并不是组件相关的消息。
然后有找打了
“scene:move-up-component”
“scene:move-down-component”
“scene:reset-component”
“scene:copy-component”
“scene:paste-component”
“scene:move-nodes” //移动node
“scene:delete-nodes”//删除node
这些是 Editor.Ipc 的东西,于是试了下在代码中写
start () {
if (CC_EDITOR) {
let ipcListener = new Editor.IpcListener ();
ipcListener.on("scene:move-nodes", function() {
cc.log("scene:move-nodes");
});
}
}
函数调用并没有报错,但是回调也没有执行(我这个代码是在组件脚本里写的,而不是插件脚本里)。
想问下各位大佬有什么好的解决方案吗?