• Unity
  • changing color on a part of a skin

Hello, i want ot add on my game an object, in wich a part is changing color. (for example, a shield in front of the hero, in wich the logo can change color depending on the element type.

the shield is a skin on the spine charater model, everything is using the base shader, for now.

i know how i would do that for a 3D model (material with a custom shader, multiplying with a mask...) bu ti'm not sure it would work with a spine model.

Any idea how do to it nicely ?

Related Discussions
...

If you only want to color a part of an image or attachment, I would recommend splitting the asset into two. In your case this would be the shield-base and a separate logo that shall be colored.

In the Spine Editor, you could duplicate the Attachments at a certain slot and set the attachment's color to the desired color, as described here:
Regions - Spine User Guide: Color
Then you could switch between the differently colored attachments at a single slot as usual (see the Spine Examples/Other Examples/Goblins for example).

Alternatively, you could set the color of an attachment or the slot via code.
One forum thread that describes this can be found here:
Can I Change attachment color?

Especially note two things:
a) When changing slot color, be sure to set the slot color after the SkeletonAnimation component sets it. As pharan wrote:

Unity can be pretty random about deciding which script should run first, so until you actually specify it in Unity's Script Execution Order settings, you may run into it magically working and not working in between Unity sessions.

b) When changing attachment color, be sure to create a clone of the colored parts first, otherwise all attachments of the same image (e.g. all shields) will be colored!

You can have a look at the following simple example:

using System.Collections;
using UnityEngine;
using Spine;
using Spine.Unity;
using Spine.Unity.Modules.AttachmentTools;

public class SetAttachmentColorTest : MonoBehaviour {

   SkeletonAnimation skeletonAnimation;
   
IEnumerator Start () { skeletonAnimation = this.GetComponent<SkeletonAnimation>(); int slotIndex = skeletonAnimation.Skeleton.FindSlotIndex("left hand item"); Attachment attachment = skeletonAnimation.Skeleton.GetAttachment(slotIndex, "spear"); // Note: if we changed the original attachment, all spears in the world would be colored - therefore we clone our own instance. Attachment coloredClone = attachment.GetClone(false); var slot = skeletonAnimation.Skeleton.Slots.Items[slotIndex]; slot.Attachment = coloredClone; yield return new WaitForSeconds(1); ChangeAttachmentColor(coloredClone, Color.red); yield return new WaitForSeconds(1); ChangeAttachmentColor(coloredClone, Color.green); } void ChangeAttachmentColor(Attachment attachment, Color color) { RegionAttachment regionAttachment = attachment as RegionAttachment; if (regionAttachment != null) { regionAttachment.SetColor(color); } MeshAttachment meshAttachment = attachment as MeshAttachment; if (meshAttachment != null) { meshAttachment.SetColor(color); } } }

If you need a more sophisticated item system, then I would suggest digging deeper into Skins. Then I would recommend having a look at the Spine Examples/OtherExamples/Mix and Match Equip example scene and the EquipsVisualsComponentExample.cs script.

Thanks a lot,

You're welcome. 🙂


One more thing that came to my mind, mentioning for the sake of completeness:
You could maybe consider using two-color-tinting, depending on the part that needs coloring. It could be suitable for coloring some parts.
See this documentation page here: Attachments - Spine User Guide.