Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 25, 2016 at 09:30 AM by GameMaker_ for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by GameMaker_ · Jul 22, 2016 at 03:37 PM · errortexture2ddetectionloading file

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 alt text 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.

errorimage.png (3.5 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Jul 24, 2016 at 05:16 PM 0
Share

Couldn't you just check if the value isn't null?

avatar image GameMaker_ Cherno · Jul 24, 2016 at 05:35 PM 0
Share

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.

avatar image Cherno GameMaker_ · Jul 24, 2016 at 09:55 PM 0
Share

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.

Show more comments

3 Replies

  • Sort: 
avatar image
2

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) or 89 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) or ff d8 ff e0 00 10 4a 46 49 46 (hex)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GameMaker_ · Jul 25, 2016 at 09:20 AM 0
Share

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 !

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GameMaker_ · Jul 24, 2016 at 05:02 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges