nngafook

Hello!

So I've looked at almost every source of information I could find about swapping attachments during runtime in Unity, and I thought that I was just doing it wrong, but now I'm not so sure anymore.
Basically I want to be able to swap out the torso of my animation, or the helmet, or any other piece of the body. i.e. when the player gets new gear, it is reflected in the art. I know this must be possible, cause this is probably the core reason people would use spine over native unity solutions. But every time I try to use the sprite attacher, or the atlas region component, it never swaps the body part right. It's either rotated wrong, or scaled wrong, or positioned wrong.

So I started thinking that maybe I'm not supposed to be using attachments. Maybe there's something I'm missing.
I know skins are to change the entire skin of the skeleton, but I don't want to have preset skins on, nor do I ever really want to change the entire skin, just parts. And those parts could be many, so they would probably have to be loaded dynamically and swapped during run time. Not in the Unity editor, or in Spine.

So I'm hoping someone can point me in the right direction of a tutorial, cause all I can find are tutorials for putting a sword in a hand attachment or something of that kind.

Thanks!
nngafook
  • Сообщения: 28

Pharan

Skins may be what you need (at least while you can't define and preview stuff like this in Spine editor yet).
This is unfortunately not super straightforward if you don't understand some parts of the runtime.

In spine-csharp, the Skin object is actually just a wrapper for a Dictionary that maps "attachment identification" (a slot and attachment name) with an actual attachment object.
This means a Skin does define "helmet", "shield", "boots" and point to actual attachments, which can change through code. So at one point "helmet" can be an bronze helmet, and then later can be an iron helmet just by redefining dictionary entries.

The setup in Spine editor is a bit specific so I'll just give you a sample spine project and Unity code in a bit.

-- 01 Mar 2016 6:24 pm --

Actually, I managed to retrieve an older demo. I cleaned it up for readability.
The zip comes with a spine project folder and unitypackage.

The spine project shows how you can put multiple attachments for one logical item into one Skin in Spine editor.
This may be more complexity than you need, but it should at least let you know that you're not restricted to having only one attachment per logical "equip".
Select the "(preview only)" skin to see a preview of the animation.
It uses skin placeholders with preview attachments in them.
The other skins under the skins node define each item. The items are defined by adding attachments to relevant skin placeholders while leaving the other placeholders empty.

The unitypackage has an exported version of the spine project, a sample scene, and sample code.
Most of the code you want is in #region Inspector and GenerateSkin.

Open the sample scene and take a look at the script's inspector to see what it does for you.
Then read the script's code to see which attributes were used to show those.
I tried to localize all the things you need to do inside GenerateSkin so it's not hard to understand.
У вас нет необходимых прав для просмотра вложений в этом сообщении.
Аватара пользователя
Pharan
  • Сообщения: 5366


Вернуться в Unity