diff --git a/cocos/3d/ui/components/widget-component.ts b/cocos/3d/ui/components/widget-component.ts index 7a2316c9e4..fd161bb518 100644 --- a/cocos/3d/ui/components/widget-component.ts +++ b/cocos/3d/ui/components/widget-component.ts @@ -616,17 +616,19 @@ export class WidgetComponent extends Component { this._recursiveDirty(); } - protected _registerTargetEvents (){ - if (this._target){ - this._target.on(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this); - this._target.on(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this); + protected _registerTargetEvents () { + const target = this._target || this.node.parent; + if (target){ + target.on(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this); + target.on(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this); } } protected _unregisterTargetEvents () { - if (this._target) { - this._target.off(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this); - this._target.off(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this); + const target = this._target || this.node.parent; + if (target) { + target.off(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this); + target.off(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this); } } diff --git a/cocos/3d/ui/components/widget-manager.ts b/cocos/3d/ui/components/widget-manager.ts index f457e4a1e3..39587ecc26 100644 --- a/cocos/3d/ui/components/widget-manager.ts +++ b/cocos/3d/ui/components/widget-manager.ts @@ -178,6 +178,8 @@ function align (node: Node, widget: WidgetComponent) { x = localRight + (anchorX - 1) * width; } } + + widget._lastSize.width = width; } if (widget.alignFlags & AlignFlags.VERTICAL) { @@ -236,6 +238,8 @@ function align (node: Node, widget: WidgetComponent) { y = localTop + (anchorY - 1) * height; } } + + widget._lastSize.height = height; } node.setPosition(x, y, _tempPos.z); @@ -375,7 +379,6 @@ function adjustWidgetToAllowMovingInEditor (this: WidgetComponent, eventType: Sy const oldPos = this._lastPos; const delta = new Vec3(newPos); delta.subtract(oldPos); - oldPos.set(newPos); let target = self.node.parent!; const inverseScale = new Vec3(1, 1, 1); @@ -427,7 +430,6 @@ function adjustWidgetToAllowResizingInEditor (this: WidgetComponent/*, oldSize: const newSize = self.node.getContentSize(); const oldSize = this._lastSize; const delta = new Vec3(newSize.width - oldSize.width, newSize.height - oldSize.height, 0); - oldSize.set(newSize); let target = self.node.parent!; const inverseScale = new Vec3(1, 1, 1); @@ -458,6 +460,11 @@ function adjustWidgetToAllowResizingInEditor (this: WidgetComponent/*, oldSize: } } +// 节点被父节点或者 target 的尺寸影响而重新更新 +function adjustWidgetToAnchorChanged (this: WidgetComponent) { + this.setDirty(); +} + const activeWidgets: WidgetComponent[] = []; // updateAlignment from scene to node recursively @@ -515,6 +522,7 @@ export const widgetManager = cc._widgetManager = { } widget.node.on(SystemEventType.TRANSFORM_CHANGED, adjustWidgetToAllowMovingInEditor, widget); widget.node.on(SystemEventType.SIZE_CHANGED, adjustWidgetToAllowResizingInEditor, widget); + widget.node.on(SystemEventType.ANCHOR_CHANGED, adjustWidgetToAnchorChanged, widget); // } }, remove (widget: WidgetComponent) { @@ -522,6 +530,7 @@ export const widgetManager = cc._widgetManager = { // if (CC_EDITOR && !cc.engine.isPlaying) { widget.node.off(SystemEventType.TRANSFORM_CHANGED, adjustWidgetToAllowMovingInEditor, widget); widget.node.off(SystemEventType.SIZE_CHANGED, adjustWidgetToAllowResizingInEditor, widget); + widget.node.off(SystemEventType.ANCHOR_CHANGED, adjustWidgetToAnchorChanged, widget); // } }, onResized () {