Skip to content

Commit

Permalink
add CustomMaterialOverride
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangHuan0407 committed Dec 5, 2024
1 parent 305066a commit 0dba22b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
16 changes: 10 additions & 6 deletions DragonBones/src/DragonBones/armature/Armature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -967,12 +967,7 @@ public object replacedTexture
}

this._replacedTexture = value;

foreach (var slot in this._slots)
{
slot.InvalidUpdate();
slot.Update(-1);
}
UpdateSlots();
}
}
/// <inheritDoc/>
Expand Down Expand Up @@ -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);
}
}
}
}
19 changes: 16 additions & 3 deletions Unity/src/DragonBones/Scripts/unity/UnityArmatureComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public class UnityArmatureComponent : DragonBoneEventDispatcher, IArmatureProxy
private bool _disposeProxy = true;
/// <private/>
internal Armature _armature = null;
private Dictionary<Material, Material> _customMaterialOverride;
public Dictionary<Material, Material> CustomMaterialOverride => _customMaterialOverride;

[Tooltip("0 : Loop")]
[Range(0, 100)]
[SerializeField]
Expand Down Expand Up @@ -666,7 +669,8 @@ private bool _IsPrefab()

/// <private/>
void Awake()
{
{
_customMaterialOverride = new Dictionary<Material, Material>();
#if UNITY_EDITOR
if (_IsPrefab())
{
Expand Down Expand Up @@ -707,8 +711,8 @@ void Awake()
_armature.animation.Play(animationName, _playTimes);
}
}


else
Debug.LogError($"_armature is null, name: {name}");
}

void Start()
Expand Down Expand Up @@ -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();
}
}
}
16 changes: 12 additions & 4 deletions Unity/src/DragonBones/Scripts/unity/UnitySlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0dba22b

Please sign in to comment.