/// <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);
        }
Exemple #2
0
        /// <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);
        }