import { _decorator, Component, Node, tween, Vec3, UITransform, log } from 'cc'; const { ccclass, property } = _decorator; @ccclass('PopupEffect') export class PopupEffect extends Component { @property public popupDuration: number = 0.5; // 弹出动画的持续时间 @property public targetHeight: number = 300; // 弹出目标的高度(终点位置的Y坐标) private initialPosition: Vec3 | null = null; // 初始位置 (隐藏状态) public hiddenPositionY: any; awake() { // this.hiddenPositionY = this.node.position.y; // log('高度:', this.hiddenPositionY); } start() { // 获取UITransform组件来确定节点和父节点的尺寸 const uiTransform = this.node.getComponent(UITransform); const parentTransform = this.node.parent?.getComponent(UITransform); if (uiTransform && parentTransform) { // 计算隐藏状态的位置(屏幕底部外) //const hiddenPositionY = -parentTransform.height / 2 - uiTransform.height / 2; this.hiddenPositionY = this.node.position.y; this.initialPosition = new Vec3(this.node.position.x, this.hiddenPositionY, this.node.position.z); // 记录初始隐藏位置 // 设置初始位置为隐藏状态 this.node.setPosition(this.initialPosition); // 调用弹出效果 //this.showPopup(); } } // 弹出效果:从底部平滑移动到指定高度 public showPopup() { log('目标高度:', this.targetHeight); if (this.initialPosition) { // 使用tween动画,从隐藏状态移动到目标高度 tween(this.node) .to(this.popupDuration, { position: new Vec3(this.initialPosition.x, this.targetHeight, this.initialPosition.z) }) .start(); } } // 恢复到初始隐藏位置 public hidePopup() { log('隐藏位置:', this.initialPosition); if (this.initialPosition) { // 使用tween动画,将节点移回到初始隐藏位置 log('隐藏位置:', this.hiddenPositionY); tween(this.node) .to(this.popupDuration, { position: this.initialPosition }) .start(); } } }