Scripted, Optimized, Texture I/O

I am attempting to export textures attached to objects in the Unity Editor as image files, and then later download them from a web server and reattach them to their objects inside a web player. This is an essential component of the Whirld project.

If I gather the texture’s byte array for exporting using EncodeToPNG(), the textures have too large a filesize for my taste - and worse, when I load them in a web player, I cant find any way to re-add their mip maps.

Is it possible for me to export textures in the format that they are optimized to inside Unity’s build pipeline, while preserving their mipmaps and other properties that make them useful? If not, what are my options to overcome this difficulty?

Look up Texture2D.Apply in the docs.

–Eric

Thanks!

I can’t believe I missed that :slight_smile:

I’ll be looking for a .net image compression library to use while I am exporting images, then I should be good to go.

Sorry about the following screenshot… It’s the island demo world loaded inside a webplayer using the Whirld library - I just had to share it…

Looks pretty nifty!

–Eric

Based on the .net image class, I came up with the following code to compress images:

import System.Drawing;
import System.Drawing.Imaging;
image = Image.FromFile(whirld.path + tr.gameObject.name + "" + i + "o.png");
for(var info : ImageCodecInfo in ImageCodecInfo.GetImageEncoders()) if (info.FormatDescription == "JPEG") {
	var encoder = info;
	break;
}
var parms : EncoderParameters = new EncoderParameters(1);
parms.Param[0] = new EncoderParameter(Encoder.Compression, 40);
image.Save(whirld.path + tr.gameObject.name + "" + i + ".png", encoder, parms);

When run, this code generates the following error:

DllNotFoundException: gdiplus.dll
System.Drawing.Image.FromFile (System.String filename, Boolean useEmbeddedColorManagement)

I found a gdiplus.dll file on the internet, but it was 1.6 mb - and worse, dropping it in my project folder did nothing.

I haven’t yet found any other libraries to accomplish this. Is there a simple way to export compressed images from a Unity script?

Many thanks to anyone with a solution, or even a suggestion!

I don’t think you will get anywhere with the .NET image class - it isn’t a part of the Mono library that comes with Unity and you’ll most likely have problems if you try to find it from elsewhere.

PNG usually doesn’t compress anywhere near as well as JPG, so there isn’t much you can do to reduce the file size. If you are just saving the images yourself to a local file, you might be able to use a tool like ImageMagick to convert them from PNG to JPG in bulk (Unity can import JPG over the web, just not export). Otherwise, if you have Unity Pro, you might be able to use an image compression library written in C/C++ to encode your textures.

A bit of an underlining echo here: System.Drawing is not included in the runtime - nothing from there should be accessible.

Andeeee;

ImageMagick sounds like a somewhat viable option!
A custom C++ library sounds even better, but I don’t yet have Unity Pro.

While researching how to invoke ImageMagick from Unity, I have found several references to a C# .net wrapper called MagickNet - but it looks like it’s homepage is offline. I’ll post back here once I figure out how to make this work…