Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Save texture file with same or smaller file size?

Discussion in 'General Graphics' started by OrangeJuice224, Nov 21, 2023.

  1. OrangeJuice224

    OrangeJuice224

    Joined:
    Jul 5, 2023
    Posts:
    9
    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:"
    https://docs.unity3d.com/ScriptReference/Texture2D.SetPixels32.html

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. using UnityEngine.Experimental.Rendering;
    6.  
    7. public class SaveTextureTest : MonoBehaviour
    8. {
    9.  
    10.     public Texture2D textureSource;
    11.  
    12.     public TextureFormat m_textureFormat;
    13.  
    14.     public GraphicsFormat m_graphicsFormat;
    15.  
    16.     void Start()
    17.     {
    18.         SaveTexture();
    19.     }
    20.  
    21.     public void SaveTexture()
    22.     {
    23.  
    24.         Texture2D tmpTexture = new Texture2D(textureSource.width, textureSource.height, m_textureFormat, false);
    25.  
    26.         Color32[] pixels = textureSource.GetPixels32();
    27.  
    28.         tmpTexture.SetPixels32(pixels);
    29.  
    30.         // Test 1
    31.         byte[] bytes = ImageConversion.EncodeToPNG(tmpTexture);
    32.  
    33.         // Test 2
    34.         // https://docs.unity3d.com/ScriptReference/ImageConversion.html
    35.         // using UnityEngine.Experimental.Rendering;
    36.         //byte[] bytes = ImageConversion.EncodeArrayToPNG(tmpTexture.GetRawTextureData(), m_graphicsFormat, (uint)textureSource.width, (uint)textureSource.height);
    37.  
    38.  
    39.         // https://docs.unity3d.com/ScriptReference/Application-dataPath.html
    40.         // Unity Editor: <path to project folder>/Assets
    41.         File.WriteAllBytes(Application.dataPath + "/SavedTexture.png", bytes);
    42.  
    43.     }
    44.  
    45. }
    46.  

    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.
     
    Last edited: Nov 21, 2023
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,847
    If it's just black or white I suggest using a single-channel texture format, probably R8.
    If it is just 2 colors you could find some way to export it directly to the GIF format, as that might be the most efficient format to locally store images with very limited colors.

    But first, just use any format to get a base working. The optimization should not be the first step for this unless you need to those thousands or millions of images.
    This might help (mostly 2nd answer): android - Unity c#, take screenshot and save to file as jpg - Stack Overflow