From 9447c862bfc8f30d17b95a53deb68ff5d1df25b8 Mon Sep 17 00:00:00 2001 From: SmallMain Date: Sat, 25 Jun 2022 00:49:07 +0800 Subject: [PATCH] =?UTF-8?q?[engine]=20RichText=20=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89=E6=9D=90=E8=B4=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- engine/cocos2d/core/components/CCRichText.js | 62 ++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/engine/cocos2d/core/components/CCRichText.js b/engine/cocos2d/core/components/CCRichText.js index 5126c555..f056bc58 100644 --- a/engine/cocos2d/core/components/CCRichText.js +++ b/engine/cocos2d/core/components/CCRichText.js @@ -352,6 +352,46 @@ let RichText = cc.Class({ } }, + /** + * 自定义内部使用的材质 + */ + customMaterial: { + default: null, + type: cc.Material, + notify: function (oldValue) { + if (this.customMaterial === oldValue) return; + const material = this.customMaterial == null ? this._getDefaultMaterial() : this.customMaterial; + for (let i = 0; i < this._labelSegments.length; i++) { + const labelComponent = this._labelSegments[i].getComponent(cc.Label); + if (labelComponent) { + if (labelComponent._materials.length === 0) { + labelComponent._materials[0] = MaterialVariant.create(material, labelComponent); + } else { + labelComponent.setMaterial(0, material); + } + } + const spriteComponent = this._labelSegments[i].getComponent(cc.Sprite); + if (spriteComponent) { + if (spriteComponent._materials.length === 0) { + spriteComponent._materials[0] = MaterialVariant.create(material, spriteComponent); + } else { + spriteComponent.setMaterial(0, material); + } + } + } + for (let i = 0; i < this._labelSegmentsCache.length; i++) { + const labelComponent = this._labelSegmentsCache[i].getComponent(cc.Label); + if (labelComponent) { + if (labelComponent._materials.length === 0) { + labelComponent._materials[0] = MaterialVariant.create(material, labelComponent); + } else { + labelComponent.setMaterial(0, material); + } + } + } + } + }, + autoSwitchMaterial: { type: RenderComponent.EnableType, default: RenderComponent.EnableType.GLOBAL, @@ -708,6 +748,17 @@ let RichText = cc.Class({ spriteComponent.autoSwitchMaterial = this.autoSwitchMaterial; spriteComponent.allowDynamicAtlas = this.allowDynamicAtlas; + // 更新材质 + if (this.customMaterial) { + if (spriteComponent._materials.length === 0) { + spriteComponent._materials[0] = MaterialVariant.create(this.customMaterial, spriteComponent); + } else { + if (spriteComponent._materials[0].material !== this.customMaterial) { + spriteComponent.setMaterial(0, this.customMaterial); + } + } + } + switch (richTextElement.style.imageAlign) { case 'top': @@ -1003,6 +1054,17 @@ let RichText = cc.Class({ labelComponent.autoSwitchMaterial = this.autoSwitchMaterial; labelComponent.allowDynamicAtlas = this.allowDynamicAtlas; + // 更新材质 + if (this.customMaterial) { + if (labelComponent._materials.length === 0) { + labelComponent._materials[0] = MaterialVariant.create(this.customMaterial, labelComponent); + } else { + if (labelComponent._materials[0].material !== this.customMaterial) { + labelComponent.setMaterial(0, this.customMaterial); + } + } + } + let isAsset = this.font instanceof cc.Font; if (isAsset && !this._isSystemFontUsed) { labelComponent.font = this.font;