private static void PackTextures(Texture2D[] textures, string path, ref List <MadAtlas.Item> items)
    {
        int padding = 2;

        var atlasTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
        var rects        = MadTexturePacker.PackTextures(atlasTexture, textures, 256, 256, padding, 4096);

        //var rects = atlasTexture.PackTextures(textures, padding, 4096);

        if (atlasTexture.format != TextureFormat.ARGB32)
        {
            // need to rewrite texture to a new one
            var newAtlasTexture = new Texture2D(atlasTexture.width, atlasTexture.height, TextureFormat.ARGB32, false);
            newAtlasTexture.SetPixels32(atlasTexture.GetPixels32());
            newAtlasTexture.Apply();
            DestroyImmediate(atlasTexture);
            atlasTexture = newAtlasTexture;
        }

        var bytes = atlasTexture.EncodeToPNG();

        DestroyImmediate(atlasTexture);

        File.WriteAllBytes(path, bytes);
        AssetDatabase.Refresh();

        for (int i = 0; i < textures.Length; ++i)
        {
            var    texture = textures[i];
            var    region  = rects[i];
            string guid    = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(texture));
            var    item    = (from el in items where el.textureGUID == guid select el).FirstOrDefault();

            if (item != null)
            {
                item.region = region;
            }
            else
            {
                item = CreateItem(texture, region);
                items.Add(item);
            }
        }

        // set texture max size to 4086
        var importer = TextureImporter.GetAtPath(path) as TextureImporter;

        importer.maxTextureSize = 4086;
        importer.isReadable     = true;
        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    }
Esempio n. 2
0
	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; }
		
		MadTexturePacker bp = new MadTexturePacker(width, height, false);
		Storage[] storage = new Storage[textures.Length];

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

			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),
						MadTexturePacker.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];
			if (!tex) continue;

			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;
		}
		texture.Apply();
		return rects;
	}