From 7d0833f881f76777e110dacca40c37e49ad5f33d Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Thu, 13 Apr 2023 17:57:12 +0200 Subject: [PATCH 1/4] [unity] Fixed SkeletonGraphic exception when Attachment changed after SkeletonGraphic.LateUpdate. Closes #2275. --- .../spine-unity/Components/SkeletonGraphic.cs | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs index ca174711e..d27cbbe0e 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs @@ -116,6 +116,7 @@ namespace Spine.Unity { public bool updateSeparatorPartLocation = true; private bool wasUpdatedAfterInit = true; + private bool requiresInstructionUpate = true; private Texture baseTexture = null; #if UNITY_EDITOR @@ -308,7 +309,10 @@ namespace Spine.Unity { public override void Rebuild (CanvasUpdate update) { base.Rebuild(update); if (canvasRenderer.cull) return; - if (update == CanvasUpdate.PreRender) UpdateMeshToInstructions(); + if (update == CanvasUpdate.PreRender) { + if (requiresInstructionUpate) PrepareInstructionsAndRenderers(isInRebuild : true); + UpdateMeshToInstructions(); + } if (allowMultipleCanvasRenderers) canvasRenderer.Clear(); } @@ -404,8 +408,7 @@ namespace Spine.Unity { if (updateMode != UpdateMode.FullUpdate) return; PrepareInstructionsAndRenderers(); - if (OnInstructionsPrepared != null) - OnInstructionsPrepared(this.currentInstructions); + SetVerticesDirty(); // triggers Rebuild and avoids potential double-update in a single frame } @@ -452,9 +455,11 @@ namespace Spine.Unity { public Skeleton Skeleton { get { Initialize(false); + requiresInstructionUpate = true; return skeleton; } set { + requiresInstructionUpate = true; skeleton = value; } } @@ -690,11 +695,12 @@ namespace Spine.Unity { OnAnimationRebuild(this); } - public void PrepareInstructionsAndRenderers () { + public void PrepareInstructionsAndRenderers (bool isInRebuild = false) { + requiresInstructionUpate = false; if (!this.allowMultipleCanvasRenderers) { MeshGenerator.GenerateSingleSubmeshInstruction(currentInstructions, skeleton, null); if (canvasRenderers.Count > 0) - DisableUnusedCanvasRenderers(usedCount: 0); + DisableUnusedCanvasRenderers(usedCount: 0, isInRebuild); usedRenderersCount = 0; } else { MeshGenerator.GenerateSkeletonRendererInstruction(currentInstructions, skeleton, null, @@ -707,8 +713,10 @@ namespace Spine.Unity { EnsureMeshesCount(submeshCount); EnsureUsedTexturesAndMaterialsCount(submeshCount); EnsureSeparatorPartCount(); - PrepareRendererGameObjects(currentInstructions); + PrepareRendererGameObjects(currentInstructions, isInRebuild); } + if (OnInstructionsPrepared != null) + OnInstructionsPrepared(this.currentInstructions); } public void UpdateMesh () { @@ -922,9 +930,11 @@ namespace Spine.Unity { } } - protected void PrepareRendererGameObjects (SkeletonRendererInstruction currentInstructions) { + protected void PrepareRendererGameObjects (SkeletonRendererInstruction currentInstructions, + bool isInRebuild = false) { + int submeshCount = currentInstructions.submeshInstructions.Count; - DisableUnusedCanvasRenderers(usedCount: submeshCount); + DisableUnusedCanvasRenderers(usedCount: submeshCount, isInRebuild); int separatorSlotGroupIndex = 0; int targetSiblingIndex = 0; @@ -961,13 +971,14 @@ namespace Spine.Unity { usedRenderersCount = submeshCount; } - protected void DisableUnusedCanvasRenderers (int usedCount) { + protected void DisableUnusedCanvasRenderers (int usedCount, bool isInRebuild = false) { #if UNITY_EDITOR RemoveNullCanvasRenderers(); #endif for (int i = usedCount; i < canvasRenderers.Count; i++) { canvasRenderers[i].Clear(); - canvasRenderers[i].gameObject.SetActive(false); + if (!isInRebuild) // rebuild does not allow disabling Graphic and thus removing it from rebuild list. + canvasRenderers[i].gameObject.SetActive(false); } } From 92728548b7402bea41f0bfe2a3fdcc2a2f7c5f97 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Thu, 13 Apr 2023 17:58:19 +0200 Subject: [PATCH 2/4] [unity] Minor: refactoring, variable renamed. --- .../spine-unity/Mesh Generation/MeshGenerator.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Mesh Generation/MeshGenerator.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Mesh Generation/MeshGenerator.cs index a58ebea2b..c9275c252 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Mesh Generation/MeshGenerator.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Mesh Generation/MeshGenerator.cs @@ -813,8 +813,8 @@ namespace Spine.Unity { b2.x *= alpha; b2.y = slot.Data.BlendMode == BlendMode.Additive ? 0 : alpha; } - int meshVertexCount = meshAttachment.WorldVerticesLength; - for (int iii = 0; iii < meshVertexCount; iii += 2) { + int verticesArrayLength = meshAttachment.WorldVerticesLength; + for (int iii = 0; iii < verticesArrayLength; iii += 2) { uv2i[vi] = rg; uv3i[vi] = b2; vi++; @@ -886,8 +886,8 @@ namespace Spine.Unity { } else { //if (settings.renderMeshes) { MeshAttachment meshAttachment = attachment as MeshAttachment; if (meshAttachment != null) { - int meshVertexCount = meshAttachment.WorldVerticesLength; - if (tempVerts.Length < meshVertexCount) this.tempVerts = tempVerts = new float[meshVertexCount]; + int verticesArrayLength = meshAttachment.WorldVerticesLength; + if (tempVerts.Length < verticesArrayLength) this.tempVerts = tempVerts = new float[verticesArrayLength]; meshAttachment.ComputeWorldVertices(slot, tempVerts); if (settings.pmaVertexColors) { @@ -917,7 +917,7 @@ namespace Spine.Unity { if (fy > bmax.y) bmax.y = fy; } - for (int iii = 0; iii < meshVertexCount; iii += 2) { + for (int iii = 0; iii < verticesArrayLength; iii += 2) { float x = tempVerts[iii], y = tempVerts[iii + 1]; vbi[vertexIndex].x = x; vbi[vertexIndex].y = y; vbi[vertexIndex].z = z; cbi[vertexIndex] = color; ubi[vertexIndex].x = attachmentUVs[iii]; ubi[vertexIndex].y = attachmentUVs[iii + 1]; From a09ec88b1624d5e0c9f1207cb347bda0d2a1b8d4 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Thu, 13 Apr 2023 18:08:09 +0200 Subject: [PATCH 3/4] [unity] Fixed compile error on old Unity version 2017.1, introduced by commit 7d0833f. See #2275. --- .../Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs index d27cbbe0e..07cb87315 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs @@ -700,7 +700,7 @@ namespace Spine.Unity { if (!this.allowMultipleCanvasRenderers) { MeshGenerator.GenerateSingleSubmeshInstruction(currentInstructions, skeleton, null); if (canvasRenderers.Count > 0) - DisableUnusedCanvasRenderers(usedCount: 0, isInRebuild); + DisableUnusedCanvasRenderers(usedCount: 0, isInRebuild : isInRebuild); usedRenderersCount = 0; } else { MeshGenerator.GenerateSkeletonRendererInstruction(currentInstructions, skeleton, null, @@ -934,7 +934,7 @@ namespace Spine.Unity { bool isInRebuild = false) { int submeshCount = currentInstructions.submeshInstructions.Count; - DisableUnusedCanvasRenderers(usedCount: submeshCount, isInRebuild); + DisableUnusedCanvasRenderers(usedCount: submeshCount, isInRebuild : isInRebuild); int separatorSlotGroupIndex = 0; int targetSiblingIndex = 0; From 245ff29b265be06f70791d38824e0924228ff304 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Thu, 13 Apr 2023 18:14:37 +0200 Subject: [PATCH 4/4] [unity] Minor: whitespace fix to please automatic checks. --- .../Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs index 07cb87315..1d150ffe1 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs @@ -310,7 +310,7 @@ namespace Spine.Unity { base.Rebuild(update); if (canvasRenderer.cull) return; if (update == CanvasUpdate.PreRender) { - if (requiresInstructionUpate) PrepareInstructionsAndRenderers(isInRebuild : true); + if (requiresInstructionUpate) PrepareInstructionsAndRenderers(isInRebuild: true); UpdateMeshToInstructions(); } if (allowMultipleCanvasRenderers) canvasRenderer.Clear(); @@ -700,7 +700,7 @@ namespace Spine.Unity { if (!this.allowMultipleCanvasRenderers) { MeshGenerator.GenerateSingleSubmeshInstruction(currentInstructions, skeleton, null); if (canvasRenderers.Count > 0) - DisableUnusedCanvasRenderers(usedCount: 0, isInRebuild : isInRebuild); + DisableUnusedCanvasRenderers(usedCount: 0, isInRebuild: isInRebuild); usedRenderersCount = 0; } else { MeshGenerator.GenerateSkeletonRendererInstruction(currentInstructions, skeleton, null, @@ -934,7 +934,7 @@ namespace Spine.Unity { bool isInRebuild = false) { int submeshCount = currentInstructions.submeshInstructions.Count; - DisableUnusedCanvasRenderers(usedCount: submeshCount, isInRebuild : isInRebuild); + DisableUnusedCanvasRenderers(usedCount: submeshCount, isInRebuild: isInRebuild); int separatorSlotGroupIndex = 0; int targetSiblingIndex = 0;