添加ButtonSimple、UIButton批量设置触摸音效

This commit is contained in:
dgflash
2025-08-20 18:11:58 +08:00
parent 32562ae2d4
commit 6f237104c1
2 changed files with 20 additions and 14 deletions

View File

@@ -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);
// });
// }
}
}

View File

@@ -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);
}
}