• Unity
  • Can I Change attachment color?

  • Uređeno
Related Discussions
...

Hello.

I need change my character's hair color in runtime.

There are many topics changing whole spine's material, but changing partial attachment.
I tried to use SpriteAttacher, but rectangle was appeared and many materials were attached.

How can I change specific attachment color?

Thanks.

Each attachment object has r, g, b and a fields. Those are its color and it is shared across all skeletons that would use its color.

However, changing the color of the slot is recommended if you want to animate the color. Slot also has r, g, b and a.

Note that colors range from 0f to 1f as a float, not 0 to 255 as a byte.

7 dana kasnije

Hello etri,

If you want to change color for specific attachment color, you can simply by:

SkeletonAnimation.skeleton.FindSlot("slot name").SetColor(Color.red);

Let me know if you made it, if you can't figure out I will write a little example for you 😉

22 dana kasnije

When I try to use SetColor it is giving me this error

Assets/Scripts/Units -Base/PlayerControl.cs(105,52): error CS1061: Type Spine.Skeleton' does not contain a definition forSetColor' and no extension method SetColor' of typeSpine.Skeleton' could be found (are you missing a using directive or an assembly reference?)

do you know what I am doing wrong?

make sure you have the using directive

using Spine.Unity;

and are using the latest spine-unity. Though this is a pretty old feature. If all else fails, see my post above.

godina kasnije

@Pharan,

I can get the code to compile, but nothing happens. Is there something wrong in my code? I've got multiple skins on this character, is that affecting anything? :angel:

using Spine.Unity;

public class ChangeHairColor : MonoBehaviour {

   [SerializeField] private SkeletonAnimator _skelAnim;

   // Use this for initialization
   void Start () {
      _skelAnim = GetComponent<SkeletonAnimator> ();
   }
   
// Update is called once per frame void Update () { if(Input.GetKeyDown(KeyCode.Alpha6)){ _skelAnim.skeleton.FindSlot ("hair_attach").SetColor (Color.red); } }

[ UPDATE ] OK, it works if using SkeletonAnimation ... but I'm using SkeletonAnimator.

If you are also keying the color of that slot in the animation, you may need to make sure your code runs AFTER SkeletonAnimator's code.
The last to update always overrides all previous values.

Thanks Pharan, will keep it in mind.

This morning magically that piece of code now works.. So problem solved? :rofl:

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. :p

4 dana kasnije

yes you're right. It's working on certain animations and not on others (idle animation) :x .. Will need to do a bit of research on the Execution Order thing you mentioned.
Thanks

2 godine kasnije

I try this and doesn't work at all. is there any way to change skin color using similar tech?

I just tried the following code successfully:

var slot = skeletonAnimation.Skeleton.FindSlot(slotName);
slot.SetColor(new Color(1, 0, 0, 1));

If you don't see any effect, please check the script update order as described above. Your script shall run after SkeletonAnimation and SkeletonRenderer. If this is already the case, please post the code you are using to set the color.

  • pppr odgovara na ovo.
    godina kasnije

    I have answered to the mentioned thread. In general there is no need to post references to other thread's postings, we will find all new ones.

    4 godine kasnije

    Harald
    Is it possible to change specific slot color of SkeletonGraphic(UI)?
    I tried same method, but didn't happen anything.

    SkeletonGraphic character;
    Skeleton skeleton = character.Skeleton;
    skeleton.FindSlot("hair").SetColor(Color.red);

      pppr It is possible to change the color of the slots even when using the `SkeletonGraphic' component, and the code you tried does not seem to be wrong. To clarify, the following code should produce the results you see in the following video:

      public class SetSlotColor : MonoBehaviour
      {
          public SkeletonGraphic skeletonGraphic;
          
          void Start()
          {
              skeletonGraphic.Skeleton.FindSlot("head").SetColor(Color.red);
          }
      }

      You may want to show us your entire code regarding your skeleton.

      • pppr odgovara na ovo.

        Misaki
        It works now! Thanks a lot!