From 70cebf6dccae4e12c807c85cfec597a0a60afa05 Mon Sep 17 00:00:00 2001 From: pharan Date: Wed, 1 Aug 2018 17:01:21 +0800 Subject: [PATCH] [unity] Use nonallocating texture copy. --- .../Modules/AttachmentTools/AttachmentTools.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Modules/AttachmentTools/AttachmentTools.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Modules/AttachmentTools/AttachmentTools.cs index 601f06685..ff9d3d858 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Modules/AttachmentTools/AttachmentTools.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Modules/AttachmentTools/AttachmentTools.cs @@ -595,10 +595,8 @@ namespace Spine.Unity.Modules.AttachmentTools { 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); + output = new Texture2D(width, height, textureFormat, mipmaps) { name = ar.name }; + AtlasUtilities.CopyTexture(sourceTexture, r, output); CachedRegionTextures.Add(ar, output); CachedRegionTexturesList.Add(output); @@ -612,9 +610,8 @@ namespace Spine.Unity.Modules.AttachmentTools { static Texture2D ToTexture (this Sprite s, bool applyImmediately = true, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) { var spriteTexture = s.texture; var r = s.textureRect; - var spritePixels = spriteTexture.GetPixels((int)r.x, (int)r.y, (int)r.width, (int)r.height); var newTexture = new Texture2D((int)r.width, (int)r.height, textureFormat, mipmaps); - newTexture.SetPixels(spritePixels); + AtlasUtilities.CopyTexture(spriteTexture, r, newTexture); if (applyImmediately) newTexture.Apply(); @@ -623,9 +620,8 @@ namespace Spine.Unity.Modules.AttachmentTools { } static Texture2D GetClone (this Texture2D t, bool applyImmediately = true, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) { - var spritePixels = t.GetPixels(0, 0, (int)t.width, (int)t.height); var newTexture = new Texture2D((int)t.width, (int)t.height, textureFormat, mipmaps); - newTexture.SetPixels(spritePixels); + AtlasUtilities.CopyTexture(t, new Rect(0, 0, t.width, t.height), newTexture); if (applyImmediately) newTexture.Apply(); @@ -633,6 +629,10 @@ namespace Spine.Unity.Modules.AttachmentTools { return newTexture; } + static void CopyTexture (Texture2D source, Rect sourceRect, Texture2D destination) { + Graphics.CopyTexture(source, 0, 0, (int)sourceRect.x, (int)sourceRect.y, (int)sourceRect.width, (int)sourceRect.height, destination, 0, 0, 0, 0); + } + static bool IsRenderable (Attachment a) { return a is IHasRendererObject; }