Skip to content

Instantly share code, notes, and snippets.

@jmcguirk
Created April 29, 2013 18:15
Show Gist options
  • Save jmcguirk/5483536 to your computer and use it in GitHub Desktop.
Save jmcguirk/5483536 to your computer and use it in GitHub Desktop.
TK2D Skeleton controller changes - Enabled tinting, normal regen
using UnityEngine;
using Spine;
/*
*/
[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class tk2dSpineSkeleton : MonoBehaviour {
public AnimationCompleteHandler animationComplete;
/*
*/
public tk2dSpineSkeletonDataAsset skeletonDataAsset;
public Skeleton skeleton;
public string animationName;
public string skinName;
public bool loop;
public float animationSpeed = 1;
public Spine.AnimationState state;
/*
*/
private Mesh mesh;
private Vector3[] vertices;
private Color32[] colors;
private Vector2[] uvs;
private int[] triangles;
private int cachedQuadCount;
/*
*/
void Update() {
SkeletonData skeletonData = (skeletonDataAsset != null) ? skeletonDataAsset.GetSkeletonData() : null;
if(skeletonData == null) {
Clear();
return;
}
if(skeleton == null || skeleton.Data != skeletonData) Initialize();
UpdateAnimation();
UpdateCache();
UpdateMesh();
}
/*
*/
private void Clear() {
GetComponent<MeshFilter>().mesh = null;
DestroyImmediate(mesh);
mesh = null;
renderer.sharedMaterial = null;
skeleton = null;
state = null;
}
public Color Color {
get {
return new Color(skeleton.R, skeleton.G, skeleton.B, skeleton.A);
}
set {
skeleton.R = value.r;
skeleton.G = value.g;
skeleton.B = value.b;
skeleton.A = value.a;
//Debug.Log("Set color " + skeleton.A);
}
}
/*
*/
private void Initialize() {
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
mesh.name = "tk2dSkeleton Mesh";
//mesh.hideFlags = HideFlags.HideAndDontSave;
state = new Spine.AnimationState(skeletonDataAsset.GetAnimationStateData());
skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData());
skeleton.SetSkin(skinName);
skeleton.SetSlotsToBindPose();
}
/*
*/
private void UpdateMesh() {
int quadIndex = 0;
int drawCount = skeleton.DrawOrder.Count;
for(int i = 0; i < drawCount; i++) {
Spine.Slot slot = skeleton.DrawOrder[i];
Attachment attachment = slot.Attachment;
Color color = new Color(slot.R * skeleton.R, slot.G * skeleton.G, slot.B * skeleton.B, slot.A * skeleton.A);
if(attachment is RegionAttachment) {
RegionAttachment regionAttachment = attachment as RegionAttachment;
regionAttachment.UpdateVertices(slot.Bone);
//regionAttachment.ScaleX = 2;
//regionAttachment.ScaleY = 2;
regionAttachment.UpdateOffset();
float[] regionVertices = regionAttachment.Vertices;
int vertexIndex = quadIndex * 4;
vertices[vertexIndex + 0] = new Vector3(regionVertices[RegionAttachment.X1],regionVertices[RegionAttachment.Y1],0);
vertices[vertexIndex + 1] = new Vector3(regionVertices[RegionAttachment.X4],regionVertices[RegionAttachment.Y4],0);
vertices[vertexIndex + 2] = new Vector3(regionVertices[RegionAttachment.X2],regionVertices[RegionAttachment.Y2],0);
vertices[vertexIndex + 3] = new Vector3(regionVertices[RegionAttachment.X3],regionVertices[RegionAttachment.Y3],0);
colors[vertexIndex + 0] = color;
colors[vertexIndex + 1] = color;
colors[vertexIndex + 2] = color;
colors[vertexIndex + 3] = color;
AtlasRegion region = regionAttachment.Region;
if(region.rotate) {
uvs[vertexIndex + 0] = new Vector2(region.u,region.v);
uvs[vertexIndex + 1] = new Vector2(region.u,region.v2);
uvs[vertexIndex + 2] = new Vector2(region.u2,region.v);
uvs[vertexIndex + 3] = new Vector2(region.u2,region.v2);
} else {
uvs[vertexIndex + 0] = new Vector2(region.u,region.v);
uvs[vertexIndex + 1] = new Vector2(region.u2,region.v);
uvs[vertexIndex + 2] = new Vector2(region.u,region.v2);
uvs[vertexIndex + 3] = new Vector2(region.u2,region.v2);
}
int index = quadIndex * 6;
triangles[index] = vertexIndex;
triangles[index + 1] = vertexIndex + 2;
triangles[index + 2] = vertexIndex + 1;
triangles[index + 3] = vertexIndex + 2;
triangles[index + 4] = vertexIndex + 3;
triangles[index + 5] = vertexIndex + 1;
quadIndex++;
}
}
mesh.vertices = vertices;
mesh.colors32 = colors;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateNormals();
Material matter = skeletonDataAsset.sprites.spriteCollection.inst.materials[0];
renderer.sharedMaterial = matter;
}
/*
*/
private void UpdateCache() {
int quadCount = 0;
int drawCount = skeleton.DrawOrder.Count;
for(int i = 0; i < drawCount; i++) {
Attachment attachment = skeleton.DrawOrder[i].Attachment;
if(attachment is RegionAttachment) quadCount++;
}
if(quadCount == cachedQuadCount) return;
cachedQuadCount = quadCount;
vertices = new Vector3[quadCount * 4];
colors = new Color32[quadCount * 4];
uvs = new Vector2[quadCount * 4];
triangles = new int[quadCount * 6];
}
/**
* Whether or not we should flip the rig's X direction
*/
public bool FlipX {
get {
return this.skeleton.FlipX;
}
set {
this.skeleton.FlipX = value;
}
}
/*
*/
private void UpdateAnimation() {
// Check if we need to stop current animation
if(state.Animation != null && animationName == null) {
state.ClearAnimation();
}
// Check for different animation name or animation end
else if(state.Animation == null || animationName != state.Animation.Name) {
Spine.Animation animation = skeleton.Data.FindAnimation(animationName);
if (animation != null) {
if (animationComplete != null) {
this.animationComplete(animationName);
}
state.SetAnimation(animation, loop);
}
}
state.Loop = loop;
// Update skeleton and animation
skeleton.Update(Time.deltaTime * animationSpeed);
state.Update(Time.deltaTime * animationSpeed);
state.Apply(skeleton);
skeleton.UpdateWorldTransform();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment