const { ccclass, menu, requireComponent } = cc._decorator; @ccclass // @menu("Plug-in/EditBox/EditboxEx") @requireComponent(cc.EditBox) export default class EditboxEx extends cc.Component { //#region private private _worldPos: cc.Vec2 = null; //#endregion //#region Lifecycle protected onLoad(): void { if (CC_DEV || cc.sys.isNative) { let editbox: cc.EditBox = this.node.getComponent(cc.EditBox); this._worldPos = this.node.GetWorldPosition(); editbox.node.on("editing-did-began", this._onEditDidBegan, this); editbox.node.on("editing-did-ended", this._onEditDidEnded, this); } } //#endregion //#region EventListener private _onEditDidBegan(editbox: cc.EditBox, customEventData: string): void { let winSizeHeight: number = cc.winSize.height; let nodeSizeHeight: number = this.node.height / 2; let targetHeight: number = winSizeHeight - nodeSizeHeight; let worldPos: cc.Vec2 = cc.v2(this.node.GetWorldPosition().x, targetHeight); this.node.SetWorldPosition(worldPos); } // 假设这个回调是给 editingDidEnded 事件的 private _onEditDidEnded(editbox: cc.EditBox, customEventData: string): void { this.node.SetWorldPosition(this._worldPos); } //#endregion }