• Editor
  • I can change the attachment in 'setup' mode but not in 'animation' mode

Basically, I characters that you can change the skin of, and that you can change the hat of.

Similar to a character that you might change the outfit of and the weapon of.

Everything works fine with the skins, no problem.
But I'm having real trouble changing character hats.

Basically, if I'm in 'setup' mode, then I can change hats no problem. I go to animation mode, and it will remember the hat that I set.

But if I'm in animation mode, then it won't let me change hats.

This is quite frustrating, because it means that in Spine Unity Runtime, when I try to call

        unit.skeletonanimation.Skeleton.SetSkin("Skin1");
        unit.skeletonanimation.Skeleton.SetAttachment("Hat", "Hats/_Hat1");

The skin changes as it should, but the Hat just defaults to whatever hat I had equipped during the animation.

I want to be able to change the Hat on the fly, separately from the skin. There are like 12 skins and dozens of hats. I don't want to make hundreds of skins for each unique skin+hat combination. I just want to be able to change the hat freely.

What am I doing wrong?

  • Nate respondió a esto
    Related Discussions
    ...

    testusername123 But if I'm in animation mode, then it won't let me change hats.

    You lost me here. How are you changing hats? If you don't set a key, changing a slot's attachment visibility will be lost as soon as the timeline position is changed. The easiest thing to do is turn on autokey.

    testusername123 The skin changes as it should, but the Hat just defaults to whatever hat I had equipped during the animation.

    If you key an attachment change in an animation, then every time you apply that animation, the attachment change will occur. You can set an attachment each frame, after you apply the animation. Otherwise, don't key it in the animation.

    I found the issue.

    I had a 'pre-idle' animation where I had keyframed everything off, except for the 'body' which I switched to an egg image and then rotated it and had it hopping around, growing, etc.

    This pre-idle animation for the egg state therefore had the hat keyframed as being off.

    So then in Unity, when I would tell it to play the idle animation, it would reset to the default attachment of the setup (a null hat).

    So I tried running

        current_trackentry = skeletonanimation.AnimationState.SetAnimation(0, "Idle", true);
        skeletonanimation.Skeleton.SetAttachment("Hat", "Hats/_Hat1");

    But this didn't work either. I think that SetAnimation updates the slot attachments in late-update or something (someone please correct me here if I'm wrong)?

    In the end, I just noticed decided to remove the egg keyframe for the hat entirely, and I just manage it in code now. I don't think it's the RIGHT solution, but it is A solution.

    Anyways. Hopefully these details will help iron out what the problem actually was, so someone else who runs into a similar issue down the road might better understand how to solve it.

    • Nate respondió a esto

      testusername123 I had a 'pre-idle' animation where I had keyframed everything off

      Glad you found it! Generally you don't want any animation to key everything. It's inefficient and what is "everything" often changes, so you'd need to remember to add things to such an animation and you'll surely forget. Mainly though, it is inefficient. You can use SetToSetupPose instead.

      testusername123 But this didn't work either.

      It works, but maybe you set a different attachment afterward.

      testusername123 I think that SetAnimation updates the slot attachments in late-update or something

      SetAnimation does it immediately, where "it" is that it sets up the AnimationState tracks. It does not apply the animation.

      Take a look at the spine-unity lifecycle. The Unity components are likely applying animations for you. You'll need to use the right callbacks so your code runs after animations are applied.

      testusername123 I just noticed decided to remove the egg keyframe for the hat entirely, and I just manage it in code now.

      That's a fine solution. Why were you keying it in an animation if you want to control it in code? If it really does need to be keyed in animations, for example to show a hat with a different perspective mid-animation, then you can do that. A "skin" is an intermediate layer between an animation and the actual attachment used. Instead of the animation keying a specific attachment named "red hat front", it keys a skin placeholder named eg "hat front". When the animation is applied, the timeline will call skeleton.SetAttachment(slot, "hat front"). See the docs on that method:
      http://esotericsoftware.com/spine-api-reference#Skeleton-setAttachment
      Since it calls GetAttachment, look at that method too:
      http://esotericsoftware.com/spine-api-reference#Skeleton-getAttachment
      GetAttachment looks in the skin for the specified name ("hat front") to find the actual attachment. This allows you to put any attachment you want in the skin for "hat front", decoupled from what the animation keys.

      Given that, the way it works is you create a skin and populate it with the attachments you want. It sounds like you are using the skins you defined in the Spine editor. Instead of doing that, you create an empty skin and add stuff to it. For example, you can add one or more skins defined in the Spine editor, and/or you can set specific attachments for the hat skin placeholders. See:
      http://esotericsoftware.com/spine-runtime-skins

      I suggest using a skin for a collection of attachments. For your hat, say you have skin placeholders hat front and hat side, then you create a skin called red hat and populate it with red hats for those placeholders. Create another skin called blue hat and populate the attachments. Then at runtime you create an empty skin and add either the red hat or blue hat skin to it.

      You don't have to combine skins like this, but it's convenient. You could instead add the attachments for the hat front and hat side skin placeholders instead of using a skin. The skin just groups it for convenience in Spine. In Spine you can apply multiple skins at once, so you can preview and animate with different skin combinations:
      http://esotericsoftware.com/spine-skins#Active-skin

      All that said, if you have many attachments (eg 1000+) then setting them all up in the editor is tedious. With careful organization you can make a setup that uses template images in Spine, so you can visualize and animation, then at runtime you configure everything dynamically.