• Runtimes
  • Add Unity Game Object to Spine Character

  • Изменено
Related Discussions
...

I am new to Spine, decided to use it to design the character for my game.

The character has already been created and has several animations working inside Unity. I am trying to make a simple inventory/equipment system that layers Unity Game Objects ontop of the Spine Character for armor, weapons, etc.

I got the Game Object to line up in the correct spot on the Spine Character, and made it a child to the bone so that it follows with the animations.

The problem I am facing is the Game Objects don't smoothly work with the animations and I can't control what layer it renders on. For example, If I want to add a glove to the hand not closest to the screen, I can't place it on top of that hand, but behind the rest of the body.

Is there a current way to achieve this? The game could have potentially hundreds of armor/weapon combinations and I don't want this to all be in Spine.

Any help would be appreciated. 🙂

Have a look at the skeleton separator in Unity, it will also "separate" the skeleton dynamically based on the sorting order you animate in Spine.

This looks promising! I will follow up here in a week or so with my results!

Thank you so much, this was driving me crazy.

6 дней спустя

I'd like to add as a reply to the recent email.

The parameters were:
Using them as equippables. Use GameObjects as equippables because there's possibly hundreds of them.

GameObjects aren't necessarily the right solution for this. You'll still solve transformation and sorting issues much more easily if they were rendered as part of the SkeletonRenderer/SkeletonAnimation's mesh. This doesn't mean you can't also create GameObjects out of these so they can appear as drops/pickups.

Here are a few tools at your disposal. You're not supposed to use all of them at once. Just pick the ones that make sense for your setup:

The _Atlas asset (AtlasAsset) inspector can convert regions to meshes.
If you select your Spine Atlas asset in Project view, you'll see that its Inspector has checkboxes for each atlas region.
Checking those checkboxes generates a synced-prefab with a rectangular mesh on it mapped to that atlas region.
This means even if you defined and packed those images in Spine, they can still become GameObjects and Unity meshes.

Spine.Unity.BoneFollower
You add this component to your GameObject and assign it its target SkeletonAnimation and Bone. At runtime, it can follow the target bone's position and rotation. Note that this is a bone "follower" and not a bone "mimicker". Unity GameObjects cannot inherit the transformation matrix of bones so they can't be a part of inherited scale and skew.

Spine.Unity.SkeletonRenderSeparator
https://github.com/pharan/spine-unity-docs/blob/master/SkeletonRenderSeparator.md
Normally, SkeletonAnimation skeletons are rendered as one whole mesh. Being a 2D mesh using alpha blending, this means you can't sandwich other meshes to render between parts of it.
The SkeletonRenderSeparator component will allow your skeleton to be rendered as separate meshes. This will allow you to do the said sandwiching by controlling each separate mesh and renderer's sorting layer and sorting order.

More information on how Unity does sorting can be found here: Spine-Unity Runtime Documentation
But also note, this is a general Unity thing at this point so you can just google those keywords and find info elsewhere, as they pertain to 2D sprites in Unity, will be just as correct.

Spine.Unity.Modules.SpriteAttacher
This is more like sample code but it does allow you to convert UnityEngine.Sprite into Spine.RegionAttachment so they can be rendered as part of the skeleton. This allows you to not worry about sorting. Ideally, you copy or piggyback from the code in this script rather than using it directly (if you are doing multiple attachments with more complicated logic).
Particularly, this code provides extension methods that allow you to do the conversion from other scripts, like this:

public static void Attach (Spine.Unity.SkeletonAnimation skeletonAnimation, UnityEngine.Sprite sprite, string slotName, Shader shader = null) {
   var loader = new Spine.Unity.Modules.SpriteAttachmentLoader(sprite, shader ?? Shader.Find("Spine/Skeleton"));
   var attachment = loader.NewRegionAttachment(null, sprite.name, "");
   skeletonAnimation.skeleton.FindSlot(slotName).Attachment = attachment;
}

4 дня спустя

Thank you very much for all the information. Currently evaluating all the options to see what will work best for what we are going for.