From 6f237104c162549d2b9cbcce021e5bb4b306dd62 Mon Sep 17 00:00:00 2001 From: dgflash Date: Wed, 20 Aug 2025 18:11:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ButtonSimple=E3=80=81UIButton?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E8=AE=BE=E7=BD=AE=E8=A7=A6=E6=91=B8=E9=9F=B3?= =?UTF-8?q?=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/libs/gui/button/ButtonSimple.ts | 23 ++++++++++------------- assets/libs/gui/button/UIButton.ts | 11 ++++++++++- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/assets/libs/gui/button/ButtonSimple.ts b/assets/libs/gui/button/ButtonSimple.ts index 6710454..b66216f 100644 --- a/assets/libs/gui/button/ButtonSimple.ts +++ b/assets/libs/gui/button/ButtonSimple.ts @@ -1,6 +1,5 @@ import { AudioClip, Component, EventTouch, Node, _decorator, game } from "cc"; import { oops } from "../../../core/Oops"; -import { resLoader } from "../../../core/common/loader/ResLoader"; const { ccclass, property, menu } = _decorator; @@ -23,10 +22,15 @@ export default class ButtonSimple extends Component { type: AudioClip }) private effect: AudioClip = null!; - // private effectIds: number[] = []; private touchCount = 0; private touchtEndTime = 0; + private static effectPath: string = null!; + /** 批量设置触摸音效 */ + static setBatchEffect(path: string) { + this.effectPath = path; + } + onLoad() { this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this); @@ -56,23 +60,16 @@ export default class ButtonSimple extends Component { /** 短按触摸音效 */ protected async playEffect() { - if (this.effect) { + if (ButtonSimple.effectPath) { + oops.audio.playEffect(ButtonSimple.effectPath); + } + else if (this.effect) { oops.audio.playEffect(this.effect); - // const effectId = await oops.audio.playEffect(this.effect, resLoader.defaultBundleName, () => { - // this.effectIds.remove(effectId); - // }); - // if (effectId > 0) this.effectIds.push(effectId); } } onDestroy() { this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this); - - // if (this.effect) { - // this.effectIds.forEach(effectId => { - // oops.audio.putEffect(effectId, this.effect); - // }); - // } } } diff --git a/assets/libs/gui/button/UIButton.ts b/assets/libs/gui/button/UIButton.ts index 00180da..9ec26c5 100644 --- a/assets/libs/gui/button/UIButton.ts +++ b/assets/libs/gui/button/UIButton.ts @@ -32,6 +32,12 @@ export default class UIButton extends Button { /** 触摸结束时间 */ private _touchEndTime = 0; + private static effectPath: string = null!; + /** 批量设置触摸音效 */ + static setBatchEffect(path: string) { + this.effectPath = path; + } + /** 触摸结束 */ protected _onTouchEnded(event: EventTouch) { if (!this._interactable || !this.enabledInHierarchy) { @@ -74,7 +80,10 @@ export default class UIButton extends Button { /** 短按触摸音效 */ protected playEffect() { - if (this.effect) { + if (UIButton.effectPath) { + oops.audio.playEffect(UIButton.effectPath); + } + else if (this.effect) { oops.audio.playEffect(this.effect); } }