yantar/assets/Scripts/ui/BackButton.ts

30 lines
844 B
TypeScript

// scripts/ui/BackButton.ts
import { _decorator, Component, Button, director, SceneAsset } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('BackButton')
export class BackButton extends Component {
@property({ type: SceneAsset, tooltip: 'Сцена для возврата' })
public targetScene: SceneAsset | null = null;
@property(Button)
public button: Button | null = null;
onLoad() {
this.button?.node?.on('click', this.onBackClick, this);
}
onDestroy() {
// защищённо отписываемся только если node ещё жив
this.button?.node?.off('click', this.onBackClick, this);
}
public onBackClick() {
if (this.targetScene) {
director.loadScene(this.targetScene.name);
} else {
console.warn('BackButton: не задан targetScene');
}
}
}