예제 #1
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();
                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);
        }
예제 #2
0
        internal static Texture2D ToTexture(this AtlasRegion ar, bool applyImmediately = true)
        {
            Texture2D sourceTexture = ar.GetMainTexture();
            Rect      r             = ar.GetUnityRect(sourceTexture.height);
            Texture2D output        = new Texture2D((int)r.width, (int)r.height);

            output.name = ar.name;
            Color[] pixelBuffer = sourceTexture.GetPixels((int)r.x, (int)r.y, (int)r.width, (int)r.height);
            output.SetPixels(pixelBuffer);

            if (applyImmediately)
            {
                output.Apply();
            }

            return(output);
        }
예제 #3
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, 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();
            }

            return(output);
        }
예제 #4
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);
        }
예제 #5
0
        //叠加材质上的颜色
        public static Texture2D ToColorTexture(this AtlasRegion ar, Material material,
                                               TextureFormat textureFormat = TextureFormat.RGBA32, bool mipmaps = false)
        {
            if (material == null || !material.HasProperty(BlendColorVal))
            {
                return(ar.ToTexture(true, textureFormat, mipmaps));
            }

            var blendColor = material.GetColor(BlendColorVal);
            Dictionary <Color, Texture2D> colorDic;

            if (!CachedRegionTextures.TryGetValue(ar, out colorDic))
            {
                colorDic = new Dictionary <Color, Texture2D>();
                CachedRegionTextures.Add(ar, colorDic);
            }

            Texture2D tex2D;

            if (!colorDic.TryGetValue(blendColor, out tex2D))
            {
                var sourceTexture = ar.GetMainTexture();
                var r             = ar.GetUnityRect(sourceTexture.height);
                var width         = (int)r.width;
                var height        = (int)r.height;
                tex2D      = new Texture2D(width, height, textureFormat, mipmaps);
                tex2D.name = ar.name;
                var pixelBuffer = sourceTexture.GetPixels((int)r.x, (int)r.y, width, height);
                for (int i = 0; i < pixelBuffer.Length; i++)
                {
                    pixelBuffer[i] = pixelBuffer[i].Overlay(blendColor);
                }

                tex2D.SetPixels(pixelBuffer);
                tex2D.Apply();
                colorDic.Add(blendColor, tex2D);
            }

            return(tex2D);
        }
예제 #6
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)
        {
            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)
                {
                    name = ar.name
                };
                AtlasUtilities.CopyTexture(sourceTexture, r, output);
                CachedRegionTextures.Add(ar, output);
                CachedRegionTexturesList.Add(output);
            }

            return(output);
        }
예제 #7
0
 public static Texture2D ToTexture(this AtlasRegion ar, bool applyImmediately = true, TextureFormat textureFormat = TextureFormat.RGBA32, bool mipmaps = false)
 {
     CachedRegionTextures.TryGetValue(ar, out Texture2D value);
     if (value == null)
     {
         Texture2D mainTexture = ar.GetMainTexture();
         Rect      unityRect   = ar.GetUnityRect(mainTexture.height);
         int       num         = (int)unityRect.width;
         int       num2        = (int)unityRect.height;
         value      = new Texture2D(num, num2, textureFormat, mipmaps);
         value.name = ar.name;
         Color[] pixels = mainTexture.GetPixels((int)unityRect.x, (int)unityRect.y, num, num2);
         value.SetPixels(pixels);
         CachedRegionTextures.Add(ar, value);
         CachedRegionTexturesList.Add(value);
         if (applyImmediately)
         {
             value.Apply();
         }
     }
     return(value);
 }
예제 #8
0
 public static Texture2D ToTexture(this AtlasRegion ar, bool applyImmediately = true, TextureFormat textureFormat = 4, bool mipmaps = false)
 {
     CachedRegionTextures.TryGetValue(ar, out Texture2D textured);
     if (textured == null)
     {
         Texture2D mainTexture = ar.GetMainTexture();
         Rect      unityRect   = ar.GetUnityRect(mainTexture.height);
         int       width       = (int)unityRect.width;
         int       height      = (int)unityRect.height;
         textured = new Texture2D(width, height, textureFormat, mipmaps)
         {
             name = ar.name
         };
         Color[] colors = mainTexture.GetPixels((int)unityRect.x, (int)unityRect.y, width, height);
         textured.SetPixels(colors);
         CachedRegionTextures.Add(ar, textured);
         CachedRegionTexturesList.Add(textured);
         if (applyImmediately)
         {
             textured.Apply();
         }
     }
     return(textured);
 }
예제 #9
0
 public static Sprite ToSprite(this AtlasRegion ar, float pixelsPerUnit = 100)
 {
     return(Sprite.Create(ar.GetMainTexture(), ar.GetUnityRect(), new Vector2(0.5f, 0.5f), pixelsPerUnit));
 }