实际开发中,经常需要为游戏添加音效和字体文件,这时候一个一个添加效率太低,这时候一键添加就简单许多了,可以节省很多时间
下面贴出代码,目前编辑器版本2.4.x,游戏初始化调用,自取
/**设置按钮点击音效 */
static SetButtonSound(): void {
if (cc.Button.prototype["touchBeganClone"]) return;
cc.Button.prototype["touchBeganClone"] = cc.Button.prototype["_onTouchEnded"];
cc.Button.prototype["_onTouchEnded"] = function (event) {
if (this.interactable && this.enabledInHierarchy) {
// 播放自己的按钮音效
cc.audioEngine.playEffect(PlayAudioMgr.btnClickSound, false);
}
this.touchBeganClone(event);
}
}
/**添加字体 */
public static AddFont(): void {
if (cc.Label.prototype["onLoadClone"]) return;
cc.Label.prototype["onLoadClone"] = cc.Label.prototype["onLoad"];
cc.Label.prototype["onLoad"] = function () {
if (!this.font) {
cc.assetManager.resources.load("Fonts/font", cc.Font, (err, res) => {
if (this.node) {
this.font = res;
this._forceUpdateRenderData(true);
}
})
}
this.onLoadClone();
}
}



