Switcher.ts 890 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { _decorator, Button, Component, Node } from 'cc';
  2. const { ccclass, property, requireComponent, executeInEditMode } = _decorator;
  3. @ccclass('UIExtend/Switcher')
  4. @requireComponent(Button)
  5. @executeInEditMode(true)
  6. export class Switcher extends Component {
  7. @property(Node)
  8. on: Node = null
  9. @property(Node)
  10. off: Node = null
  11. private _isOn: boolean = true
  12. @property
  13. public get IsOn(): boolean {
  14. return this._isOn
  15. }
  16. public set IsOn(v: boolean) {
  17. this._isOn = v;
  18. if (this.on) this.on.active = v
  19. if (this.off) this.off.active = !v
  20. }
  21. protected onLoad(): void {
  22. this.node.on(Button.EventType.CLICK, this.onClick, this)
  23. }
  24. protected onDestroy(): void {
  25. this.node.off(Button.EventType.CLICK, this.onClick, this)
  26. }
  27. protected onClick(): void {
  28. this.IsOn = !this.IsOn
  29. }
  30. }