midiphony-panda

Hello everyone :grinteeth:
I am very new to Spine (as a developer) and I hope my question isn't too stupid to begin witth :grinfake:
My use case, "how to set/update a skin on a SkeletonAnimation" is very simple, but I'd like to get a better undestanding on how to do things properly.

Let's say I have a running SkeletonAnimation, and want to set a skin on its skeleton.
My code is
(_skins being a List<Skin> deduced from the Skeleton's data) :
_skeleton.Skin = _skins[skinId];

If I don't do anything else, the skin of the SkeletonAnimation displayed is not actually updated.
The skin is eventually updated after calling :
_skeleton.SetSlotsToSetupPose();
I am also noticing a little flickering on the Skeleton after this call, if I don't instantly follow with this method call :
_skeletonAnimation.Update(0);

So my question is : why do I have to call
Skeleton.SetSlotsToSetupPose()
and
SkeletonAnimation.Update(float)
after (re)setting a skin ?

I'm not sure to understand why I need to reset the slots and the animation.
________________________________________

If it helps in any way, here's my initialization code :
private SkeletonAnimation _skeletonAnimation;
private Spine.AnimationState _spineAnimationState;
private Spine.Skeleton _skeleton;

private List<Skin> _skins = new List<Skin>();

void Start()
{
_skeletonAnimation = GetComponent<SkeletonAnimation>();
_spineAnimationState = _skeletonAnimation.AnimationState;
_skeleton = _skeletonAnimation.Skeleton;

_spineAnimationState.SetAnimation(0, idleAnimationName, true);

foreach (Skin skin in _skeleton.Data.Skins)
_skins.Add(skin);
}
User avatar
midiphony-panda
  • Posts: 15

Harald

Hello and welcome to Spine! :nerd:
midiphony-panda wrote:So my question is : why do I have to call Skeleton.SetSlotsToSetupPose()
This is necessary because animations and user-code can change attachments, therefore you might not want to have the SetSkin call automatically perform a reset of all attachments.

See also:
Skeleton setSkin
Skeleton setSlotsToSetupPose

Please also be sure to check out the example scenes that come with the spine-unity unitypackage, located in the Spine Examples directory, these will demonstrate many typical use cases.
midiphony-panda wrote:I am also noticing a little flickering on the Skeleton after this call, if I don't instantly follow with this method call
Depending on where you are calling SetSkin from, you might be too late (or too early), e.g. if it's between Update() and LateUpdate() of the SkeletonAnimation component. Then the animation will already have been applied via Update(), but with the wrong skin set. It's a matter of Unity's script execution order. So you could either setup your script to run before SkeletonAnimation, or use the provided skeleton callback delegates, in your case you can register your method to SkeletonAnimation.BeforeApply as described here. Just be aware that registering to skeleton callback delegates will call them every time, not only once.

The spine-unity documentation page should also be helpful in general:
spine-unity Runtime Documentation
User avatar
Harald

Harri
  • Posts: 3996

midiphony-panda

Thank you, very concise and useful information :)

Okay, it's clearer now that you have shown me Spine runtime lifecycle.
If I register my slots resetting method in SkeletonAnimation.BeforeApply, everything works well (and I can unregister once it's done).
User avatar
midiphony-panda
  • Posts: 15

Harald

Glad to hear you've figured it out. :) Thanks for getting back to us.
User avatar
Harald

Harri
  • Posts: 3996


Return to Unity