Search Unity

Texture2D Constructor - Format not working

Discussion in 'Scripting' started by OverPoweredTeam, Jun 5, 2020.

  1. OverPoweredTeam

    OverPoweredTeam

    Joined:
    May 3, 2017
    Posts:
    12
    Hi!

    I'm creating a Texture Editor. A very simple one.

    upload_2020-6-5_17-40-5.png

    I set the Import Settings to be editable:

    Code (CSharp):
    1.  
    2. importer.wrapMode = TextureWrapMode.Clamp;
    3. importer.textureType = TextureImporterType.Default;
    4. importer.mipmapEnabled = false;
    5. importer.maxTextureSize = textureSize;
    6. importer.isReadable = true;
    But when I try to paint it over, I get the next error.
    Unsupported TextureFormat(%d) for SetPixel operations.

    This is because the Format is set to Automatic:
    upload_2020-6-5_17-42-30.png

    But in the constructor, I'm setting the format:
    Texture2D texture = new Texture2D(textureSize, textureSize, TextureFormat.RGB24, false);

    If I change it manually, everything works fine. I don't know how to solve this, as the format cannot be edited via script.

    If any of you could help, it would be great.

    Thanks!
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
    Those settings are set with the TextureImpoter. When importing an image, the importer will recreate the
    Texture2D
    and the importer's settings will determine the texture format.

    Not sure how your saving/loading your textures but if you're making a round-trip to an image format, then you'll have to configure the image's importer with the settings you need.

    You can however just save a
    Texture2D
    directly using AssetDatabase.CreateAsset. This native asset will not be reimported and retains the format it was created with.