Skip to content

Instantly share code, notes, and snippets.

@grimmdev
Last active August 11, 2020 22:25
Show Gist options
  • Save grimmdev/13248b04869cf7ab05c66e34fe5d63ea to your computer and use it in GitHub Desktop.
Save grimmdev/13248b04869cf7ab05c66e34fe5d63ea to your computer and use it in GitHub Desktop.
Using spine, attaching skin placeholders from other skins.
using Spine.Unity;
using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(SkeletonAnimation))]
public class SkinChanger : MonoBehaviour {
private SkeletonAnimation sa;
private Skin originalSkin;
void Start () {
sa = GetComponent<SkeletonAnimation> ();
originalSkin = sa.skeleton.Skin;
}
public void SkinSwap(string skinName, string placeholderName)
{
Skin tmpSkin = sa.skeleton.data.FindSkin (skinName);
Skin combinedSkin = new Skin ("Combined");
foreach (KeyValuePair<Spine.Skin.AttachmentKeyTuple,Spine.Attachment> a in originalSkin.Attachments) {
int slotIndex = a.Key.slotIndex;
string attachmentName = a.Key.name;
Attachment attachment = a.Value;
combinedSkin.AddAttachment (slotIndex, attachmentName, attachment);
}
foreach (KeyValuePair<Spine.Skin.AttachmentKeyTuple,Spine.Attachment> a in tmpSkin.Attachments) {
int slotIndex = a.Key.slotIndex;
string attachmentName = a.Key.name;
Attachment attachment = a.Value;
if (attachmentName == placeholderName) {
combinedSkin.AddAttachment (slotIndex, attachmentName, attachment);
}
}
sa.skeleton.SetSkin (combinedSkin);
sa.skeleton.SetSlotsToSetupPose ();
}
}
// This is just a test using the goblin project and setting the spear as a weapon for one skin, using skin placeholders and a dagger for the other skin.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SkinChanger))]
public class WeaponSwap : MonoBehaviour {
private SkinChanger sc;
private bool isDagger = true;
// Use this for initialization
private void Start () {
sc = GetComponent<SkinChanger> ();
}
public void OnButtonClick()
{
isDagger = !isDagger;
if (isDagger) {
sc.SkinSwap ("goblingirl", "weapon");
} else {
sc.SkinSwap ("goblin", "weapon");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment