Last active
January 11, 2016 15:11
-
-
Save lukz/53209b3ab65ec8eb3760 to your computer and use it in GitHub Desktop.
SpineUtils.putRegionIntoSpineSkin() allows to programmatically put LibGDX TextureRegion into Spine animation slot by replacing existing attachment in cloned spine skin. This way you don't have to change SkeletonData so you can use it for multiple spine animations with LibGDX AssetManager.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Lukasz Zmudziak, @lukz_dev on 2016-01-11. | |
*/ | |
public class SpineUtils { | |
public static Skin cloneSkin(Skeleton skeleton, Skin oldSkin, String newName) { | |
Skin newSkin = new Skin(newName); | |
Array<Attachment> slotAttachments = new Array<Attachment>(); | |
Array<String> attachmentNames = new Array<String>(); | |
// Loop through every slot | |
for(Slot slot : skeleton.getSlots()) { | |
// Get the slot index | |
int slotIndex = skeleton.findSlotIndex(slot.getData().getName()); | |
// Get attachments and names from this slot | |
oldSkin.findAttachmentsForSlot(slotIndex, slotAttachments); | |
oldSkin.findNamesForSlot(slotIndex, attachmentNames); | |
// For each attachment in this slot, copy it to new skin | |
for(int i = 0; i < slotAttachments.size; i++) { | |
newSkin.addAttachment(slotIndex, attachmentNames.get(i), slotAttachments.get(i)); | |
} | |
// Clear arrays | |
slotAttachments.clear(); | |
attachmentNames.clear(); | |
} | |
return newSkin; | |
} | |
/** | |
* Makes a copy of default skin and replace attachment with the new one | |
* @param spineAnim | |
* @param region | |
* @param slot name of slot that contains replaced attachment | |
*/ | |
public static void putRegionIntoSpineSkin(SpineAnimationComponent spineAnim, TextureRegion region, String slot) { | |
// Get old attachment | |
RegionAttachment oldAttach = (RegionAttachment)spineAnim.skeleton.findSlot(slot).getAttachment(); | |
// Prepare new attachment | |
RegionAttachment newAttach = new RegionAttachment("Attachment"); | |
newAttach.setRegion(region); | |
newAttach.setWidth(region.getRegionWidth()); | |
newAttach.setHeight(region.getRegionHeight()); | |
newAttach.setScaleX(Assets.ASSET_RESOLUTION_SCALE); // This is specific for my code | |
newAttach.setScaleY(Assets.ASSET_RESOLUTION_SCALE); // This is specific for my code | |
newAttach.setRotation(oldAttach.getRotation()); | |
newAttach.getColor().set(oldAttach.getColor()); | |
newAttach.setX(oldAttach.getX()); | |
newAttach.setY(oldAttach.getY()); | |
newAttach.updateOffset(); | |
// Find proper slot | |
int slotIndex = spineAnim.skeleton.findSlotIndex(slot); | |
// Prepare new skin and replace attachment with the new one | |
Skin skin = SpineUtils.cloneSkin(spineAnim.skeleton, spineAnim.skeleton.getData().getDefaultSkin(), "Custom"); | |
skin.addAttachment(slotIndex, oldAttach.getName(), newAttach); | |
// Set up new skin | |
spineAnim.skeleton.setSkin(skin); | |
spineAnim.skeleton.setSlotsToSetupPose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment