private void updateRectangles() { DateTime start = DateTime.Now; const int padding = 1; if (mPacker == null) { mPacker = new RectanglePacker((int)sliderWidth.value, (int)sliderHeight.value, padding); } else { mPacker.Reset((int)sliderWidth.value, (int)sliderHeight.value, padding); } for (int i = 0; i < RECTANGLE_COUNT; i++) { mPacker.InsertRectangle((int)mRectangles[i].width, (int)mRectangles[i].height, i); } mPacker.PackRectangles(); DateTime end = DateTime.Now; if (mPacker.rectangleCount > 0) { packingTimeText.text = mPacker.rectangleCount + " rectangles packed in " + (end - start).Milliseconds + "ms"; mTexture.SetPixels32(mFillColor); IntegerRectangle rect = new IntegerRectangle(); Color32[] tmpColor; for (int j = 0; j < mPacker.rectangleCount; j++) { rect = mPacker.GetRectangle(j, rect); int size = rect.width * rect.height; tmpColor = new Color32[size]; for (int k = 0; k < size; ++k) { tmpColor[k] = Color.black; } mTexture.SetPixels32(rect.x, rect.y, rect.width, rect.height, tmpColor); int index = mPacker.GetRectangleId(j); Color color = convertHexToRGBA((uint)(0xFF171703 + (((18 * ((index + 4) % 13)) << 16) + ((31 * ((index * 3) % 8)) << 8) + 63 * (((index + 1) * 3) % 5)))); size -= 4; tmpColor = new Color32[size]; for (int k = 0; k < size; ++k) { tmpColor[k] = color; } mTexture.SetPixels32(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2, tmpColor); } mTexture.Apply(); } }
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(); }