- Home /
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
Answer by themipper · Mar 17, 2013 at 01:52 AM
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());
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?
Answer by XienDev · Mar 16, 2013 at 07:46 AM
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.