Skip to content

Instantly share code, notes, and snippets.

@pharan
Last active June 21, 2016 14:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pharan/8e082b2645f875e4954073da85ebe072 to your computer and use it in GitHub Desktop.
Save pharan/8e082b2645f875e4954073da85ebe072 to your computer and use it in GitHub Desktop.
[spine-unity] Sample code for how to dynamically create and image-swap a linked MeshAttachment.
/*****************************************************************************
* LinkedMeshSample code created by John Dy
* Full irrevocable rights and permissions granted to Esoteric Software
*****************************************************************************/
using UnityEngine;
using System.Collections;
using Spine;
using Spine.Unity;
public class LinkedMeshSample : MonoBehaviour {
SkeletonAnimation skeletonAnimation;
[SpineSlot] public string slotName;
[Header("Parent/Master Mesh")]
[SpineSkin] public string skinOfParent;
[SpineAttachment(slotField:"slotName")] public string parentAttachment;
[Header("New Linked Mesh")]
public string linkedMeshName = "New Linked Mesh";
public AtlasAsset atlasAsset;
[SpineAtlasRegion] public string regionName;
[SpineAtlasRegion] public string anotherRegionForFun;
IEnumerator Start () {
skeletonAnimation = GetComponent<SkeletonAnimation>();
// Find the parent mesh
MeshAttachment parentMesh = LinkedMeshUtilities.FindAttachment(skeletonAnimation.skeleton, slotName, skinOfParent, parentAttachment) as MeshAttachment;
if (parentMesh == null) {
Debug.LogError(parentAttachment + " in [" + skinOfParent + "] is not a MeshAttachment.");
yield break;
}
// Make a new linked mesh and give it an image
AtlasRegion region = atlasAsset.GetAtlas().FindRegion(regionName);
MeshAttachment newLinkedMesh = LinkedMeshUtilities.NewLinkedMeshAttachment(linkedMeshName, region, parentMesh);
skeletonAnimation.skeleton.FindSlot(slotName).Attachment = newLinkedMesh; // Swap the parent mesh in its slot with the newly created linked mesh.
// For fun, change the image of the existing linked mesh after 2 seconds.
yield return new WaitForSeconds(2f);
AtlasRegion region2 = atlasAsset.GetAtlas().FindRegion(anotherRegionForFun);
LinkedMeshUtilities.SetRegion(newLinkedMesh, region2);
}
public static class LinkedMeshUtilities {
/// <summary>May return null if the attachment is not found.</summary>
static public Attachment FindAttachment (Skeleton skeleton, string slotName, string skinName, string attachmentName) {
int slotIndex = skeleton.FindSlotIndex(slotName);
Skin skin = string.IsNullOrEmpty(skinName) ? skeleton.data.defaultSkin : skeleton.data.FindSkin(skinName);
Attachment attachment = skin.GetAttachment(slotIndex, attachmentName);
return attachment;
}
/// <summary>Instantiate a new MeshAttachment with a region, and linked to parentMesh.</summary>
static public MeshAttachment NewLinkedMeshAttachment (string attachmentName, AtlasRegion region, MeshAttachment parentMesh, bool inheritDeform = true) {
if (string.IsNullOrEmpty(attachmentName)) throw new System.ArgumentException("attachmentName can't be null or empty.", "attachmentName");
if (region == null) throw new System.ArgumentNullException("region");
if (parentMesh == null) throw new System.ArgumentNullException("parentMesh");
// 1. NewMeshAttachment (AtlasAttachmentLoader.cs)
MeshAttachment mesh = new MeshAttachment(attachmentName);
SetRegion(mesh, region, false);
// 2. ReadAttachment. case: LinkedMesh (SkeletonJson.cs)
mesh.Path = attachmentName;
mesh.r = 1f;
mesh.g = 1f;
mesh.b = 1f;
mesh.a = 1f;
// Setting width and height is redundant when setting ParentMesh.
//mesh.Width = masterMesh.Width;
//mesh.Height = masterMesh.Height;
// 3. Link mesh with parent. (SkeletonJson.cs)
mesh.InheritFFD = inheritDeform;
mesh.ParentMesh = parentMesh;
mesh.UpdateUVs();
return mesh;
}
/// <summary>Sets the region (image) of a MeshAttachment</summary>
static public void SetRegion (MeshAttachment attachment, AtlasRegion region, bool updateUVs = true) {
// (based on AtlasAttachmentLoader.cs)
attachment.RendererObject = region;
attachment.RegionU = region.u;
attachment.RegionV = region.v;
attachment.RegionU2 = region.u2;
attachment.RegionV2 = region.v2;
attachment.RegionRotate = 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;
if (updateUVs) attachment.UpdateUVs();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment