So I have been successfully using the SetAttachment method to switch out props on a character at runtime but now I am running into an exception when using attachments and Skins.
So situational overview I have gotten a character that has three skins (Light, Dark, Normal) the character also has two arms (normal, muscular)
Because of the different skins slot F_Arm_Small has three attachments F_Arm_Small, F_Arm_Small_Light, F_Arm_Small_Dark. But due to them being attached to a certain skin only one of them shows up in the inspector, as is expected
now I have a property
private bool smallArms
public bool SmallArms{
get { return smallArms; }
set {
smallArms = value;
if(smallArms){
//disable the large arms
skeleton.SetAttachment("F_Arm_Large", null);
//enable the small arms
List<Attachment> posAttachements = new List<Attachment>();
skeleton.Skin.FindAttachmentsForSlot(skeleton.FindSlotIndex("F_Arm_Small"), posAttachements);
skeleton.SetAttachment("F_Arm_Small", posAttachements[0].Name);
}
}
}
Now it is skeleton.SetAttachment("F_Arm_Small", posAttachements[0].Name); that is throwing the exception
Exception: Attachment not found: F_Arm_Small_Dark, for slot: F_Arm_Small
Spine.Skeleton.SetAttachment (System.String slotName, System.String attachmentName) (at Assets/Spine/spine-csharp/Skeleton.cs:433)
LoK.FemaleClothingController.set_UseSmallBoobs (Boolean value) (at Assets/Scripts/SharpClawVillage/Animation/FemaleSC/FemaleClothingController.cs:513)
LoK.FemaleClothingController.Start () (at Assets/Scripts/SharpClawVillage/Animation/FemaleSC/FemaleClothingController.cs:79)
I have been looking into the code behind the SetAttachment and the problem lies in the attachments dictionary.
When reading the json data these methods are used (SkeletonJson.cs lines 254 and 255)
Attachment attachment = ReadAttachment((Dictionary<String, Object>)entry.Value, skin, slotIndex, entry.Key);
if (attachment != null) skin.AddAttachment(slotIndex, entry.Key, attachment);
In this case the Key to the attachement dictionary is constructed from the slotIndex (correct) and the entry.Key F_Arm_Small
Here is the excerpt from the Json file
"F_Arm_Small": {
"F_Arm_Small": { "name": "F_Arm_Small_Dark", "type": "linkedmesh", "skin": "Normal", "parent": "F_Arm_Small", "width": 158, "height": 532 }
}
When we then get to the SetAttachment method It runs down trough a couple of functions until it gets to the GetAttachment function in skin.cs
public Attachment GetAttachment (int slotIndex, String name) {
Attachment attachment;
attachments.TryGetValue(new AttachmentKeyTuple(slotIndex, name), out attachment);
return attachment;
}
The problem is that the key that is constructed now uses the name that is given (F_Arm_Small_Dark) to construct the Key to the dictionary. This obviously fails to match the Key constructed previous with the entry.Key So it returns null and thus throwing the error
What am I missing? or is it something my animator has done wrong when constructing the various skins?