示例#1
0
        protected IEnumerator createPack(string savePath = "")
        {
            if (savePath != "")
            {
                if (deletePreviousCacheVersion && Directory.Exists(Application.persistentDataPath + "/AssetPacker/" + cacheName + "/"))
                {
                    foreach (string dirPath in Directory.GetDirectories(Application.persistentDataPath + "/AssetPacker/" + cacheName + "/", "*", SearchOption.AllDirectories))
                    {
                        Directory.Delete(dirPath, true);
                    }
                }

                Directory.CreateDirectory(savePath);
            }

            List <Texture2D> textures = new List <Texture2D>();
            List <string>    images   = new List <string>();

            foreach (TextureToPack itemToRaster in itemsToRaster)
            {
                WWW loader = new WWW("file:///" + itemToRaster.file);

                yield return(loader);

                textures.Add(loader.texture);
                images.Add(itemToRaster.id);
            }

            int textureSize = allow4096Textures ? 4096 : 2048;

            List <Rect> rectangles = new List <Rect>();

            for (int i = 0; i < textures.Count; i++)
            {
                if (textures[i].width > textureSize || textures[i].height > textureSize)
                {
                    throw new Exception("A texture size is bigger than the sprite sheet size!");
                }
                else
                {
                    rectangles.Add(new Rect(0, 0, textures[i].width, textures[i].height));
                }
            }

            const int padding = 1;

            int numSpriteSheet = 0;

            while (rectangles.Count > 0)
            {
                Texture2D texture   = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false);
                Color32[] fillColor = texture.GetPixels32();
                for (int i = 0; i < fillColor.Length; ++i)
                {
                    fillColor[i] = Color.clear;
                }

                RectanglePacker packer = new RectanglePacker(texture.width, texture.height, padding);

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

                packer.packRectangles();

                if (packer.rectangleCount > 0)
                {
                    texture.SetPixels32(fillColor);
                    IntegerRectangle    rect          = new IntegerRectangle();
                    List <TextureAsset> textureAssets = new List <TextureAsset>();

                    List <Rect>      garbageRect    = new List <Rect>();
                    List <Texture2D> garabeTextures = new List <Texture2D>();
                    List <string>    garbageImages  = new List <string>();

                    for (int j = 0; j < packer.rectangleCount; j++)
                    {
                        rect = packer.getRectangle(j, rect);

                        int index = packer.getRectangleId(j);

                        texture.SetPixels32(rect.x, rect.y, rect.width, rect.height, textures[index].GetPixels32());

                        TextureAsset textureAsset = new TextureAsset();
                        textureAsset.x      = rect.x;
                        textureAsset.y      = rect.y;
                        textureAsset.width  = rect.width;
                        textureAsset.height = rect.height;
                        textureAsset.name   = images[index];

                        textureAssets.Add(textureAsset);

                        garbageRect.Add(rectangles[index]);
                        garabeTextures.Add(textures[index]);
                        garbageImages.Add(images[index]);
                    }

                    foreach (Rect garbage in garbageRect)
                    {
                        rectangles.Remove(garbage);
                    }

                    foreach (Texture2D garbage in garabeTextures)
                    {
                        textures.Remove(garbage);
                    }

                    foreach (string garbage in garbageImages)
                    {
                        images.Remove(garbage);
                    }

                    texture.Apply();

                    if (savePath != "")
                    {
                        File.WriteAllBytes(savePath + "/data" + numSpriteSheet + ".png", texture.EncodeToPNG());
                        File.WriteAllText(savePath + "/data" + numSpriteSheet + ".json", JsonUtility.ToJson(new TextureAssets(textureAssets.ToArray())));
                        ++numSpriteSheet;
                    }

                    foreach (TextureAsset textureAsset in textureAssets)
                    {
                        mSprites.Add(textureAsset.name, Sprite.Create(texture, new Rect(textureAsset.x, textureAsset.y, textureAsset.width, textureAsset.height), Vector2.zero, pixelsPerUnit, 0, SpriteMeshType.FullRect));
                    }
                }
            }

            OnProcessCompleted.Invoke();
        }
示例#2
0
        protected IEnumerator createPack(string savePath = "")
        {
            if (savePath != "")
            {
                if (deletePreviousCacheVersion && Directory.Exists(Application.persistentDataPath + "/AssetPacker/" + cacheName + "/"))
                {
                    foreach (string dirPath in Directory.GetDirectories(Application.persistentDataPath + "/AssetPacker/" + cacheName + "/", "*", SearchOption.AllDirectories))
                    {
                        Directory.Delete(dirPath, true);
                    }
                }

                Directory.CreateDirectory(savePath);
            }

            List <Texture2D> textures = new List <Texture2D>();
            List <string>    images   = new List <string>();

            foreach (TextureToPack itemToRaster in itemsToRaster)
            {
                Texture2D baseTexture = itemToRaster.preLoadedTexture;

                if (!baseTexture)
                {
                    WWW loader = new WWW("file:///" + itemToRaster.file);

                    yield return(loader);

                    baseTexture = loader.texture;
                }

                if (itemToRaster.sliceParams != null)
                {
                    TextureToPack.GridSlice sliceParams = itemToRaster.sliceParams;

                    if (sliceParams.width > baseTexture.width || sliceParams.height > baseTexture.height)
                    {
                        throw new Exception(string.Format("Width and/or height of texture {0} is less than the provided slice parameters.", itemToRaster.id));
                    }

                    if (!(baseTexture.width % sliceParams.width == 0 && baseTexture.height % sliceParams.height == 0))
                    {
                        throw new Exception(string.Format("Width and/or height of texture {0} not a multiple of the provided slice parameters.", itemToRaster.id));
                    }

                    int totalRows    = baseTexture.height / sliceParams.height;
                    int totalColumns = baseTexture.width / sliceParams.width;

                    int textureIndex = 0;

                    // Scan each slice left to right, top to bottom
                    for (int rowIndex = 0; rowIndex < totalRows; ++rowIndex)
                    {
                        for (int columnIndex = 0; columnIndex < totalColumns; ++columnIndex)
                        {
                            int x = columnIndex * sliceParams.width;
                            int y = (totalRows - rowIndex - 1) * sliceParams.height;    // Starting from the top, not the bottom. Specific to GH3 sprite sheets, not nesacarily universal.

                            Color[]   pixels  = baseTexture.GetPixels(x, y, sliceParams.width, sliceParams.height);
                            Texture2D texture = new Texture2D(sliceParams.width, sliceParams.height);
                            texture.SetPixels(pixels);
                            texture.Apply();

                            textures.Add(texture);
                            images.Add(string.Format("{0}_{1}", itemToRaster.id, textureIndex.ToString("D8")));

                            ++textureIndex;
                        }
                    }
                }
                else
                {
                    textures.Add(baseTexture);
                    images.Add(itemToRaster.id);
                }
            }

            int textureSize = allow4096Textures ? 4096 : 2048;

            List <Rect> rectangles = new List <Rect>();

            for (int i = 0; i < textures.Count; i++)
            {
                if (textures[i].width > textureSize || textures[i].height > textureSize)
                {
                    throw new Exception("A texture size is bigger than the sprite sheet size!");
                }
                else
                {
                    rectangles.Add(new Rect(0, 0, textures[i].width, textures[i].height));
                }
            }

            const int padding = 1;

            int numSpriteSheet = 0;

            while (rectangles.Count > 0)
            {
                Texture2D texture   = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false);
                Color32[] fillColor = texture.GetPixels32();
                for (int i = 0; i < fillColor.Length; ++i)
                {
                    fillColor[i] = Color.clear;
                }

                RectanglePacker packer = new RectanglePacker(texture.width, texture.height, padding);

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

                packer.packRectangles();

                if (packer.rectangleCount > 0)
                {
                    texture.SetPixels32(fillColor);
                    IntegerRectangle    rect          = new IntegerRectangle();
                    List <TextureAsset> textureAssets = new List <TextureAsset>();

                    List <Rect>      garbageRect    = new List <Rect>();
                    List <Texture2D> garabeTextures = new List <Texture2D>();
                    List <string>    garbageImages  = new List <string>();

                    for (int j = 0; j < packer.rectangleCount; j++)
                    {
                        rect = packer.getRectangle(j, rect);

                        int index = packer.getRectangleId(j);

                        texture.SetPixels32(rect.x, rect.y, rect.width, rect.height, textures[index].GetPixels32());

                        TextureAsset textureAsset = new TextureAsset();
                        textureAsset.x      = rect.x;
                        textureAsset.y      = rect.y;
                        textureAsset.width  = rect.width;
                        textureAsset.height = rect.height;
                        textureAsset.name   = images[index];

                        textureAssets.Add(textureAsset);

                        garbageRect.Add(rectangles[index]);
                        garabeTextures.Add(textures[index]);
                        garbageImages.Add(images[index]);
                    }

                    foreach (Rect garbage in garbageRect)
                    {
                        rectangles.Remove(garbage);
                    }

                    foreach (Texture2D garbage in garabeTextures)
                    {
                        textures.Remove(garbage);
                    }

                    foreach (string garbage in garbageImages)
                    {
                        images.Remove(garbage);
                    }

                    texture.Apply();

                    if (savePath != "")
                    {
                        File.WriteAllBytes(savePath + "/data" + numSpriteSheet + ".png", texture.EncodeToPNG());
                        File.WriteAllText(savePath + "/data" + numSpriteSheet + ".json", JsonUtility.ToJson(new TextureAssets(textureAssets.ToArray()), true));
                        ++numSpriteSheet;
                    }

                    foreach (TextureAsset textureAsset in textureAssets)
                    {
                        Sprite sprite = Sprite.Create(texture, new Rect(textureAsset.x, textureAsset.y, textureAsset.width, textureAsset.height), spritePivot, pixelsPerUnit, 0, SpriteMeshType.FullRect);
                        sprite.name = textureAsset.name;
                        mSprites.Add(textureAsset.name, sprite);
                    }
                }
            }

            OnProcessCompleted.Invoke();
        }