- Home /
The question is answered, right answer was accepted
How to know if the Texture2D.LoadImage succeed in loading
Hello,
The user can select an image (texPath) on the PC and then the image is loaded in the script:
if (File.Exists(texPath))
{
Texture2D tex = new Texture2D(2, 2);
if (tex.LoadImage(File.ReadAllBytes(texPath)))
GetComponent<Renderer>().material.mainTexture = tex;
else
print("Texture cannot be loaded");
}
else
print("Tex path not found at " + texPath);
If the user selects an image, it works well. Otherwise, I would like it displays "Texture cannot be loaded" if the selected file is not an image. This script seems not to works since, when the user load a file which is not a jpeg or png, the renderer texture becomes and no error message is displayed. So... How can I detect if the selected file is not an image (jpeg or png), so that I can display the error ?
Thanks in advance.
Which value ? If it is the tex, I cannot since the tex exists in all cases: if it works, it is the loaded picture, otherwise it is the "?" picture.
Yes, tex. Íf you move it into the main body of the script you don't have to assign a value to it when declaring it.
Answer by saschandroid · Jul 25, 2016 at 06:57 AM
You could check the header of the files to see if they are pngs or jpgs (by reading the first few bytes) before loading them into a texture.
First 8 bytes of a png-file:
137 80 78 71 13 10 26 10
(decimal) or89 50 4e 24 0d 0a 1a 0a
(hex).First 10 bytes of a jpg-file:
255 216 255 224 0 16 74 70 73 70
(decimal) orff d8 ff e0 00 10 4a 46 49 46
(hex)
Ok, it works !
Now I have:
if (File.Exists(texPath))
{
byte[] data = File.ReadAllBytes(texPath);
if ((data[0] == 137 && data[1] == 80 && data[2] == 78 && data[3] == 71 && data[4] == 13 && data[5] == 10 && data[6] == 26 && data[7] == 10) ||
(data[0] == 255 && data[1] == 216 && data[2] == 255 && data[3] == 224 && data[4] == 0 && data[5] == 16 && data[6] == 74 && data[7] == 70 && data[8] == 73 && data[9] == 70))
{
Texture2D tex = new Texture2D(2, 2);
tex.LoadImage(File.ReadAllBytes(texPath));
GetComponent<Renderer>().material.mainTexture = tex;
}else
print("Texture cannot be loaded");
}else
print("Tex path not found at " + texPath);
Thanks !
Answer by Arshia001 · Jul 23, 2016 at 05:05 PM
Since it's such a small texture, What I did was to check whether the texture was more that 8 pixels wide or high. Anything smaller than 8x8 was an error. This may or may not be a plausible solution in your case.
Good idea even but it is not "universal". I can also test some pixel colors to know if it is the "?" picture.
Since it is not perfect, I am waiting for other suggestions. I will make that if there is no other solution.
Thanks.
Answer by GameMaker_ · Jul 24, 2016 at 05:42 PM
I know nothing about mipmap levels but I made some tests and there may be a solution. I displayed the mipmap count in both cases: when it is a png or jpeg picture, the mipmapCount returns a high value (between 10 and 12 in my tests), otherwise it returns 1.
I would like to know if an image that could be selected by the user can have only one level, so that I can test if the texture is well loaded according to the mipmapCount.
Thanks in advance.
Follow this Question
Related Questions
Creating Sprites in code for use Dynamically. 1 Answer
How can I detect error messages in-game? 1 Answer
Display webcamtexture via sprite.texture, error at GetPixels(). Help :) 1 Answer
Have there been changes to PackTextures in 5.6 ? Seeing Artifacts in Scene which disappear in 5.5 0 Answers
Collision error 1 Answer