Search Unity

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

Question Unity already support importing WebP but in ugly way. Why?

Discussion in 'Formats & External Tools Previews' started by JesOb, Sep 8, 2023.

  1. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,091
    WebP is superior format comparing to png, tga, jpg and many others

    By default it seems that unity can not import it BUT.

    IT IS ACTUALLY CAN!!! :)

    if you get WebP image from somwhere and just change filename to end with .png unity try to import it because it thinks that it is regular png image and actually will import it successfully :)

    So Unity already can read WebP images just .webp file not registered as image file format and unity dont try to import it.

    Please Unity add that potentially one line of code to get official support for import WebP images without hacks :)
     
    WongKit and mgear like this.
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,961
    Would help to know which Unity version you used, as this unofficial support may not be available in earlier versions. ;)
     
  3. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,091
    2022.3.5
     
  4. sdfsdfsfsdfsdfsdf

    sdfsdfsfsdfsdfsdf

    Joined:
    Jan 27, 2024
    Posts:
    1
    Very interesting, thanks for the idea of renaming the webp file to PNG. it worked for me.
     
  5. juppelzuppel

    juppelzuppel

    Joined:
    Jun 10, 2018
    Posts:
    2
    This has nothing to do with any Unity feature. You can rename all of your .webp files to .png and you have a working png file.
    Download any .webp file to your system and rename it to .png.
     
  6. WongKit

    WongKit

    Joined:
    Apr 27, 2017
    Posts:
    26
    This is very interesting and I can confirm, that webp files could be imported as long as they are named .png. I would really like to have webp as a recognized format. As you said, the support is already there. It is just missing the recognition of the file extension.

    I tried to hack my way with overriding the default importer, but unfortunately, the texture importer also seems to be picky about the filename.

    Code (CSharp):
    1. //Sadly not working
    2. class ImportWebpAsTexturePostprocessor : AssetPostprocessor {
    3.     void OnPreprocessAsset() {
    4.         if (assetPath.EndsWith(".webp")) {
    5.             AssetDatabase.SetImporterOverride<TextureImporter>(assetPath);
    6.         }
    7.     }
    8. }
     
  7. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,057
    WebP and PNG are completely different file formats and not compatible in any way.

    The reason changing file extensions works is because many systems/applications use a library to handle image decoding that supports many different file formats. The library then doesn't actually see the file extension but instead only looks at the data to determine the file type. So, if you change .webp to .png, depending on the system/application you use it with, it just feeds it into the same library, which will then recognize it as WebP and still decode it properly. However, if the system/application would use a specialized library like libpng, then it would actually not recognize it as png and fail.
     
    WongKit, JesOb and Lurking-Ninja like this.