Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

How to tell when ImageConversion.LoadImage REALLY succeeds?

Discussion in 'Scripting' started by JoeStrout, Jan 20, 2021.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    According to the docs, ImageConversion.LoadImage should return true on success and false on failure. But it never seems to return false for me. Given some bogus non-image data (like a JSON file or the contents of some .DS_Store file on disk), it returns true and gives me an 8x8 pixel image that looks like a red question mark on a white background:

    upload_2021-1-19_21-29-44.png

    This is... not helpful. Is there any way to tell when LoadImage is throwing its hands up in this way?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    Nobody? I'm still stuck on this. This affects my users, since we support loading images at runtime, and they'd really like to know when that has been done successfully.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    That's annoying.

    Personally I would push past it and just feed it some garbage data on startup, keep that little 8x8 error texture handy, and then compare what you really get back with a supposedly real valid picture.

    Odds are it will be the same actual object, but you could also just compare it for dimensions, then if the dimensions match the bad image, compare its pixels until they miss.

    The primary use case is the dimensions will not match (one or two integer comparisons) so you can probably assume it's a valid image and move on.

    The worst case performance would be your users constantly feed 8x8 pixel images in, at which point you would compare colors until the first one mismatched and then say, "Nope, it's valid."

    The main downside is that nobody could use that red question mark image for some intended purposes.
     
    JoeStrout likes this.