diff --git a/spine-unity/Assets/Plugins/Spine/SkeletonComponent.cs b/spine-unity/Assets/Plugins/Spine/SkeletonComponent.cs index 244a7c104..385b3f576 100644 --- a/spine-unity/Assets/Plugins/Spine/SkeletonComponent.cs +++ b/spine-unity/Assets/Plugins/Spine/SkeletonComponent.cs @@ -40,7 +40,6 @@ public class SkeletonComponent : MonoBehaviour { private Color[] colors; private Vector2[] uvs; private int[] triangles; - private int quadCount; private float[] vertexPositions = new float[8]; public virtual void Clear () { @@ -57,6 +56,8 @@ public class SkeletonComponent : MonoBehaviour { mesh.name = "Skeleton Mesh"; mesh.hideFlags = HideFlags.HideAndDontSave; + vertices = new Vector3[0]; + skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData(false)); if (initialSkinName != null && initialSkinName.Length > 0) { @@ -94,13 +95,27 @@ public class SkeletonComponent : MonoBehaviour { } // Ensure mesh data is the right size. - if (quadCount != this.quadCount) { - this.quadCount = quadCount; + if (quadCount > vertices.Length / 4) { vertices = new Vector3[quadCount * 4]; colors = new Color[quadCount * 4]; uvs = new Vector2[quadCount * 4]; triangles = new int[quadCount * 6]; mesh.Clear(); + + for (int i = 0, n = quadCount; i < n; i++) { + int index = i * 6; + int vertexIndex = i * 4; + 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; + } + } else { + Vector3 zero = new Vector3(0, 0, 0); + for (int i = quadCount * 4, n = vertices.Length; i < n; i++) + vertices[i] = zero; } // Setup mesh. @@ -135,14 +150,6 @@ public class SkeletonComponent : MonoBehaviour { uvs[vertexIndex + 2] = new Vector2(regionUVs[RegionAttachment.X2], 1 - regionUVs[RegionAttachment.Y2]); uvs[vertexIndex + 3] = new Vector2(regionUVs[RegionAttachment.X3], 1 - regionUVs[RegionAttachment.Y3]); - 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++; } }