prefab的close优化

prefab里,我添加了一个带btn组件的节点,只要命名为close,点击后就自动关闭当前prefab,而不用写额外的代码,cocos有这样的功能吗。

或者说如果我想实现这样的功能的话,引擎的代码在哪里去改

没有。你可以新建一个带有close功能的组建

写个脚本挂载,实际上没方便多少

这也是个方法。不过没写过自定义组件,得研究下。

就是想优化掉这一步。

修改点击事件监听,判断点击的节点名称,是你定的名字,然后执行你的方法

var dispatchEvent = cc.Node.prototype.dispatchEvent;

    cc.Node.prototype.dispatchEvent = function (event)

    {

        switch (event.type)

        {

            case 'touchstart':

                __dispatchEvent__.call(this, event);

                break;

            case 'touchmove':

                __dispatchEvent__.call(this, event);

                break;

            case 'touchend':

                __dispatchEvent__.call(this, event);

                break;

            case 'touchcancel':

                __dispatchEvent__.call(this, event);

                break;

            default:

                __dispatchEvent__.call(this, event);

        }

    };

压根没有省多少事 以前莫名关闭了还要排查原因 麻烦

谢谢大佬。__dispatchEvent__是未识别,我2.2.2的。

贴一下昨天写的一个脚本组件解决方法。
cc.Class({

extends: cc.Component,

editor:{

    executeInEditMode:true,

    menu:'自定义/close'

},

onLoad(){

    this.node.zIndex = 999;

    this.node.on(cc.Node.EventType.TOUCH_START,()=>{

        this.node.parent.destroy();

    })

}

})

截屏2021-12-03 上午11.52.49
是这个功能吗? :upside_down_face: