getAttachment
is returning null and addAttachment
is throwing an illegal argument exception because null is not allowed. Either 0 is the wrong index or, more likely from seeing your screenshot, body0
is the wrong name. Your skin placeholder is body
so that is the name to pass. The attachment returned will have the name body0
. For other skins it could be bodyRed
, bodyGreen
, etc.
Try:
String slotName = "body";
String placeholderName = "body";
Skin newSkin = new Skin("new");
Skin dogSkin = skeletonData.findSkin("dog1");
SlotData bodySlot = skeletonData.findSlot(slotName);
Attachment bodyAttachment = dogSkin.getAttachment(bodySlot.getIndex(), placeholderName);
newSkin.addAttachment(bodySlot.getIndex(), placeholderName, bodyAttachment);
skeleton.setSkin(newSkin);
FWIW, here is some code to copy all attachments from one skin to another:
Skin fromSkin = ...
Skin toSkin = ...
Array<String> names = new Array();
Array<Attachment> attachments = new Array();
Array<SlotData> slots = skeleton.getData().getSlots();
for (int i = 0, n = slots.size; i < n; i++) {
fromSkin.findNamesForSlot(i, names);
fromSkin.findAttachmentsForSlot(i, attachments);
for (int ii = 0, nn = names.size; ii < nn; ii++)
toSkin.addAttachment(i, names.get(ii), attachments.get(ii));
names.clear();
attachments.clear();
}
That is a little clunky because we don't expose the Skin internals. @badlogic, it would be cool to have a Skin method to copy all attachments from another skin. Eg, for libgdx:
/** Adds all attachments from the specified skin to this skin. */
public void addAttachments (Skin skin) {
for (Entry<Key, Attachment> entry : skin.attachments.entries())
addAttachment(entry.key.slotIndex, entry.key.name, entry.value);
}