diff --git a/spine-tk2d/Assets/Spine/Editor/SkeletonAnimationInspector.cs b/spine-tk2d/Assets/Spine/Editor/SkeletonAnimationInspector.cs index 3fd552270..02f6b9264 100644 --- a/spine-tk2d/Assets/Spine/Editor/SkeletonAnimationInspector.cs +++ b/spine-tk2d/Assets/Spine/Editor/SkeletonAnimationInspector.cs @@ -60,9 +60,9 @@ public class SkeletonAnimationInspector : Editor { if (component.skeleton != null) { // Initial skin name. - String[] skins = new String[component.skeleton.Data.Skins.Count + 1]; + String[] skins = new String[component.skeleton.Data.Skins.Count]; int skinIndex = 0; - for (int i = 0; i < skins.Length - 1; i++) { + for (int i = 0; i < skins.Length; i++) { String name = component.skeleton.Data.Skins[i].Name; skins[i] = name; if (name == initialSkinName.stringValue) @@ -76,7 +76,7 @@ public class SkeletonAnimationInspector : Editor { EditorGUIUtility.LookLikeInspector(); EditorGUILayout.EndHorizontal(); - initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex]; + initialSkinName.stringValue = skins[skinIndex]; // Animation name. String[] animations = new String[component.skeleton.Data.Animations.Count + 2]; diff --git a/spine-tk2d/Assets/Spine/Editor/SkeletonComponentInspector.cs b/spine-tk2d/Assets/Spine/Editor/SkeletonComponentInspector.cs index 7fef19cc9..d3a6f9a5d 100644 --- a/spine-tk2d/Assets/Spine/Editor/SkeletonComponentInspector.cs +++ b/spine-tk2d/Assets/Spine/Editor/SkeletonComponentInspector.cs @@ -56,9 +56,9 @@ public class SkeletonComponentInspector : Editor { if (component.skeleton != null) { // Initial skin name. - String[] skins = new String[component.skeleton.Data.Skins.Count + 1]; + String[] skins = new String[component.skeleton.Data.Skins.Count]; int skinIndex = 0; - for (int i = 0; i < skins.Length - 1; i++) { + for (int i = 0; i < skins.Length; i++) { String name = component.skeleton.Data.Skins[i].Name; skins[i] = name; if (name == initialSkinName.stringValue) @@ -72,7 +72,7 @@ public class SkeletonComponentInspector : Editor { EditorGUIUtility.LookLikeInspector(); EditorGUILayout.EndHorizontal(); - initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex]; + initialSkinName.stringValue = skins[skinIndex]; } EditorGUILayout.PropertyField(timeScale); diff --git a/spine-tk2d/Assets/Spine/SkeletonComponent.cs b/spine-tk2d/Assets/Spine/SkeletonComponent.cs index 4208433fc..f1bea215d 100644 --- a/spine-tk2d/Assets/Spine/SkeletonComponent.cs +++ b/spine-tk2d/Assets/Spine/SkeletonComponent.cs @@ -54,8 +54,7 @@ public class SkeletonComponent : MonoBehaviour { private Vector2[] uvs; private Material[] sharedMaterials = new Material[0]; private List submeshMaterials = new List(); - private List submeshIndexes = new List(); - private List submeshFirstVertex = new List(); + private List submeshes = new List(); private Vector4[] tangents = new Vector4[0]; public virtual void Clear () { @@ -77,7 +76,7 @@ public class SkeletonComponent : MonoBehaviour { skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData(false)); - if (initialSkinName != null && initialSkinName.Length > 0) { + if (initialSkinName != null && initialSkinName.Length > 0 && initialSkinName != "default") { skeleton.SetSkin(initialSkinName); skeleton.SetSlotsToSetupPose(); } @@ -150,7 +149,7 @@ public class SkeletonComponent : MonoBehaviour { mesh.Clear(); } else { // Too many vertices, zero the extra. - Vector3 zero = new Vector3(0, 0, 0); + Vector3 zero = Vector3.zero; for (int i = vertexCount, n = lastVertexCount; i < n; i++) vertices[i] = zero; } @@ -162,6 +161,7 @@ public class SkeletonComponent : MonoBehaviour { Color32[] colors = this.colors; int vertexIndex = 0; Color32 color = new Color32(); + float a = skeleton.A * 255, r = skeleton.R, g = skeleton.G, b = skeleton.B; for (int i = 0, n = drawOrder.Count; i < n; i++) { Slot slot = drawOrder[i]; RegionAttachment regionAttachment = slot.Attachment as RegionAttachment; @@ -175,10 +175,10 @@ public class SkeletonComponent : MonoBehaviour { vertices[vertexIndex + 2] = new Vector3(vertexPositions[RegionAttachment.X2], vertexPositions[RegionAttachment.Y2], 0); vertices[vertexIndex + 3] = new Vector3(vertexPositions[RegionAttachment.X3], vertexPositions[RegionAttachment.Y3], 0); - color.a = (byte)(skeleton.A * slot.A * 255); - color.r = (byte)(skeleton.R * slot.R * color.a); - color.g = (byte)(skeleton.G * slot.G * color.a); - color.b = (byte)(skeleton.B * slot.B * color.a); + color.a = (byte)(a * slot.A); + color.r = (byte)(r * slot.R * color.a); + color.g = (byte)(g * slot.G * color.a); + color.b = (byte)(b * slot.B * color.a); colors[vertexIndex] = color; colors[vertexIndex + 1] = color; colors[vertexIndex + 2] = color; @@ -195,10 +195,11 @@ public class SkeletonComponent : MonoBehaviour { mesh.vertices = vertices; mesh.colors32 = colors; mesh.uv = uvs; - - mesh.subMeshCount = submeshMaterials.Count; - for (int i = 0, n = mesh.subMeshCount; i < n; ++i) - mesh.SetTriangles(submeshIndexes[i], i); + + int submeshCount = submeshes.Count; + mesh.subMeshCount = submeshCount; + for (int i = 0; i < submeshCount; ++i) + mesh.SetTriangles(submeshes[i].indexes, i); if (calculateNormals) { mesh.RecalculateNormals(); @@ -222,41 +223,32 @@ public class SkeletonComponent : MonoBehaviour { int indexCount = submeshQuadCount * 6; int vertexIndex = (endQuadCount - submeshQuadCount) * 4; - - int[] indexes; - if (submeshIndexes.Count > submeshIndex) { - indexes = submeshIndexes[submeshIndex]; - // Don't reallocate if existing indexes are right size. Skip setting vertices if already set correctly. - if (!lastSubmesh) { - if (indexes.Length == indexCount) { - if (submeshFirstVertex[submeshIndex] == vertexIndex) return; - } else - submeshIndexes[submeshIndex] = indexes = new int[indexCount]; - } else { - if (indexes.Length >= indexCount) { // Allow last submesh to have more indices than required. - if (submeshFirstVertex[submeshIndex] == vertexIndex) return; - } else - submeshIndexes[submeshIndex] = indexes = new int[indexCount]; + + if (submeshes.Count <= submeshIndex) submeshes.Add(new Submesh()); + Submesh submesh = submeshes[submeshIndex]; + + // Allocate indexes if not the right size, allowing last submesh to have more than required. + int[] indexes = submesh.indexes; + if (lastSubmesh ? (indexes.Length < indexCount) : (indexes.Length != indexCount)) + submesh.indexes = indexes = new int[indexCount]; + + // Set indexes if not already set. + if (submesh.firstVertex != vertexIndex || submesh.indexCount < indexCount) { + submesh.indexCount = indexCount; + submesh.firstVertex = vertexIndex; + for (int i = 0; i < indexCount; i += 6, vertexIndex += 4) { + indexes[i] = vertexIndex; + indexes[i + 1] = vertexIndex + 2; + indexes[i + 2] = vertexIndex + 1; + indexes[i + 3] = vertexIndex + 2; + indexes[i + 4] = vertexIndex + 3; + indexes[i + 5] = vertexIndex + 1; } - submeshFirstVertex[submeshIndex] = vertexIndex; - } else { - // Need new indexes. - indexes = new int[indexCount]; - submeshIndexes.Add(indexes); - submeshFirstVertex.Add(vertexIndex); } - for (int i = 0; i < indexCount; i += 6, vertexIndex += 4) { - indexes[i] = vertexIndex; - indexes[i + 1] = vertexIndex + 2; - indexes[i + 2] = vertexIndex + 1; - indexes[i + 3] = vertexIndex + 2; - indexes[i + 4] = vertexIndex + 3; - indexes[i + 5] = vertexIndex + 1; - } - - if (lastSubmesh) { - // Update vertices to the end. + // Last submesh may have more indices than required, so zero indexes to the end. + if (lastSubmesh && submesh.indexCount != indexCount) { + submesh.indexCount = indexCount; for (int i = indexCount, n = indexes.Length; i < n; i++) indexes[i] = 0; } @@ -291,3 +283,9 @@ public class SkeletonComponent : MonoBehaviour { } #endif } + +class Submesh { + public int[] indexes = new int[0]; + public int firstVertex = -1; + public int indexCount; +} diff --git a/spine-tk2d/Assets/examples/spineboy/spineboy.unity b/spine-tk2d/Assets/examples/spineboy/spineboy.unity index eaedafe36..eebc4b940 100644 Binary files a/spine-tk2d/Assets/examples/spineboy/spineboy.unity and b/spine-tk2d/Assets/examples/spineboy/spineboy.unity differ diff --git a/spine-unity/Assets/Spine/Editor/SkeletonAnimationInspector.cs b/spine-unity/Assets/Spine/Editor/SkeletonAnimationInspector.cs index 3fd552270..02f6b9264 100644 --- a/spine-unity/Assets/Spine/Editor/SkeletonAnimationInspector.cs +++ b/spine-unity/Assets/Spine/Editor/SkeletonAnimationInspector.cs @@ -60,9 +60,9 @@ public class SkeletonAnimationInspector : Editor { if (component.skeleton != null) { // Initial skin name. - String[] skins = new String[component.skeleton.Data.Skins.Count + 1]; + String[] skins = new String[component.skeleton.Data.Skins.Count]; int skinIndex = 0; - for (int i = 0; i < skins.Length - 1; i++) { + for (int i = 0; i < skins.Length; i++) { String name = component.skeleton.Data.Skins[i].Name; skins[i] = name; if (name == initialSkinName.stringValue) @@ -76,7 +76,7 @@ public class SkeletonAnimationInspector : Editor { EditorGUIUtility.LookLikeInspector(); EditorGUILayout.EndHorizontal(); - initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex]; + initialSkinName.stringValue = skins[skinIndex]; // Animation name. String[] animations = new String[component.skeleton.Data.Animations.Count + 2]; diff --git a/spine-unity/Assets/Spine/Editor/SkeletonComponentInspector.cs b/spine-unity/Assets/Spine/Editor/SkeletonComponentInspector.cs index 7fef19cc9..d3a6f9a5d 100644 --- a/spine-unity/Assets/Spine/Editor/SkeletonComponentInspector.cs +++ b/spine-unity/Assets/Spine/Editor/SkeletonComponentInspector.cs @@ -56,9 +56,9 @@ public class SkeletonComponentInspector : Editor { if (component.skeleton != null) { // Initial skin name. - String[] skins = new String[component.skeleton.Data.Skins.Count + 1]; + String[] skins = new String[component.skeleton.Data.Skins.Count]; int skinIndex = 0; - for (int i = 0; i < skins.Length - 1; i++) { + for (int i = 0; i < skins.Length; i++) { String name = component.skeleton.Data.Skins[i].Name; skins[i] = name; if (name == initialSkinName.stringValue) @@ -72,7 +72,7 @@ public class SkeletonComponentInspector : Editor { EditorGUIUtility.LookLikeInspector(); EditorGUILayout.EndHorizontal(); - initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex]; + initialSkinName.stringValue = skins[skinIndex]; } EditorGUILayout.PropertyField(timeScale); diff --git a/spine-unity/Assets/Spine/SkeletonComponent.cs b/spine-unity/Assets/Spine/SkeletonComponent.cs index 70273a37a..0634294a2 100644 --- a/spine-unity/Assets/Spine/SkeletonComponent.cs +++ b/spine-unity/Assets/Spine/SkeletonComponent.cs @@ -54,8 +54,7 @@ public class SkeletonComponent : MonoBehaviour { private Vector2[] uvs; private Material[] sharedMaterials = new Material[0]; private List submeshMaterials = new List(); - private List submeshIndexes = new List(); - private List submeshFirstVertex = new List(); + private List submeshes = new List(); private Vector4[] tangents = new Vector4[0]; public virtual void Clear () { @@ -77,7 +76,7 @@ public class SkeletonComponent : MonoBehaviour { skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData(false)); - if (initialSkinName != null && initialSkinName.Length > 0) { + if (initialSkinName != null && initialSkinName.Length > 0 && initialSkinName != "default") { skeleton.SetSkin(initialSkinName); skeleton.SetSlotsToSetupPose(); } @@ -150,7 +149,7 @@ public class SkeletonComponent : MonoBehaviour { mesh.Clear(); } else { // Too many vertices, zero the extra. - Vector3 zero = new Vector3(0, 0, 0); + Vector3 zero = Vector3.zero; for (int i = vertexCount, n = lastVertexCount; i < n; i++) vertices[i] = zero; } @@ -162,6 +161,7 @@ public class SkeletonComponent : MonoBehaviour { Color32[] colors = this.colors; int vertexIndex = 0; Color32 color = new Color32(); + float a = skeleton.A * 255, r = skeleton.R, g = skeleton.G, b = skeleton.B; for (int i = 0, n = drawOrder.Count; i < n; i++) { Slot slot = drawOrder[i]; RegionAttachment regionAttachment = slot.Attachment as RegionAttachment; @@ -175,10 +175,10 @@ public class SkeletonComponent : MonoBehaviour { vertices[vertexIndex + 2] = new Vector3(vertexPositions[RegionAttachment.X2], vertexPositions[RegionAttachment.Y2], 0); vertices[vertexIndex + 3] = new Vector3(vertexPositions[RegionAttachment.X3], vertexPositions[RegionAttachment.Y3], 0); - color.a = (byte)(skeleton.A * slot.A * 255); - color.r = (byte)(skeleton.R * slot.R * color.a); - color.g = (byte)(skeleton.G * slot.G * color.a); - color.b = (byte)(skeleton.B * slot.B * color.a); + color.a = (byte)(a * slot.A); + color.r = (byte)(r * slot.R * color.a); + color.g = (byte)(g * slot.G * color.a); + color.b = (byte)(b * slot.B * color.a); colors[vertexIndex] = color; colors[vertexIndex + 1] = color; colors[vertexIndex + 2] = color; @@ -195,10 +195,11 @@ public class SkeletonComponent : MonoBehaviour { mesh.vertices = vertices; mesh.colors32 = colors; mesh.uv = uvs; - - mesh.subMeshCount = submeshMaterials.Count; - for (int i = 0, n = mesh.subMeshCount; i < n; ++i) - mesh.SetTriangles(submeshIndexes[i], i); + + int submeshCount = submeshes.Count; + mesh.subMeshCount = submeshCount; + for (int i = 0; i < submeshCount; ++i) + mesh.SetTriangles(submeshes[i].indexes, i); if (calculateNormals) { mesh.RecalculateNormals(); @@ -222,41 +223,32 @@ public class SkeletonComponent : MonoBehaviour { int indexCount = submeshQuadCount * 6; int vertexIndex = (endQuadCount - submeshQuadCount) * 4; - - int[] indexes; - if (submeshIndexes.Count > submeshIndex) { - indexes = submeshIndexes[submeshIndex]; - // Don't reallocate if existing indexes are right size. Skip setting vertices if already set correctly. - if (!lastSubmesh) { - if (indexes.Length == indexCount) { - if (submeshFirstVertex[submeshIndex] == vertexIndex) return; - } else - submeshIndexes[submeshIndex] = indexes = new int[indexCount]; - } else { - if (indexes.Length >= indexCount) { // Allow last submesh to have more indices than required. - if (submeshFirstVertex[submeshIndex] == vertexIndex) return; - } else - submeshIndexes[submeshIndex] = indexes = new int[indexCount]; + + if (submeshes.Count <= submeshIndex) submeshes.Add(new Submesh()); + Submesh submesh = submeshes[submeshIndex]; + + // Allocate indexes if not the right size, allowing last submesh to have more than required. + int[] indexes = submesh.indexes; + if (lastSubmesh ? (indexes.Length < indexCount) : (indexes.Length != indexCount)) + submesh.indexes = indexes = new int[indexCount]; + + // Set indexes if not already set. + if (submesh.firstVertex != vertexIndex || submesh.indexCount < indexCount) { + submesh.indexCount = indexCount; + submesh.firstVertex = vertexIndex; + for (int i = 0; i < indexCount; i += 6, vertexIndex += 4) { + indexes[i] = vertexIndex; + indexes[i + 1] = vertexIndex + 2; + indexes[i + 2] = vertexIndex + 1; + indexes[i + 3] = vertexIndex + 2; + indexes[i + 4] = vertexIndex + 3; + indexes[i + 5] = vertexIndex + 1; } - submeshFirstVertex[submeshIndex] = vertexIndex; - } else { - // Need new indexes. - indexes = new int[indexCount]; - submeshIndexes.Add(indexes); - submeshFirstVertex.Add(vertexIndex); } - for (int i = 0; i < indexCount; i += 6, vertexIndex += 4) { - indexes[i] = vertexIndex; - indexes[i + 1] = vertexIndex + 2; - indexes[i + 2] = vertexIndex + 1; - indexes[i + 3] = vertexIndex + 2; - indexes[i + 4] = vertexIndex + 3; - indexes[i + 5] = vertexIndex + 1; - } - - if (lastSubmesh) { - // Update vertices to the end. + // Last submesh may have more indices than required, so zero indexes to the end. + if (lastSubmesh && submesh.indexCount != indexCount) { + submesh.indexCount = indexCount; for (int i = indexCount, n = indexes.Length; i < n; i++) indexes[i] = 0; } @@ -291,3 +283,9 @@ public class SkeletonComponent : MonoBehaviour { } #endif } + +class Submesh { + public int[] indexes = new int[0]; + public int firstVertex = -1; + public int indexCount; +} diff --git a/spine-unity/Assets/examples/goblins/goblins.unity b/spine-unity/Assets/examples/goblins/goblins.unity index f17db1cd6..431e59489 100644 Binary files a/spine-unity/Assets/examples/goblins/goblins.unity and b/spine-unity/Assets/examples/goblins/goblins.unity differ