Search Unity

[Solved] 'DefaultTexturePlatform' is not valid with the current texture type 'Default' (Script)

Discussion in 'Scripting' started by Quatum1000, Aug 6, 2021.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    A solution to use script the TextureImporter with a different format than dxt and RGBA32.

    Change the default texture format to BC7 or any different format by script and unity editor does it's job without an error.

    If you have an existing Texture2D changed by setpixel() or data[] and want to save and reload. I can recommended to use the following simple code.

    using System.IO; is required to use File.xxxxxx

    Code (CSharp):
    1. byte[] bytes = mytex2D.EncodeToTGA();
    2. var filepath_relative = "MyNewTexture/" + FName + ".tga";     /// ... path to .../Assets/MyNewTexture/
    3. var path = Application.dataPath + "/" + filepath_relative;    /// ... Application.dataPath includes "...../Assets"
    4. File.WriteAllBytes(path, bytes);
    5.  
    6. /// ... Tell unity to add this file. Otherwise it does not show up and its not accessible
    7. AssetDatabase.ImportAsset("Assets" + filepath_relative);
    8.  
    Change the compression format from dxt and re-import again.

    Code (CSharp):
    1. TextureImporter textureImporter = (TextureImporter)TextureImporter.GetAtPath("Assets" + filepath_relative);
    2. textureImporter.maxTextureSize = 4096;
    3. textureImporter.textureType = TextureImporterType.Default;
    4. textureImporter.textureShape = TextureImporterShape.Texture2D;
    5. textureImporter.textureCompression = TextureImporterCompression.Uncompressed;
    6. textureImporter.crunchedCompression = false;
    7. textureImporter.compressionQuality = 0;
    8. textureImporter.mipmapEnabled = true;
    9. textureImporter.sRGBTexture = true;
    10. textureImporter.isReadable = true;
    11.  
    12. TextureImporterPlatformSettings platform = new TextureImporterPlatformSettings();
    13. platform.name = "Standalone";       // ... important
    14. platform.overridden = true;        // ... important
    15. platform.format = TextureImporterFormat.BC7;
    16. platform.textureCompression = TextureImporterCompression.Compressed;
    17. platform.compressionQuality = 0;
    18. platform.maxTextureSize = 4096;
    19.                  
    20. textureImporter.SetPlatformTextureSettings(platform);
    21.  
    22. textureImporter.SaveAndReimport();
    23. AssetDatabase.Refresh();