- Home /
Any way to validate WWW.texture
When using WWW.texture, if the WWW request was unable to get parse the response data into a Texture2D then it returns a "dummy link"
The data must be an image in JPG or PNG format. If the data is not a valid image, the generated texture will be a small image of a question mark
Does anyone know of way to validate that what is returned is NOT that dummy question mark?
I'm not sure what form the data is in, but if you can get it in an object where you can check the .width / or .height like in Texture base class that would work.
The question mark image is 8x8, I could check against it, but that imposes a limitiation on the web service that it isnt allowed to host images that are 8x8 lest they be filtered by the application
Answer by Danmietz · Feb 01, 2013 at 08:15 AM
A similar question has been asked previously; Warwick Allison gave the following answer;
...The most you can do is check theimage against a known-failed image(like file://nosuchimage).
Answer by Bunny83 · Feb 06, 2013 at 12:01 AM
How about using Texture2D.LoadImage? It returns a boolean. The documentation doesn't say anything about it, but i guess it will tell you success / failure of the function.
Interesting, Texture2D forces you to specify height and width at instantiation, but the documentation says LoadImage can override it. This could work much better. I'll take a look.
Nope, has the exact same behaviour. Returns true with the 8x8 image when given crummy data.
Hmm, well if it does you can use it to produce the bogus texture more efficiently than loading a WWW page ;). That means you can use LoadImage with bogus data in this extension. That would completely remove the need for a coroutine.
Answer by standardcombo · Feb 04, 2015 at 07:01 PM
I was looking for a solution to this and have found that checking the size does the trick.
Answer by Zocker1996 · Feb 09, 2015 at 01:59 PM
You can start this in a Coroutine:
private IEnumerator LoadImages (string path, UnityAction<Texture2D> textureCallback)
{
WWW www = new WWW (path);
yield return www;
if (www.error != null) {
//there was an error
textureCallback.Invoke(null);
} else if (www.texture.width == 8 && www.texture.height == 8) {
//there seems to be an unsupportet file or the user really passed a 8 x 8 image.
textureCallback.Invoke(null);
} else {
//all fine
textureCallback.Invoke(www.texture);
}
}
Answer by HeyNau · May 05, 2019 at 10:30 AM
I've just checked that those error images have 8x8 size. So if you work with higher res images you can check them with something like this:
bool CheckIfImageExists(Texture imageToCheck) {
return imageToCheck.width > 10 && imageToCheck.height > 10;
}
Your answer