- Modifié
Changing attachment in slot when skin doesn't have it
Our skeleton has outfit_torso slot, which has 3 possible attachments: outfit_torso_small, outfit_torso_med, outfit_torso_big.
Most skins have these 3 attachments, but not all.
Because of it, SetAttachment("outfit_torso", "outfit_torso_"+ doll.size) throws an error about not finding attachment for outfit_torso slot when we use a skin without these attachments.
Atm I fixed it by adding optional boolean variable to SetAttachment, when it's false the function doesn't throw an error, ie
if (attachment == null && throw_exception) throw new Exception("Attachment not found: " + attachmentName + ", for slot: " + slotName);
Which make the function work equivalently to SetAttachment("outfit_torso", null) when throw_exception is false.
It works just fine, but I wonder if there is a better way to handle it?
Good question. You could use these methods instead, so you can also cache the slot:
// it is best to cache the slot and slotIndex in e.g. Start()
int slotIndex = skeleton.FindSlotIndex(string slotName);
Slot slot = skeleton.Slots[slotIndex];
Attachment attachment = skeleton.GetAttachment(slotIndex, attachmentName);
if (attachment != null)
slot.Attachment = attachment;