• RuntimesUnity
  • Making skin presets in Scriptable objects with using [SpineSkin] Attribute

Hello!

I wanna make a bunch of skin presets for my units, and i wanna use SO for this purpose, but I can't use [SpineSkin] attrbute in SO because it's ask for a reference to SkeletonDataAsset. Is thre any workaround or another way to use [SpineSkin] attribute in SO? Thanks!

    Related Discussions
    ...

    To create skin presets using ScriptableObjects and the [SpineSkin] attribute, you need to reference the SkeletonDataAsset in your ScriptableObject. Here’s a step-by-step guide on how to achieve this:

    1. Create a ScriptableObject Class:
      Define a ScriptableObject class that holds a reference to the SkeletonDataAsset and the skin name.

         using UnityEngine;
         using Spine.Unity;
      
         [CreateAssetMenu(fileName = "SkinPreset", menuName = "Spine/SkinPreset")]
         public class SkinPreset : ScriptableObject
         {
             public SkeletonDataAsset skeletonDataAsset;
             [SpineSkin(dataField: "skeletonDataAsset")]
             public string skinName;
         }
    2. Create Skin Presets:
      Create instances of the SkinPreset ScriptableObject in the Unity Editor. Assign the SkeletonDataAsset and select the desired skin using the [SpineSkin] attribute.

    3. Apply Skin Presets at Runtime:
      Use the SkinPreset instances to set the skin on your skeleton at runtime.

         using UnityEngine;
         using Spine.Unity;
      
         public class ApplySkinPreset : MonoBehaviour
         {
             public SkeletonAnimation skeletonAnimation;
             public SkinPreset skinPreset;
      
             void Start()
             {
                 ApplySkin();
             }
      
             void ApplySkin()
             {
                 if (skeletonAnimation == null || skinPreset == null) return;
      
                 var skeleton = skeletonAnimation.Skeleton;
                 var skin = skinPreset.skeletonDataAsset.GetSkeletonData(true).FindSkin(skinPreset.skinName);
                 if (skin != null)
                 {
                     skeleton.SetSkin(skin);
                     skeleton.SetSlotsToSetupPose();
                     skeletonAnimation.AnimationState.Apply(skeleton);
                 }
             }
         }

    This approach allows you to create and manage skin presets using ScriptableObjects while leveraging the [SpineSkin] attribute to select skins conveniently in the Unity Editor.

    dreding Is thre any workaround or another way to use [SpineSkin] attribute in SO? Thanks!

    The [SpineSkin] attribute requires a SkeletonDataAsset to populate the popup menu accordingly with suitable skin names. If you want to avoid storing a reference to SkeletonDataAsset, you would have to omit [SpineSkin] and just use a normal string.

      Harald No problem with storing skeleton data, I just didn't know about put skeleton as data field possibility. Thanks a lot!