• Unity
  • How to do Multiple Skins in Script

Related Discussions
...

I'm a complete beginner on Unity myself, but I am wondering if there's a quick and easy way to mix and match Skins to an individual sprite. I tried making the script from the "Combining Skins" section from spine-unity Runtime Documentation: Getting Started but I'm specifically confused what to rename in the first few lines commented.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine;
using Spine.Unity;

public class KinSkin : MonoBehaviour
{

public SkeletonAnimation skeletonAnimation;
void UseRuntimeSkin()
{
// here
        var skeleton = skeletonAnimation.Skeleton;
        var skeletonData = skeleton.data;
// to here
        var mixAndMatchSkin = new Skin("Kin_1");
        mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Arm"));
        mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Chest"));
        mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Ear_Human"));
        mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Head"));
        mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Hips"));
        mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Legs"));
        mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Hair/4c"));
        mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Iris/Iris_7"));
        skeleton.SetSkin(mixAndMatchSkin);
        skeleton.SetSlotsToSetupPose();

}

}

Our Unity guru, Harald, will be along tomorrow, but check this part of the docs (scroll down a little to where it says C#):
spine-unity Runtime Documentation: Life cycle

The code snippet you found for Combining Skins omits this boilerplate:

public class YourComponent : MonoBehaviour {
   SkeletonAnimation skeletonAnimation;
   Skeleton skeleton;

   void Awake () {
      skeletonAnimation = GetComponent<SkeletonAnimation>();
      skeleton = skeletonAnimation.Skeleton;
   }
}

That's likely the part you are missing: you need to add two fields to your class and set them in Awake(), then you can use them in your other methods.

The GetComponent call is Unity API to get a component on the Unity GameObject, so you'll need to be sure you have a SkeletonAnimation on the GameObject you have attached your behavior (script) to.

Hopefully I did not butcher the (weird) Unity terminology. 🙂

Currently, the problem is isolated to the "data" part of skeleton.data, which is saying that it's inaccessible due to protection level. Even after changing it to "Data", the Skins are still not active. Could you review what I might be missing? My goal at the moment is to simply activate a set of Skins to one of my sprite objects.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine;
using Spine.Unity;

public class KinSkin : MonoBehaviour
{

SkeletonAnimation skeletonAnimation;
Skeleton skeleton;

void Awake () {
    skeletonAnimation = GetComponent<SkeletonAnimation>();
    skeleton = skeletonAnimation.Skeleton;
}

void UseRuntimeSkin()
{
    var skeleton = skeletonAnimation.Skeleton;
    var skeletonData = skeleton.data;
    var mixAndMatchSkin = new Skin("Kin_1");
    mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Arm"));
    mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Chest"));
    mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Ear_Human"));
    mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Head"));
    mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Hips"));
    mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Legs"));
    mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Hair/4c"));
    mixAndMatchSkin.AddSkin(skeletonData.FindSkin("a2/Base/Iris/Iris_7"));
    skeleton.SetSkin(mixAndMatchSkin);
    skeleton.SetSlotsToSetupPose();

}

}

Sorry about the lowercase .data field access in the spine-unity documentation page. Accessing it via skeleton.Data is the proper way in recent spine-unity versions. I have just updated the line on the documentation page accordingly, thanks for reporting.

Regarding your skin not showing: I can see nothing obviously wrong with your code. Are you sure that the script is executing without any error, i.e. no exception logged in the Log window?

Apart from that, are you sure that the respective slots are visible in the setup pose?

Please note that you can add triple-backticks before and after code sections to format it properly:

``` 

or surround it with

[code]code goes here

[/code]