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:
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;
}
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.
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.