diff --git a/spine-tk2d/Code/tk2dSpineAnimation.cs b/spine-tk2d/Code/tk2dSpineAnimation.cs index ed86ad554..f0639c4c6 100644 --- a/spine-tk2d/Code/tk2dSpineAnimation.cs +++ b/spine-tk2d/Code/tk2dSpineAnimation.cs @@ -11,11 +11,11 @@ public class tk2dSpineAnimation : MonoBehaviour { public float animationSpeed = 1; public Spine.AnimationState state; - private tk2dSpineSkeleton spineSkeleton; + private tk2dSpineSkeleton cachedSpineSkeleton; void Start () { - spineSkeleton = GetComponent(); - state = new Spine.AnimationState(spineSkeleton.skeletonDataAsset.GetAnimationStateData()); + cachedSpineSkeleton = GetComponent(); + state = new Spine.AnimationState(cachedSpineSkeleton.skeletonDataAsset.GetAnimationStateData()); } void Update () { @@ -26,17 +26,19 @@ public class tk2dSpineAnimation : MonoBehaviour { // Check if we need to stop current animation if (state.Animation != null && animationName == null) { state.ClearAnimation(); - } else if (state.Animation == null || animationName != state.Animation.Name) { - // Check for different animation name or animation end - Spine.Animation animation = spineSkeleton.skeleton.Data.FindAnimation(animationName); + } + + // Check for different animation name or animation end + else if (state.Animation == null || animationName != state.Animation.Name) { + Spine.Animation animation = cachedSpineSkeleton.skeleton.Data.FindAnimation(animationName); if (animation != null) state.SetAnimation(animation,loop); } state.Loop = loop; // Update animation - spineSkeleton.skeleton.Update(Time.deltaTime * animationSpeed); + cachedSpineSkeleton.skeleton.Update(Time.deltaTime * animationSpeed); state.Update(Time.deltaTime * animationSpeed); - state.Apply(spineSkeleton.skeleton); + state.Apply(cachedSpineSkeleton.skeleton); } } diff --git a/spine-tk2d/Code/tk2dSpineSkeleton.cs b/spine-tk2d/Code/tk2dSpineSkeleton.cs index 0b577e25b..ca1031f25 100644 --- a/spine-tk2d/Code/tk2dSpineSkeleton.cs +++ b/spine-tk2d/Code/tk2dSpineSkeleton.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using UnityEngine; using Spine; -// TODO: multiple atlas support - [ExecuteInEditMode] [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] @@ -19,12 +17,14 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor private float[] vertexPositions; private List submeshMaterials = new List(); private List submeshIndices = new List(); + private Color cachedCurrentColor; void Awake() { vertexPositions = new float[8]; submeshMaterials = new List(); submeshIndices = new List(); + cachedCurrentColor = new Color(); } void Start () { @@ -38,7 +38,7 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor return; } - if(skeleton == null || skeleton.Data != skeletonData) Initialize(); + if (skeleton == null || skeleton.Data != skeletonData) Initialize(); skeleton.UpdateWorldTransform(); @@ -51,8 +51,6 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor DestroyImmediate(mesh); mesh = null; - renderer.sharedMaterials = null; - skeleton = null; } @@ -62,13 +60,14 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor mesh.name = "tk2dSkeleton Mesh"; mesh.hideFlags = HideFlags.HideAndDontSave; - skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData()); + if(skeletonDataAsset != null) { + skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData()); + } } private void UpdateMesh() { int quadIndex = 0; int drawCount = skeleton.DrawOrder.Count; - Color currentColor = new Color(); for (int i = 0; i < drawCount; i++) { Slot slot = skeleton.DrawOrder[i]; @@ -91,15 +90,15 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor uvs[vertexIndex + 2] = new Vector2(regionUVs[RegionAttachment.X2],regionUVs[RegionAttachment.Y2]); uvs[vertexIndex + 3] = new Vector2(regionUVs[RegionAttachment.X3],regionUVs[RegionAttachment.Y3]); - currentColor.a = skeleton.A * slot.A; - currentColor.r = skeleton.R * slot.R * slot.A; - currentColor.g = skeleton.G * slot.G * slot.A; - currentColor.b = skeleton.B * slot.B * slot.A; + cachedCurrentColor.a = skeleton.A * slot.A; + cachedCurrentColor.r = skeleton.R * slot.R * slot.A; + cachedCurrentColor.g = skeleton.G * slot.G * slot.A; + cachedCurrentColor.b = skeleton.B * slot.B * slot.A; - colors[vertexIndex] = currentColor; - colors[vertexIndex + 1] = currentColor; - colors[vertexIndex + 2] = currentColor; - colors[vertexIndex + 3] = currentColor; + colors[vertexIndex] = cachedCurrentColor; + colors[vertexIndex + 1] = cachedCurrentColor; + colors[vertexIndex + 2] = cachedCurrentColor; + colors[vertexIndex + 3] = cachedCurrentColor; quadIndex++; } @@ -118,7 +117,7 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor if (skeletonDataAsset.normalGenerationMode != tk2dSpriteCollection.NormalGenerationMode.None) { mesh.RecalculateNormals(); - + if (skeletonDataAsset.normalGenerationMode == tk2dSpriteCollection.NormalGenerationMode.NormalsAndTangents) { Vector4[] tangents = new Vector4[mesh.normals.Length]; for (int i = 0; i < tangents.Length; i++) { @@ -127,8 +126,6 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor mesh.tangents = tangents; } } - - renderer.sharedMaterials = submeshMaterials.ToArray(); } private void UpdateCache() { @@ -189,6 +186,8 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor submeshIndices.Add(currentSubmesh.ToArray()); submeshMaterials.Add(oldMaterial); + + renderer.sharedMaterials = submeshMaterials.ToArray(); } diff --git a/spine-tk2d/Example/Example.unity b/spine-tk2d/Example/Example.unity index 1467fe69e..bc7e18b13 100644 Binary files a/spine-tk2d/Example/Example.unity and b/spine-tk2d/Example/Example.unity differ