- Home /
How to save a texture2d into a PNG?
I am using a Perlin noise based map generator for my game and I found a texture that I would like to export is as a PNG ![alt text][1]to make some more refined edits. If you were wondering why I am not using the one you see, it's because I need a very precise image, down to the pixel. Bellow you can find my code and thanks in advance:
using UnityEngine; using System.Collections;
public static class TextureGenerator {
public static Texture2D TextureFromColourMap(Color[] colourMap, int width, int height) {
Texture2D texture = new Texture2D (width, height);
texture.filterMode = FilterMode.Point;
texture.wrapMode = TextureWrapMode.Clamp;
texture.SetPixels (colourMap);
texture.Apply ();
return texture;
}
public static Texture2D TextureFromHeightMap(float[,] heightMap) {
int width = heightMap.GetLength (0);
int height = heightMap.GetLength (1);
Color[] colourMap = new Color[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
colourMap [y * width + x] = Color.Lerp (Color.black, Color.white, heightMap [x, y]);
}
}
return TextureFromColourMap (colourMap, width, height);
}
}
------ Not code----- [1]: /storage/temp/90671-capture.png
Answer by Dave-Carlile · Mar 29, 2017 at 03:22 PM
Texture2D.EncodeToPNG will convert the texture into PNG data and returns an array of bytes. You can then use File.WriteAllBytes to save the byte array to a file.
Answer by naufalazzmy · Nov 14, 2018 at 06:10 AM
beacuse you want to save a PNG format, first, check if your texture has transparancy or not, because texture have many different format , for your case, try this code
//first Make sure you're using RGB24 as your texture format
Texture2D texture = new Texture2D(width, height, TextureFormat.RGB24, false);
//then Save To Disk as PNG
byte[] bytes = texture.EncodeToPNG();
var dirPath = Application.dataPath + "/../SaveImages/";
if(!Directory.Exists(dirPath)) {
Directory.CreateDirectory(dirPath);
}
File.WriteAllBytes(dirPath + "Image" + ".png", bytes);
Answer by FlightOfOne · Jan 13, 2018 at 11:14 PM
This is what I came up with by building upon the answer @Dave-Carlile provided.
public static void SaveTextureAsPNG(Texture2D _texture, string _fullPath)
{
byte[] _bytes =_texture.EncodeToPNG();
System.IO.File.WriteAllBytes(_fullPath, _bytes);
Debug.Log(_bytes.Length/1024 + "Kb was saved as: " + _fullPath);
}
Answer by Norite113 · Dec 30, 2020 at 07:09 AM
use this is working just fine i know im late :D
private void SaveTexture(Texture2D texture)
{
byte[] bytes = texture.EncodeToPNG();
var dirPath = Application.dataPath + "/RenderOutput";
if (!System.IO.Directory.Exists(dirPath))
{
System.IO.Directory.CreateDirectory(dirPath);
}
System.IO.File.WriteAllBytes(dirPath + "/R_" + Random.Range(0, 100000) + ".png", bytes);
Debug.Log(bytes.Length / 1024 + "Kb was saved as: " + dirPath);
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
}
Answer by Sir-G · Sep 12, 2020 at 01:57 AM
Not to necro a post but this code works like a treat but I was wondering if there is a way to increase the size of the PNG that is output say 250x250 and can you make it transparent.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Add collision to raycasted object? 2 Answers
Image not display current slot location while add new item. 0 Answers
Position gui button over 2d sprite 0 Answers