From d2f79a0fab375511174408c335d2ae9241c8a6ca Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Thu, 30 May 2019 11:33:38 +0800 Subject: [PATCH] fix ui performance (#4511) 1. label ui drawcall too much high 2. switch visibility when parent changed --- cocos/3d/ui/components/ui-component.ts | 43 ++++++++++++++++--- cocos/3d/ui/components/ui-render-component.ts | 8 +--- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/cocos/3d/ui/components/ui-component.ts b/cocos/3d/ui/components/ui-component.ts index 12fb25f719..b49943044e 100644 --- a/cocos/3d/ui/components/ui-component.ts +++ b/cocos/3d/ui/components/ui-component.ts @@ -1,6 +1,8 @@ import { Component } from '../../../components'; +import { SystemEventType } from '../../../core'; import { ccclass, executeInEditMode, executionOrder, property } from '../../../core/data/class-decorator'; import { UI } from '../../../renderer/ui/ui'; +import { Node } from '../../../scene-graph'; import { CanvasComponent } from './canvas-component'; /** @@ -42,7 +44,35 @@ export class UIComponent extends Component { protected _visibility = -1; + private _lastParent: Node | null = null; public onEnable () { + this._lastParent = this.node.parent; + this._updateVisibility(); + if (this._lastParent) { + this.node.on(SystemEventType.CHILD_REMOVED, this._parentChanged, this); + } + + } + + public onDisable () { + this._cancelEventFromParent(); + } + + public updateAssembler (render: UI) { + } + + public postUpdateAssembler (render: UI) { + } + + private _parentChanged (node: Node){ + if (node === this.node) { + this._updateVisibility(); + this._cancelEventFromParent(); + this._lastParent = this.node.parent; + } + } + + private _updateVisibility () { let parent = this.node; // 获取被渲染相机的 visibility while (parent) { @@ -59,13 +89,12 @@ export class UIComponent extends Component { } } - public onDisable () { + private _cancelEventFromParent (){ + if (this._lastParent) { + this._lastParent.off(SystemEventType.CHILD_REMOVED, this._parentChanged, this); + this._lastParent = null; + } + this._visibility = -1; } - - public updateAssembler (render: UI) { - } - - public postUpdateAssembler (render: UI) { - } } diff --git a/cocos/3d/ui/components/ui-render-component.ts b/cocos/3d/ui/components/ui-render-component.ts index 43ace9a2cf..26be4bcd56 100644 --- a/cocos/3d/ui/components/ui-render-component.ts +++ b/cocos/3d/ui/components/ui-render-component.ts @@ -234,7 +234,7 @@ export class UIRenderComponent extends UIComponent { public onEnable () { super.onEnable(); this.node.on(SystemEventType.ANCHOR_CHANGED, this._nodeStateChange, this); - this.node.on(SystemEventType.TRANSFORM_CHANGED, this._nodeStateChange, this); + // this.node.on(SystemEventType.TRANSFORM_CHANGED, this._nodeStateChange, this); this.node.on(SystemEventType.SIZE_CHANGED, this._nodeStateChange, this); // if (this.node._renderComponent) { // this.node._renderComponent.enabled = false; @@ -249,7 +249,7 @@ export class UIRenderComponent extends UIComponent { // this.node._renderComponent = null; // this.disableRender(); this.node.off(SystemEventType.ANCHOR_CHANGED, this._nodeStateChange, this); - this.node.off(SystemEventType.TRANSFORM_CHANGED, this._nodeStateChange, this); + // this.node.off(SystemEventType.TRANSFORM_CHANGED, this._nodeStateChange, this); this.node.off(SystemEventType.SIZE_CHANGED, this._nodeStateChange, this); } @@ -328,10 +328,6 @@ export class UIRenderComponent extends UIComponent { return false; } - if (this.node.hasChanged && !this._renderDataDirty) { - this.markForUpdateRenderData(); - } - this._checkAndUpdateRenderData(); return true; }