|
@@ -469,9 +469,9 @@ namespace Spine.Unity.Modules.AttachmentTools {
|
|
|
a.SetRegion(repackedRegions[regionIndexes[i]]);
|
|
|
}
|
|
|
|
|
|
- // Clean up
|
|
|
- foreach (var ttp in texturesToPack)
|
|
|
- UnityEngine.Object.Destroy(ttp);
|
|
|
+// // Clean up
|
|
|
+// foreach (var ttp in texturesToPack)
|
|
|
+// UnityEngine.Object.Destroy(ttp);
|
|
|
|
|
|
t = newTexture;
|
|
|
m = newMaterial;
|
|
@@ -482,20 +482,38 @@ namespace Spine.Unity.Modules.AttachmentTools {
|
|
|
return Sprite.Create(ar.GetMainTexture(), ar.GetUnityRect(), new Vector2(0.5f, 0.5f), pixelsPerUnit);
|
|
|
}
|
|
|
|
|
|
+ static Dictionary<AtlasRegion, Texture2D> CachedRegionTextures = new Dictionary<AtlasRegion, Texture2D>();
|
|
|
+ static List<Texture2D> CachedRegionTexturesList = new List<Texture2D>();
|
|
|
+
|
|
|
+ public static void ClearCache () {
|
|
|
+ foreach (var t in CachedRegionTexturesList) {
|
|
|
+ UnityEngine.Object.Destroy(t);
|
|
|
+ }
|
|
|
+ CachedRegionTextures.Clear();
|
|
|
+ CachedRegionTexturesList.Clear();
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>Creates a new Texture2D object based on an AtlasRegion.
|
|
|
/// If applyImmediately is true, Texture2D.Apply is called immediately after the Texture2D is filled with data.</summary>
|
|
|
public static Texture2D ToTexture (this AtlasRegion ar, bool applyImmediately = true, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) {
|
|
|
- Texture2D sourceTexture = ar.GetMainTexture();
|
|
|
- Rect r = ar.GetUnityRect(sourceTexture.height);
|
|
|
- int width = (int)r.width;
|
|
|
- int height = (int)r.height;
|
|
|
- Texture2D output = new Texture2D(width, height, textureFormat, mipmaps);
|
|
|
- output.name = ar.name;
|
|
|
- Color[] pixelBuffer = sourceTexture.GetPixels((int)r.x, (int)r.y, width, height);
|
|
|
- output.SetPixels(pixelBuffer);
|
|
|
-
|
|
|
- if (applyImmediately)
|
|
|
- output.Apply();
|
|
|
+ Texture2D output;
|
|
|
+
|
|
|
+ CachedRegionTextures.TryGetValue(ar, out output);
|
|
|
+ if (output == null) {
|
|
|
+ Texture2D sourceTexture = ar.GetMainTexture();
|
|
|
+ Rect r = ar.GetUnityRect(sourceTexture.height);
|
|
|
+ int width = (int)r.width;
|
|
|
+ int height = (int)r.height;
|
|
|
+ output = new Texture2D(width, height, textureFormat, mipmaps);
|
|
|
+ output.name = ar.name;
|
|
|
+ Color[] pixelBuffer = sourceTexture.GetPixels((int)r.x, (int)r.y, width, height);
|
|
|
+ output.SetPixels(pixelBuffer);
|
|
|
+ CachedRegionTextures.Add(ar, output);
|
|
|
+ CachedRegionTexturesList.Add(output);
|
|
|
+
|
|
|
+ if (applyImmediately)
|
|
|
+ output.Apply();
|
|
|
+ }
|
|
|
|
|
|
return output;
|
|
|
}
|