- Home /
Is there a way to replace the question mark in the WWW.LoadImageIntoTexture?
According to the Unity Script Reference WWW.LoadImageIntoTexture page, "if the data is not a valid image, the generated texture will be a small image of a question mark." Is there any way to change the question mark or at least get a return from the error, so that I could replace the texture manually? Thank You.
-Sixakoo
Possibly:
public static bool isErrorImage(Texture tex) {
//The "?" image that Unity returns for an invalid www.texture has these consistent properties:
//(we also reject null.)
return (tex && tex.name == "" && tex.height == 8 && tex.width == 8 && tex.filter$$anonymous$$ode == Filter$$anonymous$$ode.Bilinear && tex.anisoLevel == 1 && tex.wrap$$anonymous$$ode == TextureWrap$$anonymous$$ode.Repeat && tex.mip$$anonymous$$apBias == 0);
}
I suspect, since a PNG can't specify anisoLevel etc anyway, this may reject any every 8x8 image, unless .name alone is useful.
You are correct: http://dummyimage.com/8x8/f00/fff.png is falsely identified as the "?" by this test. And Bilinear,1,Repeat,0 are simply the default values for Texture, so they are redundant here.
However, it's entirely possible (even probable) that a particular project can be guaranteed to never intentionally require a WWW download of an 8x8 image. In that case, this function is sufficient.
Answer by Waz · Jul 12, 2011 at 12:06 PM
No. The most you can do is check the image against a known-failed image (like file://nosuchimage).
Could you show us code that resembles what you are talking about?
He just suggests that you first use the WWW class to try to load any file path where you know it doesn't exist. That means as result you get the "questionmark" texture. You save it for reference in a variable. When you actually loading your images you can compare the result with the reference texture.
Answer by MattMaker · Sep 30, 2011 at 06:42 PM
Warwick Allison's suggestion (of comparing to a "known-failed" image) is probably the best, most future-proof option. Another possibility which is a little simpler, but will fail if unity ever changes the appearance of the default "error" texture, is to convert your texture into an array of bytes using EncodeToPNG(), and compare the array to a predetermined array representing the PNG-encoded version of the default "error" texture.
I have implemented an example of Warwick Allison's suggestion, plus the other technique I suggest, here: http://wiki.unity3d.com/index.php/TextureBogusExtensions
I implemented it as a C# extension method on the Texture class, which may be overkill. But people keep asking this question on the #unity3d IRC chat channel, so I figured I'd put the code out there.
The link does not load... Could you forward us to the valid URL
I've fixed the old wiki link. However, at least on my side, the wiki currently seems to be unreachable. I get a "Bad Gateway" after some time.
Though i found the same script on a random Github repository
Hey, I've tried implementing this solution, but it seems like it is very computationally expensive (my framerate drops like 50%). Also, since my texture is jpg, my app is still rendering dropped frames which result in the red question mark. Do you have any other ideas on how I could get past this issue?
"will fail if unity ever changes the appearance of the default 'error' texture"
But the texture must be in the build, surely? So it'll only fail if one neglects to update the byte array when that texture changes.