import { _decorator, Button, Component, Node, Sprite, SpriteFrame, Toggle } from 'cc'; import { AudioMgr } from '../audio/AudioMgr'; import { AssetController } from '../../AssetController'; const { ccclass, property } = _decorator; @ccclass('UISwitcher') export class UISwitcher extends Component { toggle: Button value: boolean sprite: Sprite; @property({ type: SpriteFrame }) onSelect: SpriteFrame = null; @property({ type: SpriteFrame }) onNoSelect: SpriteFrame = null; protected onLoad(): void { this.toggle = this.getComponent(Button) this.sprite = this.getComponent(Sprite) } start() { this.toggle.node.on(Button.EventType.CLICK, this.onClickToggleBtn, this); } onSwitchCallBack addCallBack(callBack) { this.onSwitchCallBack = callBack } SetValue(v: boolean) { this.value = v this.switchSprite(this.value) } switchSprite(v: boolean) { if (v) { this.sprite.spriteFrame = this.onSelect } else { this.sprite.spriteFrame = this.onNoSelect } } onClickToggleBtn() { AudioMgr.inst.playOneShot(AssetController.instance.click) // AudioMgr.inst.playOneShot(AssetController.instance.buttonClip) this.value = !this.value this.switchSprite(this.value) if (this.onSwitchCallBack != null) { this.onSwitchCallBack(this.value) } } }