From a80c766ed045e3fecd05ce95e13d0ecd15008832 Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Thu, 4 Jul 2019 14:55:20 +0800 Subject: [PATCH] fix bug (#4813) 1. the mode was trimmed and sprite does not take effect 2. add a tip when ui-model-component does not have modelcomponent --- cocos/3d/ui/components/sprite-component.ts | 21 ++++++++++---------- cocos/3d/ui/components/ui-model-component.ts | 17 ++++++++++++---- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/cocos/3d/ui/components/sprite-component.ts b/cocos/3d/ui/components/sprite-component.ts index 6e6a0f56b5..5777890ac5 100644 --- a/cocos/3d/ui/components/sprite-component.ts +++ b/cocos/3d/ui/components/sprite-component.ts @@ -579,13 +579,13 @@ export class SpriteComponent extends UIRenderComponent { } } - // _onTextureLoaded() { - // if (!this.isValid) { - // return; - // } + private _onTextureLoaded () { + if (!this.isValid) { + return; + } - // this._applySpriteSize(); - // } + this._applySpriteSize(); + } private _applySpriteFrame (oldFrame: SpriteFrame | null) { // if (oldFrame && oldFrame.off) { @@ -601,11 +601,12 @@ export class SpriteComponent extends UIRenderComponent { if (spriteFrame) { if (!oldFrame || spriteFrame !== oldFrame) { // this._material.setProperty('mainTexture', spriteFrame); - this._activateMaterial(); + if (spriteFrame.loaded) { + this._onTextureLoaded(); + } else { + spriteFrame.once('load', this._onTextureLoaded, this); + } } - // else { - // this._applySpriteSize(); - // } } if (CC_EDITOR) { diff --git a/cocos/3d/ui/components/ui-model-component.ts b/cocos/3d/ui/components/ui-model-component.ts index 40767377c0..31f5f53379 100644 --- a/cocos/3d/ui/components/ui-model-component.ts +++ b/cocos/3d/ui/components/ui-model-component.ts @@ -52,14 +52,23 @@ export class UIModelComponent extends UIComponent { private _modelComponent: RenderableComponent | null = null; public onLoad () { - this._modelComponent = this.getComponent(cc.RenderableComponent) as RenderableComponent; - (this._modelComponent as any)._sceneGetter = cc.director.root.ui.getRenderSceneGetter(); + this._modelComponent = this.getComponent('cc.RenderableComponent') as RenderableComponent; + if (!this._modelComponent){ + console.warn(`Use this component on a node(${this.node && this.node.name}) that has a model component`); + return; + } + + this._modelComponent._sceneGetter = cc.director.root.ui.getRenderSceneGetter(); this._modelComponent.recreateModel(); } public onDestroy () { - this._modelComponent = this.getComponent(cc.RenderableComponent) as RenderableComponent; - (this._modelComponent as any)._sceneGetter = null; + this._modelComponent = this.getComponent('cc.RenderableComponent') as RenderableComponent; + if (!this._modelComponent){ + return; + } + + this._modelComponent._sceneGetter = null; this._modelComponent.recreateModel(); }