From a5b45a75b5507abc8c7a7c48bea9587fd7ef4f00 Mon Sep 17 00:00:00 2001 From: SmallMain Date: Tue, 21 Jun 2022 11:42:39 +0800 Subject: [PATCH 07/15] =?UTF-8?q?RichText=20=E6=94=AF=E6=8C=81=E8=87=AA?= =?UTF-8?q?=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 160184a..cc13266 100644 --- a/engine/cocos2d/core/components/CCRichText.js +++ b/engine/cocos2d/core/components/CCRichText.js @@ -351,6 +351,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, @@ -711,6 +751,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': @@ -1006,6 +1057,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; -- 2.32.0 (Apple Git-132)