Howto save ProceduralTexture as PNG

Hi Everyone,

I am experimenting with ProceduralMaterials and specifically with the method “GetGeneratedTextures()”.
The method returns an Array of ProceduralTextures.

I am searching for a way to save the content of the ProceduralTexture to disk. Preferably as PNG or JPEG.

In the Unity Editor there is a menu option (in the context menu of the ProceduralMaterial component) called “Export Bitmaps”.
Is there a way to call this from within a script file?

My goal is to automate the process of setting certain properties on the material, rebuild the textures and then save them to disk.

Thanks

2 Answers

2

byte data = texture.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + “/…/text.png”, data);

To exucute code above, be sure that your texture is marked as readable

This only works for Texture2D. Texture and ProceduralTexture don't have this method.

Found the answer.

My Unity was stuck in version 4.0 pretending to be up to date.
In Unity 4.1 ProceduralTexture.GetPixels32 was introduced.

So after updating to 4.1, the following code works like a charm.

var tex = new Texture2D(proceduralTexture.width,proceduralTexture.height); tex.SetPixels32(proceduralTexture.GetPixels32()); tex.Apply(false); File.WriteAllBytes(path, tex.EncodeToPNG());

And how do i set proceduralTexture to be readable?

I Tried this... output textures are black at random (i wait until is isProcessing is false) and normalmaps are b/w (not useful too) Any idea?