Esempio n. 1
0
    /// <summary>
    /// Pack all of the specified sprites into a single texture, updating the outer and inner rects of the sprites as needed.
    /// </summary>

    static bool PackTextures(Texture2D tex, List <SpriteEntry> sprites)
    {
        Texture2D[] textures = new Texture2D[sprites.Count];
        Rect[]      rects;

#if UNITY_3_5 || UNITY_4_0
        int maxSize = 4096;
#else
        int maxSize = SystemInfo.maxTextureSize;
#endif

#if UNITY_ANDROID || UNITY_IPHONE
#if !UNITY_3_5
        if (PlayerSettings.targetGlesGraphics == TargetGlesGraphics.OpenGLES_1_x)
        {
            maxSize = Mathf.Min(maxSize, 1024);
        }
        else
#endif
        {
            maxSize = Mathf.Min(maxSize, NGUISettings.allow4096 ? 4096 : 2048);
        }
#endif

        if (NGUISettings.unityPacking)
        {
            for (int i = 0; i < sprites.Count; ++i)
            {
                textures[i] = sprites[i].tex;
            }
            rects = tex.PackTextures(textures, NGUISettings.atlasPadding, maxSize);
        }
        else
        {
            sprites.Sort(Compare);
            for (int i = 0; i < sprites.Count; ++i)
            {
                textures[i] = sprites[i].tex;
            }
            rects = NGUITexturePacker.PackTextures(tex, textures, 4, 4, NGUISettings.atlasPadding, maxSize);
        }

        for (int i = 0; i < sprites.Count; ++i)
        {
            Rect rect = NGUIMath.ConvertToPixels(rects[i], tex.width, tex.height, true);

            // Make sure that we don't shrink the textures
            if (Mathf.RoundToInt(rect.width) != textures[i].width)
            {
                return(false);
            }

            sprites[i].rect = rect;
            //BleedTexture(tex, sprites[i].rect);
        }
        return(true);
    }
    public static Rect[] PackTextures(Texture2D texture, Texture2D[] textures, int width, int height, int padding, int maxSize)
    {
        if (width > maxSize && height > maxSize)
        {
            return(null);
        }
        if (width > maxSize || height > maxSize)
        {
            int temp = width; width = height; height = temp;
        }

        // Force square by sizing up
        if (NGUISettings.forceSquareAtlas)
        {
            if (width > height)
            {
                height = width;
            }
            else if (height > width)
            {
                width = height;
            }
        }
        NGUITexturePacker bp = new NGUITexturePacker(width, height, false);

        Storage[] storage = new Storage[textures.Length];

        for (int i = 0; i < textures.Length; i++)
        {
            Texture2D tex = textures[i];

            Rect rect = new Rect();

            int xPadding = 1;
            int yPadding = 1;

            for (xPadding = 1; xPadding >= 0; --xPadding)
            {
                for (yPadding = 1; yPadding >= 0; --yPadding)
                {
                    rect = bp.Insert(tex.width + (xPadding * padding), tex.height + (yPadding * padding),
                                     NGUITexturePacker.FreeRectChoiceHeuristic.RectBestAreaFit);
                    if (rect.width != 0 && rect.height != 0)
                    {
                        break;
                    }

                    // After having no padding if it still doesn't fit -- increase texture size.
                    else if (xPadding == 0 && yPadding == 0)
                    {
                        return(PackTextures(texture, textures, width * (width <= height ? 2 : 1),
                                            height * (height < width ? 2 : 1), padding, maxSize));
                    }
                }
                if (rect.width != 0 && rect.height != 0)
                {
                    break;
                }
            }

            storage[i]          = new Storage();
            storage[i].rect     = rect;
            storage[i].paddingX = (xPadding != 0);
            storage[i].paddingY = (yPadding != 0);
        }

        texture.Resize(width, height);
        texture.SetPixels(new Color[width * height]);

        // The returned rects
        Rect[] rects = new Rect[textures.Length];

        for (int i = 0; i < textures.Length; i++)
        {
            Texture2D tex      = textures[i];
            Rect      rect     = storage[i].rect;
            int       xPadding = (storage[i].paddingX ? padding : 0);
            int       yPadding = (storage[i].paddingY ? padding : 0);
            Color[]   colors   = tex.GetPixels();

            // Would be used to rotate the texture if need be.
            if (rect.width != tex.width + xPadding)
            {
                Color[] newColors = tex.GetPixels();

                for (int x = 0; x < rect.width; x++)
                {
                    for (int y = 0; y < rect.height; y++)
                    {
                        int prevIndex = ((int)rect.height - (y + 1)) + x * (int)tex.width;
                        newColors[x + y * (int)rect.width] = colors[prevIndex];
                    }
                }

                colors = newColors;
            }

            texture.SetPixels((int)rect.x, (int)rect.y, (int)rect.width - xPadding, (int)rect.height - yPadding, colors);
            rect.x     /= width;
            rect.y     /= height;
            rect.width  = (rect.width - xPadding) / width;
            rect.height = (rect.height - yPadding) / height;
            rects[i]    = rect;
        }
        return(rects);
    }
    public static Rect[] PackTextures(Texture2D texture, Texture2D[] textures, int width, int height, int padding, int maxSize)
    {
        if (width > maxSize && height > maxSize) return null;
        if (width > maxSize || height > maxSize) { int temp = width; width = height; height = temp; }

        // Force square by sizing up
        if (NGUISettings.forceSquareAtlas)
        {
            if (width > height)
                height = width;
            else if (height > width)
                width = height;
        }
        NGUITexturePacker bp = new NGUITexturePacker(width, height, false);
        Storage[] storage = new Storage[textures.Length];

        for (int i = 0; i < textures.Length; i++)
        {
            Texture2D tex = textures[i];

            Rect rect = new Rect();

            int xPadding = 1;
            int yPadding = 1;

            for (xPadding = 1; xPadding >= 0; --xPadding)
            {
                for (yPadding = 1; yPadding >= 0; --yPadding)
                {
                    rect = bp.Insert(tex.width + (xPadding * padding), tex.height + (yPadding * padding),
                        NGUITexturePacker.FreeRectChoiceHeuristic.RectBestAreaFit);
                    if (rect.width != 0 && rect.height != 0) break;

                    // After having no padding if it still doesn't fit -- increase texture size.
                    else if (xPadding == 0 && yPadding == 0)
                    {
                        return PackTextures(texture, textures, width * (width <= height ? 2 : 1),
                            height * (height < width ? 2 : 1), padding, maxSize);
                    }
                }
                if (rect.width != 0 && rect.height != 0) break;
            }

            storage[i] = new Storage();
            storage[i].rect = rect;
            storage[i].paddingX = (xPadding != 0);
            storage[i].paddingY = (yPadding != 0);
        }

        texture.Resize(width, height);
        texture.SetPixels(new Color[width * height]);

        // The returned rects
        Rect[] rects = new Rect[textures.Length];

        for (int i = 0; i < textures.Length; i++)
        {
            Texture2D tex = textures[i];
            Rect rect = storage[i].rect;
            int xPadding = (storage[i].paddingX ? padding : 0);
            int yPadding = (storage[i].paddingY ? padding : 0);
            Color[] colors = tex.GetPixels();

            // Would be used to rotate the texture if need be.
            if (rect.width != tex.width + xPadding)
            {
                Color[] newColors = tex.GetPixels();

                for (int x = 0; x < rect.width; x++)
                {
                    for (int y = 0; y < rect.height; y++)
                    {
                        int prevIndex = ((int)rect.height - (y + 1)) + x * (int)tex.width;
                        newColors[x + y * (int)rect.width] = colors[prevIndex];
                    }
                }

                colors = newColors;
            }

            texture.SetPixels((int)rect.x, (int)rect.y, (int)rect.width - xPadding, (int)rect.height - yPadding, colors);
            rect.x /= width;
            rect.y /= height;
            rect.width = (rect.width - xPadding) / width;
            rect.height = (rect.height - yPadding) / height;
            rects[i] = rect;
        }
        return rects;
    }