From 305066ade56ebcb1456fea1a075aa8ced09d190e Mon Sep 17 00:00:00 2001 From: Zhang Huan <2360950578@qq.com> Date: Thu, 5 Dec 2024 12:41:30 +0800 Subject: [PATCH 1/2] optimize color calculation --- .../DragonBones/Scripts/unity/UnitySlot.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Unity/src/DragonBones/Scripts/unity/UnitySlot.cs b/Unity/src/DragonBones/Scripts/unity/UnitySlot.cs index 762cb8f..dec8807 100644 --- a/Unity/src/DragonBones/Scripts/unity/UnitySlot.cs +++ b/Unity/src/DragonBones/Scripts/unity/UnitySlot.cs @@ -430,13 +430,15 @@ protected override void _UpdateColor() if (this._isCombineMesh) { var meshBuffer = this._combineMesh.meshBuffers[this._sumMeshIndex]; + Color32 color32 = default; + color32.r = (byte)(_colorTransform.redMultiplier * proxyTrans.redMultiplier * 255); + color32.g = (byte)(_colorTransform.greenMultiplier * proxyTrans.greenMultiplier * 255); + color32.b = (byte)(_colorTransform.blueMultiplier * proxyTrans.blueMultiplier * 255); + color32.a = (byte)(_colorTransform.alphaMultiplier * proxyTrans.alphaMultiplier * 255); for (var i = 0; i < this._meshBuffer.vertexBuffers.Length; i++) { var index = this._verticeOffset + i; - this._meshBuffer.color32Buffers[i].r = (byte)(_colorTransform.redMultiplier * proxyTrans.redMultiplier * 255); - this._meshBuffer.color32Buffers[i].g = (byte)(_colorTransform.greenMultiplier * proxyTrans.greenMultiplier * 255); - this._meshBuffer.color32Buffers[i].b = (byte)(_colorTransform.blueMultiplier * proxyTrans.blueMultiplier * 255); - this._meshBuffer.color32Buffers[i].a = (byte)(_colorTransform.alphaMultiplier * proxyTrans.alphaMultiplier * 255); + this._meshBuffer.color32Buffers[i] = color32; // meshBuffer.color32Buffers[index] = this._meshBuffer.color32Buffers[i]; } @@ -445,12 +447,14 @@ protected override void _UpdateColor() } else if (this._meshBuffer.sharedMesh != null) { + Color32 color32 = default; + color32.r = (byte)(_colorTransform.redMultiplier * proxyTrans.redMultiplier * 255); + color32.g = (byte)(_colorTransform.greenMultiplier * proxyTrans.greenMultiplier * 255); + color32.b = (byte)(_colorTransform.blueMultiplier * proxyTrans.blueMultiplier * 255); + color32.a = (byte)(_colorTransform.alphaMultiplier * proxyTrans.alphaMultiplier * 255); for (int i = 0, l = this._meshBuffer.sharedMesh.vertexCount; i < l; ++i) { - this._meshBuffer.color32Buffers[i].r = (byte)(_colorTransform.redMultiplier * proxyTrans.redMultiplier * 255); - this._meshBuffer.color32Buffers[i].g = (byte)(_colorTransform.greenMultiplier * proxyTrans.greenMultiplier * 255); - this._meshBuffer.color32Buffers[i].b = (byte)(_colorTransform.blueMultiplier * proxyTrans.blueMultiplier * 255); - this._meshBuffer.color32Buffers[i].a = (byte)(_colorTransform.alphaMultiplier * proxyTrans.alphaMultiplier * 255); + this._meshBuffer.color32Buffers[i] = color32; } // this._meshBuffer.UpdateColors(); From 0dba22bdc830c94ba574dcc875578a6a217e4ce2 Mon Sep 17 00:00:00 2001 From: Zhang Huan <2360950578@qq.com> Date: Thu, 5 Dec 2024 13:09:11 +0800 Subject: [PATCH 2/2] add CustomMaterialOverride --- .../src/DragonBones/armature/Armature.cs | 16 ++++++++++------ .../Scripts/unity/UnityArmatureComponent.cs | 19 ++++++++++++++++--- .../DragonBones/Scripts/unity/UnitySlot.cs | 16 ++++++++++++---- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/DragonBones/src/DragonBones/armature/Armature.cs b/DragonBones/src/DragonBones/armature/Armature.cs index 3d2f476..12aa07d 100644 --- a/DragonBones/src/DragonBones/armature/Armature.cs +++ b/DragonBones/src/DragonBones/armature/Armature.cs @@ -967,12 +967,7 @@ public object replacedTexture } this._replacedTexture = value; - - foreach (var slot in this._slots) - { - slot.InvalidUpdate(); - slot.Update(-1); - } + UpdateSlots(); } } /// @@ -1025,6 +1020,15 @@ public WorldClock clock public Slot parent { get { return this._parent; } + } + + internal void UpdateSlots() + { + foreach (var slot in this._slots) + { + slot.InvalidUpdate(); + slot.Update(-1); + } } } } diff --git a/Unity/src/DragonBones/Scripts/unity/UnityArmatureComponent.cs b/Unity/src/DragonBones/Scripts/unity/UnityArmatureComponent.cs index b01cb76..6ea931a 100644 --- a/Unity/src/DragonBones/Scripts/unity/UnityArmatureComponent.cs +++ b/Unity/src/DragonBones/Scripts/unity/UnityArmatureComponent.cs @@ -99,6 +99,9 @@ public class UnityArmatureComponent : DragonBoneEventDispatcher, IArmatureProxy private bool _disposeProxy = true; /// internal Armature _armature = null; + private Dictionary _customMaterialOverride; + public Dictionary CustomMaterialOverride => _customMaterialOverride; + [Tooltip("0 : Loop")] [Range(0, 100)] [SerializeField] @@ -666,7 +669,8 @@ private bool _IsPrefab() /// void Awake() - { + { + _customMaterialOverride = new Dictionary(); #if UNITY_EDITOR if (_IsPrefab()) { @@ -707,8 +711,8 @@ void Awake() _armature.animation.Play(animationName, _playTimes); } } - - + else + Debug.LogError($"_armature is null, name: {name}"); } void Start() @@ -819,5 +823,14 @@ public void CloseCombineMeshs() } } } + + public void SetMaterialOverride(Material material, Material materialOverride) + { + if (materialOverride != null) + _customMaterialOverride[material] = materialOverride; + else + _customMaterialOverride.Remove(material); + _armature.UpdateSlots(); + } } } \ No newline at end of file diff --git a/Unity/src/DragonBones/Scripts/unity/UnitySlot.cs b/Unity/src/DragonBones/Scripts/unity/UnitySlot.cs index dec8807..07e113b 100644 --- a/Unity/src/DragonBones/Scripts/unity/UnitySlot.cs +++ b/Unity/src/DragonBones/Scripts/unity/UnitySlot.cs @@ -397,12 +397,18 @@ internal override void _UpdateBlendMode() if (this._childArmature == null) { if (this._uiDisplay != null) - { - this._uiDisplay.material = (this._textureData as UnityTextureData).GetMaterial(this._blendMode, true); + { + Material material = (this._textureData as UnityTextureData).GetMaterial(this._blendMode, true); + if (_proxy.CustomMaterialOverride.TryGetValue(material, out Material materialOverride)) + material = materialOverride; + this._uiDisplay.material = material; } else { - this._meshRenderer.sharedMaterial = (this._textureData as UnityTextureData).GetMaterial(this._blendMode); + Material material = (this._textureData as UnityTextureData).GetMaterial(this._blendMode); + if (_proxy.CustomMaterialOverride.TryGetValue(material, out Material materialOverride)) + material = materialOverride; + this._meshRenderer.sharedMaterial = material; } this._meshBuffer.name = this._uiDisplay != null ? this._uiDisplay.material.name : this._meshRenderer.sharedMaterial.name; @@ -479,7 +485,9 @@ protected override void _UpdateFrame() this._isActive = false; if (this._displayIndex >= 0 && this._display != null && currentTextureData != null) { - var currentTextureAtlas = _proxy.isUGUI ? currentTextureAtlasData.uiTexture : currentTextureAtlasData.texture; + Material currentTextureAtlas = _proxy.isUGUI ? currentTextureAtlasData.uiTexture : currentTextureAtlasData.texture; + if (_proxy.CustomMaterialOverride.TryGetValue(currentTextureAtlas, out Material materialOverride)) + currentTextureAtlas = materialOverride; if (currentTextureAtlas != null) { this._isActive = true;