Skip to content

Commit

Permalink
Fix the UIOpacity issue when the node is re-added to the scene. (#17700)
Browse files Browse the repository at this point in the history
* fix ui-opacity

fix the render.node._uiProps.localOpacity only update on native.

* Add JSB Check in _parentChanged function

The setEntityLocalOpacityDirtyRecursively function should not work on web platform.

* fix the opacity issue on native

Whenever the node is re-added to the scene, the _parentOpacity value needs to be updated again in the onEnable callback.

* Remove the JSB checking in setEntityLocalOpacityDirtyRecursively function

* Update onEnable function
  • Loading branch information
yoki0805 authored Oct 12, 2024
1 parent 186fa0f commit 2ec9087
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cocos/2d/components/ui-opacity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class UIOpacity extends Component {
*/
private _parentOpacity: number = 1.0;

private _parentOpacityResetFlag: boolean = true;

/**
* @en
* The transparency value of the impact.
Expand Down Expand Up @@ -97,7 +99,7 @@ export class UIOpacity extends Component {
// }
// UIRenderer.setEntityColorDirtyRecursively(this.node, dirty);

UIOpacity.setEntityLocalOpacityDirtyRecursively(this.node, dirty, 1, false);
UIOpacity.setEntityLocalOpacityDirtyRecursively(this.node, dirty, this._parentOpacity, false);
}
}

Expand Down Expand Up @@ -194,11 +196,16 @@ export class UIOpacity extends Component {
}

protected _parentChanged (): void {
if (!JSB) {
return;
}
const parent = this.node.getParent();
let opacity = 1;
if (parent) {
this._parentOpacity = this._getParentOpacity(parent);
opacity = this._parentOpacity;
} else {
this._parentOpacityResetFlag = true;
}
UIOpacity.setEntityLocalOpacityDirtyRecursively(this.node, true, opacity, false);
}
Expand All @@ -224,7 +231,12 @@ export class UIOpacity extends Component {
public onEnable (): void {
this.node.on(NodeEventType.PARENT_CHANGED, this._parentChanged, this);
this.node._uiProps.localOpacity = this._parentOpacity * this._opacity / 255;
this._setEntityLocalOpacityRecursively(this.node._uiProps.localOpacity);
if (this._parentOpacityResetFlag) {
this._parentChanged();
this._parentOpacityResetFlag = false;
} else {
this._setEntityLocalOpacityRecursively(this.node._uiProps.localOpacity);
}
}

public onDisable (): void {
Expand Down

0 comments on commit 2ec9087

Please sign in to comment.