[unity] Dispose temp textures after repacking.

This commit is contained in:
John 2017-03-31 02:01:14 +08:00 committed by GitHub
parent b8a603d6af
commit 48173c6d85

View File

@ -305,9 +305,11 @@ namespace Spine.Unity.Modules.AttachmentTools {
var skinAttachments = o.Attachments;
var newSkin = new Skin(newName);
// Use these to detect and use shared regions.
var existingRegions = new Dictionary<AtlasRegion, int>();
var regionIndexes = new List<int>();
// Collect all textures from the attachments of the original skin.
var repackedAttachments = new List<Attachment>();
var texturesToPack = new List<Texture2D>();
var originalRegions = new List<AtlasRegion>();
@ -334,11 +336,13 @@ namespace Spine.Unity.Modules.AttachmentTools {
newSkin.AddAttachment(key.slotIndex, key.name, newAttachment);
}
// Fill a new texture with the collected attachment textures.
var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps);
newTexture.anisoLevel = texturesToPack[0].anisoLevel;
newTexture.name = newName;
var rects = newTexture.PackTextures(texturesToPack.ToArray(), padding, maxAtlasSize);
// Rehydrate the repacked textures as a Material, Spine atlas and Spine.AtlasAttachments
var newMaterial = new Material(shader);
newMaterial.name = newName;
newMaterial.mainTexture = newTexture;
@ -352,11 +356,16 @@ namespace Spine.Unity.Modules.AttachmentTools {
repackedRegions.Add(newRegion);
}
// Map the cloned attachments to the repacked atlas.
for (int i = 0, n = repackedAttachments.Count; i < n; i++) {
var a = repackedAttachments[i];
a.SetRegion(repackedRegions[regionIndexes[i]]);
}
// Clean up
foreach (var ttp in texturesToPack)
UnityEngine.Object.Destroy(ttp);
t = newTexture;
m = newMaterial;
return newSkin;