Hi,
I added a texture in my assets folder.
The texture is created with a free raster graphics editor and is exported as an 8-bit grayscale texture.
The files size is 21 KB.
Texture size is 2048 x 2048px
Here is the texture;
Inspector settings of the texture:
I’ve created a script, which gets the texture and saves it.
The problem is that the saved image has a file size of 55 KB.
When I save the same image again in the raster graphics editor as a *.png file format the file size stays the same (21 KB).
Here is the script:
I use SetPixels32() in the code. “m_textureFormat” can only be these Texture Formats:
“You can use SetPixels with the following texture formats:”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Experimental.Rendering;
public class SaveTextureTest : MonoBehaviour
{
public Texture2D textureSource;
public TextureFormat m_textureFormat;
public GraphicsFormat m_graphicsFormat;
void Start()
{
SaveTexture();
}
public void SaveTexture()
{
Texture2D tmpTexture = new Texture2D(textureSource.width, textureSource.height, m_textureFormat, false);
Color32[] pixels = textureSource.GetPixels32();
tmpTexture.SetPixels32(pixels);
// Test 1
byte[] bytes = ImageConversion.EncodeToPNG(tmpTexture);
// Test 2
// https://docs.unity3d.com/ScriptReference/ImageConversion.html
// using UnityEngine.Experimental.Rendering;
//byte[] bytes = ImageConversion.EncodeArrayToPNG(tmpTexture.GetRawTextureData(), m_graphicsFormat, (uint)textureSource.width, (uint)textureSource.height);
// https://docs.unity3d.com/ScriptReference/Application-dataPath.html
// Unity Editor: <path to project folder>/Assets
File.WriteAllBytes(Application.dataPath + "/SavedTexture.png", bytes);
}
}
How can I save the texture with the same file size (or if possible smaller file size)?
The texture will only contain black or white colors. No antialiasing.
Thanks.