/// <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, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, int texturePropertyId = 0, bool linear = false, bool applyPMA = false) { Texture2D output; IntAndAtlasRegionKey cacheKey = new IntAndAtlasRegionKey(texturePropertyId, ar); CachedRegionTextures.TryGetValue(cacheKey, out output); if (output == null) { Texture2D sourceTexture = texturePropertyId == 0 ? ar.GetMainTexture() : ar.GetTexture(texturePropertyId); Rect r = ar.GetUnityRect(); int width = (int)r.width; int height = (int)r.height; output = new Texture2D(width, height, textureFormat, mipmaps, linear) { name = ar.name }; output.CopyTextureAttributesFrom(sourceTexture); if (applyPMA) { AtlasUtilities.CopyTextureApplyPMA(sourceTexture, r, output); } else { AtlasUtilities.CopyTexture(sourceTexture, r, output); } CachedRegionTextures.Add(cacheKey, output); CachedRegionTexturesList.Add(output); } return(output); }
static Texture2D GetClone(this Texture2D t, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, bool linear = false, bool applyPMA = false) { var newTexture = new Texture2D((int)t.width, (int)t.height, textureFormat, mipmaps, linear); newTexture.CopyTextureAttributesFrom(t); if (applyPMA) { AtlasUtilities.CopyTextureApplyPMA(t, new Rect(0, 0, t.width, t.height), newTexture); } else { AtlasUtilities.CopyTexture(t, new Rect(0, 0, t.width, t.height), newTexture); } return(newTexture); }
static Texture2D ToTexture(this Sprite s, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, bool linear = false, bool applyPMA = false) { var spriteTexture = s.texture; var r = s.textureRect; var newTexture = new Texture2D((int)r.width, (int)r.height, textureFormat, mipmaps, linear); newTexture.CopyTextureAttributesFrom(spriteTexture); if (applyPMA) { AtlasUtilities.CopyTextureApplyPMA(spriteTexture, r, newTexture); } else { AtlasUtilities.CopyTexture(spriteTexture, r, newTexture); } return(newTexture); }
/// <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, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, int texturePropertyId = 0, bool linear = false, bool applyPMA = false) { Texture2D output; IntAndAtlasRegionKey cacheKey = new IntAndAtlasRegionKey(texturePropertyId, ar); CachedRegionTextures.TryGetValue(cacheKey, out output); if (output == null) { Texture2D sourceTexture = texturePropertyId == 0 ? ar.GetMainTexture() : ar.GetTexture(texturePropertyId); Rect r = ar.GetUnityRect(); // Compensate any image resizing due to Texture 'Max Size' import settings. // sourceTexture.width returns the resized image dimensions, at least in newer Unity versions. if (sourceTexture.width < ar.page.width) { float scaleX = (float)(sourceTexture.width) / (float)(ar.page.width); float scaleY = (float)(sourceTexture.height) / (float)(ar.page.height); var scale = new Vector2(scaleX, scaleY); r = new Rect(Vector2.Scale(r.position, scale), Vector2.Scale(r.size, scale)); } int width = (int)r.width; int height = (int)r.height; output = new Texture2D(width, height, textureFormat, mipmaps, linear) { name = ar.name }; output.CopyTextureAttributesFrom(sourceTexture); if (applyPMA) { AtlasUtilities.CopyTextureApplyPMA(sourceTexture, r, output); } else { AtlasUtilities.CopyTexture(sourceTexture, r, output); } CachedRegionTextures.Add(cacheKey, output); CachedRegionTexturesList.Add(output); } return(output); }
static Texture2D ToTexture(this Sprite s, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, bool linear = false, bool applyPMA = false) { var spriteTexture = s.texture; Rect r; if (!s.packed || s.packingMode == SpritePackingMode.Rectangle) { r = s.textureRect; } else { r = new Rect(); r.xMin = Math.Min(s.uv[0].x, s.uv[1].x) * spriteTexture.width; r.xMax = Math.Max(s.uv[0].x, s.uv[1].x) * spriteTexture.width; r.yMin = Math.Min(s.uv[0].y, s.uv[2].y) * spriteTexture.height; r.yMax = Math.Max(s.uv[0].y, s.uv[2].y) * spriteTexture.height; #if UNITY_EDITOR if (s.uv.Length > 4) { Debug.LogError("When using a tightly packed SpriteAtlas with Spine, you may only access Sprites that are packed as 'FullRect' from it! " + "You can either disable 'Tight Packing' at the whole SpriteAtlas, or change the single Sprite's TextureImporter Setting 'MeshType' to 'Full Rect'." + "Sprite Asset: " + s.name, s); } #endif } var newTexture = new Texture2D((int)r.width, (int)r.height, textureFormat, mipmaps, linear); newTexture.CopyTextureAttributesFrom(spriteTexture); if (applyPMA) { AtlasUtilities.CopyTextureApplyPMA(spriteTexture, r, newTexture); } else { AtlasUtilities.CopyTexture(spriteTexture, r, newTexture); } return(newTexture); }