Ejemplo n.º 1
0
        public bool CreateAtlas(Texture[] textures, int width = 1024, int height = 1024, int padding = 1)
        {
            bool result = false;

            rectangles.Clear();
            dictionary.Clear();
            packer.ResetAll(width, height, padding);

            CreateMaterial();
            CreateRenderTexture(width, height);

            for (int i = 0; i < textures.Length; i++)
            {
                rectangles.Add(new Rect(0, 0, textures[i].width, textures[i].height));
            }

            for (int i = 0; i < rectangles.Count; i++)
            {
                packer.insertRectangle((int)rectangles[i].width, (int)rectangles[i].height, i);
            }

            packer.packRectangles();

            int index = 0;

            if (packer.rectangleCount > 0)
            {
                for (int j = 0; j < packer.rectangleCount; j++)
                {
                    integerRect = packer.getRectangle(j, integerRect);
                    index       = packer.getRectangleId(j);

                    if (!dictionary.ContainsKey(textures[index].name))
                    {
                        Rect rect = new Rect();
                        rect.x      = (float)integerRect.x / width;
                        rect.y      = (float)integerRect.y / height;
                        rect.width  = (float)integerRect.width / width;
                        rect.height = (float)integerRect.height / height;

                        dictionary.Add(textures[index].name, rect);
                    }

                    DrawTexture(textures[index], renderTexture,
                                new Rect(integerRect.x, integerRect.y, integerRect.width, integerRect.height));
                }

                result = true;
            }

            return(result);
        }