Ah, well here's a quick static method that just does that.
public static int GetAttachmentCount (Skeleton skeleton, string slotName) {
var skin = skeleton.Skin ?? skeleton.Data.DefaultSkin;
int slotIndex = skeleton.FindSlotIndex(slotName);
int count = 0;
foreach (var key in skin.Attachments.Keys)
if (key.slotIndex == slotIndex)
count++;
// // Count both active skin and default skin.
// if (skin != skeleton.Data.DefaultSkin) {
// foreach (var key in skeleton.Data.DefaultSkin.Attachments.Keys)
// if (key.slotIndex == slotIndex)
// count++;
// }
return count;
}
To elaborate:
Skins just store attachments according to their name and the slot index.
You can get the slotIndex from skeleton.FindSlotIndex
or skeletonData.FindSlotIndex
. The above method removes that need for you.
As for FindNamesForSlot and FindAttachmentsForSlot, the lists you supply are actually output buffers/results buffers.
This is a fairly common pattern when methods have to return an indeterminate number of values, and is the same idea behind Physics2D.RaycastNonAlloc, where you provide the array or list where it will return multiple results. This is to reduce memory allocation pressure.
I guess we could be clearer about what that parameter is for. Thanks for giving us feedback!
Anyway, the sample use for that method would look like this:
public class GetAttachmentNamesSampleClass : MonoBehaviour {
List<string> attachmentNamesResults = new List<string>();
void Start () {
var skeletonAnimation = GetComponent<SkeletonAnimation>();
var defaultSkin = skeletonAnimation.Skeleton.Data.DefaultSkin;
string slotName = "eyes";
int slotIndex = skeletonAnimation.skeleton.FindSlotIndex(slotName);
attachmentNamesResults.Clear();
defaultSkin.FindNamesForSlot(slotIndex, this.attachmentNamesResults);
int attachmentCount = attachmentNamesResults.Count;
}
}