diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/AtlasUtilities.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/AtlasUtilities.cs index 2fd359ec9..4aa838e96 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/AtlasUtilities.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/AtlasUtilities.cs @@ -228,6 +228,12 @@ namespace Spine.Unity.AttachmentTools { } #region Runtime Repacking + static readonly Dictionary existingRegions = new Dictionary(); + static readonly List regionIndices = new List(); + static readonly List originalRegions = new List(); + static readonly List repackedRegions = new List(); + static List[] texturesToPackAtParam = new List[1]; + /// /// Fills the outputAttachments list with new attachment objects based on the attachments in sourceAttachments, /// but mapped to a new single texture using the same material. @@ -312,17 +318,21 @@ namespace Spine.Unity.AttachmentTools { } // Use these to detect and use shared regions. - var existingRegions = new Dictionary(); - var regionIndexes = new List(); + existingRegions.Clear(); + regionIndices.Clear(); // Collect all textures from original attachments. int numTextureParamsToRepack = 1 + (additionalTexturePropertyIDsToCopy == null ? 0 : additionalTexturePropertyIDsToCopy.Length); additionalOutputTextures = (additionalTexturePropertyIDsToCopy == null ? null : new Texture2D[additionalTexturePropertyIDsToCopy.Length]); - List[] texturesToPackAtParam = new List[numTextureParamsToRepack]; + if (texturesToPackAtParam.Length < numTextureParamsToRepack) + Array.Resize(ref texturesToPackAtParam, numTextureParamsToRepack); for (int i = 0; i < numTextureParamsToRepack; ++i) { - texturesToPackAtParam[i] = new List(); + if (texturesToPackAtParam[i] != null) + texturesToPackAtParam[i].Clear(); + else + texturesToPackAtParam[i] = new List(); } - var originalRegions = new List(); + originalRegions.Clear(); if (!object.ReferenceEquals(sourceAttachments, outputAttachments)) { outputAttachments.Clear(); @@ -339,7 +349,7 @@ namespace Spine.Unity.AttachmentTools { var region = ((IHasRendererObject)newAttachment).RendererObject as AtlasRegion; int existingIndex; if (existingRegions.TryGetValue(region, out existingIndex)) { - regionIndexes.Add(existingIndex); + regionIndices.Add(existingIndex); } else { originalRegions.Add(region); @@ -353,7 +363,7 @@ namespace Spine.Unity.AttachmentTools { } existingRegions.Add(region, newRegionIndex); - regionIndexes.Add(newRegionIndex); + regionIndices.Add(newRegionIndex); newRegionIndex++; } @@ -361,7 +371,7 @@ namespace Spine.Unity.AttachmentTools { } else { outputAttachments[attachmentIndex] = useOriginalNonrenderables ? originalAttachment : originalAttachment.Copy(); - regionIndexes.Add(NonrenderingRegion); // Output attachments pairs with regionIndexes list 1:1. Pad with a sentinel if the attachment doesn't have a region. + regionIndices.Add(NonrenderingRegion); // Output attachments pairs with regionIndices list 1:1. Pad with a sentinel if the attachment doesn't have a region. } } @@ -404,7 +414,7 @@ namespace Spine.Unity.AttachmentTools { var page = newMaterial.ToSpineAtlasPage(); page.name = newAssetName; - var repackedRegions = new List(); + repackedRegions.Clear(); for (int i = 0, n = originalRegions.Count; i < n; i++) { var oldRegion = originalRegions[i]; var newRegion = UVRectToAtlasRegion(rects[i], oldRegion, page); @@ -415,7 +425,7 @@ namespace Spine.Unity.AttachmentTools { for (int i = 0, n = outputAttachments.Count; i < n; i++) { var a = outputAttachments[i]; if (a is IHasRendererObject) - a.SetRegion(repackedRegions[regionIndexes[i]]); + a.SetRegion(repackedRegions[regionIndices[i]]); } // Clean up.