Hello!
I have some simple code set up to attach sprites to skels at runtime, when some of our creatures pick up items to carry. It's based off some of your sample code and works perfectly most of the time. But every now and then, the attached sprite shows up as black, like so:
Here's the core code in question:
void attachSpriteToSkeleton(SkeletonAnimation _skelAnim, MeshRenderer _mesh, Slot _slot, Sprite _sprite)
{
//Get handle to a few things that we'll be using it a lot
Skeleton skel = _skelAnim.skeleton;
string slotName = _slot.Data.Name;
string newAttachmentName = _sprite.name;
//All attachment changes will be applied to the skin. We use a clone so other instances will not be affected.
Skin newSkin = skel.UnshareSkin(true, false);
//Create an attachment from a Unity Sprite
RegionAttachment newAttachment = _sprite.ToRegionAttachmentPMAClone(_mesh.material);
//Add the attachement to the slot
int slotIndex = _skelAnim.skeleton.FindSlotIndex(slotName);
newSkin.AddAttachment(slotIndex, newAttachmentName, newAttachment);
//Set the skin to the one we made, and then make our attachments visible.
//Note that any keying to the slot in the anim will still be recognized, so make sure that's all good to go!
skel.SetSkin(newSkin);
skel.SetToSetupPose();
skel.SetAttachment(slotName, newAttachmentName);
}
Like I said pretty straightforward, and will probably look familiar to you Any insight would be appreciated! Not really sure why it would only be happening rarely and not consistently... I'll also post some images of various related settings below.
The spine files are brought in as json data and individual assets, the latter of which are packed and referenced via tk2d. The tk2d atlas settings are:
The sprite that gets attached is loaded from Resources at runtime, and has the following settings: