Great question! This is a common challenge when switching from RegionAttachment to MeshAttachment for runtime swapping.
Understanding the problem
When you copy a MeshAttachment and set a new region on it, the mesh vertices (and their UVs) are defined in the original skeleton data. The UVs are computed relative to the original template region's bounds. When you swap in a replacement AtlasRegion of different proportions, the UVs no longer map correctly to the new image — the mesh geometry stays the same but the texture mapping stretches or overflows, which is exactly what you're seeing.
The recommended approach — GetRemappedClone
In spine-unity, the correct tool for this is GetRemappedClone, part of AttachmentCloneExtensions:
AttachmentCloneExtensions
For a MeshAttachment, it looks like this:
MeshAttachment remapped = originalMeshAttachment.GetRemappedClone(
replacementAtlasRegion,
true, // true = inherit the original mesh's UV/animation data
true, // true = use original mesh proportions
1.0f // scale
);
The key parameter here is the inheritDeformTo flag — when set to true, it keeps the original mesh's vertex positions and animations intact while remapping the texture region onto it.
What this does and does not do
This approach will correctly remap the UVs of the mesh to fit within the bounds of the replacement AtlasRegion, so the image is mapped properly onto the mesh geometry. However, the mesh vertices themselves will remain in the same positions — which is actually what you want for weighted/deforming meshes, since you want the squash-and-stretch behaviour to be driven by the template mesh.
The implication is that your replacement images should ideally be designed to fit within the same proportional bounds as the template mesh. If a weapon image is significantly narrower or shorter than the template, it will appear correctly mapped to the mesh but the mesh may still extend slightly beyond the visible image area (since the mesh shape is fixed).
Practical recommendation
The best workflow for your use case is:
- Design all weapon images to be roughly the same aspect ratio and size as the template mesh region
- Pack them all into your replacement atlas
- Use
GetRemappedClone to swap the region at runtime
This is also demonstrated in the example scenes Spine Examples/Other Examples/Mix and Match and Mix and Match Equip in the spine-unity package, which show runtime attachment replacement with both sprites and atlas regions.
Note on HasTextureRegion.updateRegion
If you ever set the region manually on a mesh attachment copy, always call updateRegion() afterwards:
meshAttachment.Region = newAtlasRegion;
meshAttachment.UpdateRegion();
This ensures the attachment recalculates its internal values based on the new region, as described in the API reference.