For example, in an animation file, there are multiple attachments in one slot. When playing an animation, only one major attachment(sach as 'Bracelet0') is displayed in that slot.I want to control the display of another attachment (like "Bracelet1" or whatever) through Unity runtime code when the animation plays at some point.
I looked in the spine-unity Runtime Documentation and there seems to be nothing about it.
Maybe I can make all the attachments in the animation file display, and then control the display or hiding of specific attachments in Unity.
Is there a better suggestion?
How to control attachments are display or hidden in animations?
To control the display or hiding of specific attachments in Unity during runtime, you can use the Spine API to set the attachment for a slot. Here is a step-by-step guide on how to achieve this:
Find the Slot and Set the Attachment:
You can find the slot by its name and set the attachment to the desired one. If you want to hide the attachment, you can set it tonull
.// Assuming you have a reference to the SkeletonAnimation component SkeletonAnimation skeletonAnimation = GetComponent<SkeletonAnimation>(); Slot slot = skeletonAnimation.skeleton.FindSlot("slotName"); // To display a specific attachment slot.Attachment = skeletonAnimation.skeleton.GetAttachment("slotName", "Bracelet1"); // To hide the attachment slot.Attachment = null;
Update the Attachment Every Frame:
If your animation keys the attachment for the slot, you need to override it every frame after the animation has been applied. You can use theUpdateComplete
callback for this purpose.void Start() { skeletonAnimation.UpdateComplete += OverrideAttachment; } void OverrideAttachment(ISkeletonAnimation animatedSkeleton) { Slot slot = skeletonAnimation.skeleton.FindSlot("slotName"); slot.Attachment = skeletonAnimation.skeleton.GetAttachment("slotName", "Bracelet1"); }
Ensure No Conflicting Keys in Animation:
Make sure that your animation does not key the attachment for the slot you are trying to control via code. If it does, your code changes will be overridden by the animation.Use Skins for Different Configurations:
If you have multiple configurations (e.g., different bracelets), consider using skins to manage them. This way, you can switch between different skins without manually setting attachments.skeletonAnimation.skeleton.SetSkin("skinName"); skeletonAnimation.skeleton.SetSlotsToSetupPose();
By following these steps, you can control the display or hiding of specific attachments in Unity during runtime, ensuring that your animations and code changes work harmoniously.
- Изменено
RalphYDY This kind of operation can be done using the same API in all runtimes, so please refer to the general Spine Runtime Guide, not the spine-unity runtime documentation. For example, you can find more details about the method explained in the first solution suggested by Spinebot on the following page: