• Unity
  • Set Attachment to slot from RegionAttachment

I'm trying to use a spine atlas asset in Unity to attach a region to a slot. The skeleton.SetAttachments has no overload method that takes an attachment only "string, string". I'm sure this has been answered before but after looking around for an hour or so I can't seem to get the attachment to render on screen.

My code:

public static void LinkSprite(Skeleton skeleton, string targetSlot, string spriteName ) {
        LoadAtlas();
        RegionAttachment attachment = GetAttachment( spriteName );
        skeleton.FindSlot( targetSlot ).attachment = attachment;

}

static RegionAttachment GetAttachment(string spriteName ) {
    string path = "Equipment/" + spriteName;
    AtlasRegion region = equipmentAtlas.GetAtlas().FindRegion( path );
    if ( region == null ) throw new System.ArgumentException( string.Format( "Region not found in atlas: {0} (region attachment: {1})", path, spriteName ) );
    RegionAttachment attachment = new RegionAttachment( path );
    attachment.RendererObject = region;
    attachment.SetUVs( region.u, region.v, region.u2, region.v2, region.rotate );
    attachment.regionOffsetX = region.offsetX;
    attachment.regionOffsetY = region.offsetY;
    attachment.regionWidth = region.width;
    attachment.regionHeight = region.height;
    attachment.regionOriginalWidth = region.originalWidth;
    attachment.regionOriginalHeight = region.originalHeight;
    return attachment;
}

Is there an easier built-in method to convert an AtlasRegion to Attachment to Slot?

Related Discussions
...

Hey.
You must have gotten that code from a pretty old thread. Spine-Unity comes with some methods to do that for you now.

Though it depends on what you want and what your current setup is. (though really, it's sort of more scalable if you base your setup on what's possible in the runtime).

Make sure you add this using.

using Spine.Unity.Modules.AttachmentTools;

That will scope you into the namespace of the various Spine.Attachment-related tools in general. A couple of them are described below.

  1. If you want to get a clone of an existing attachment (including all its alignment information) then you can use this:

    Attachment originalAttachment = skeletonAnimation.Skeleton.GetAttachment(slotName, attachmentName);
    Attachment newAttachment = originalAttachment.GetRemappedClone(unitySprite, sourceMaterial);
    

    This will remap it regardless of whether it's a mesh or a plain region.

  2. If you just want a new RegionAttachment from scratch from a UnityEngine.Sprite, you can use this:

    Attachment newAttachment = unitySprite.ToRegionAttachment(sourceMaterial);
    

    This will make a new attachment but it won't have any alignment data so you have align it yourself by setting the x, y, rotation, scaleX, scaleY yourself, and update the verts after.

The sourceMaterial parameter determines the material property settings (including the shader) to be used.

Is there a way that I can grab the sprite from a spine atlas rather than Unity for the examples above?

Sure, if you have the Spine AtlasRegion, you would just go:

atlasRegion.ToRegionAttachment("whatever name you want");

or if based on an existing attachment...

originalAttachment.GetRemappedClone(atlasRegion);

You can get the AtlasRegion from your AtlasAsset.GetAtlas().FindRegion("name of the image or region");

We're currently working on the documentation for Spine-Unity AttachmentTools.

4 days later

Thank you! Works perfectly!


28 Sep 2017, 12:58


Pharan wrote

This will make a new attachment but it won't have any alignment data so you have align it yourself by setting the x, y, rotation, scaleX, scaleY yourself, and update the verts after.

How do I update the verts of an attachment? I'm not seeing any methods available to the attachment itself.

Also, have you guys looking into streamlining the new UnityEngine.SpriteAtlas into the Spine-Unity runtime? It would be fantastic if I could just covert the spriteAtlas into a spine atlas and it would retain all the pivot points of the sprites in the spriteAtlas. At the moment, I am feel I am forced to use unity sprites/atlas since the spine atlas doesn't hold pivot point data (at least when using the texture packer feature).

After applying changes to x, y, rotation, scaleX, scaleY, you would call RegionAttachment.UpdateOffset().
MeshAttachments don't have these values as they are actually just a set of vertices (stored as a raw float array).

We'll see about getting some extra methods to use pivot data when creating new RegionAttachments.
You may want also to see what we've documented so far as workable workflows. https://github.com/pharan/spine-unity-docs/blob/master/Mix-and-Match.md
If you have any comments/feedback/confusion on it, let us know.