按钮音效

请问,如果要在cocos creator里给按钮添加音效,除了在按钮点击事件里手动调用AudioSource播放音乐外,有没有有没有不写代码的方法?

自己写组件继承按钮组件,把音效作为一个属性,然后在点击事件时加入播放音效代码,一次编写

1赞

cc.Class({
extends: cc.Button,
properties: {
clip: {
default: null,
url: cc.AudioClip
},
},
_onTouchEnded: function (event) {
if (!this.interactable || !this.enabledInHierarchy) return;

             if (this._pressed) {
                 cc.audioEngine.play(this.clip, false, 1);
                 cc.Component.EventHandler.emitEvents(this.clickEvents, event);
                 this.node.emit('click', this);
             }
             this._pressed = false;
             this._updateState();
             event.stopPropagation();
         },
     });
1赞

学到了,谢谢老哥