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 /
  • Help Room /
avatar image
0
Question by Psychokiller1888 · Dec 14, 2017 at 03:04 PM · texturetexture2dresources.load

Resources.Load doesn't work

Hello!

I spent hours trying to achieve what I want, but it doesn't want to work.

I'm trying, to Resources.Load() an image from my Resources directory and display it on an Image then.

This works, prints "Texture loaded":

         var texture = Resources.Load<Texture>("Categories/Matchsticks/1");
         if (texture != null) {
             print("Texture loaded");
         }
         else {
             print("Missing image file");
         }

So yes, the path is correct, the file is present. But... This doesn't work, prints "Missing image file":

         var texture = Resources.Load<Texture2D>("Categories/Matchsticks/1");
         if (texture != null) {
             print("Texture loaded");
         }
         else {
             print("Missing image file");
         }

The only difference is Texture to Texture2D

I need to load that texture in order to create a Sprite (Sprite.Create(Texture2D, Rect)) and display the image, on Unity, Android and IOS, explaining why I use the Resources folder and Resources.Load()

I did try with other methods, they all fail

 var texture = Resources.Load("Categories/Matchsticks/1", typeof(Texture2D));

 var texture = Resources.Load("Categories/Matchsticks/1", typeof(Texture2D)) as Texture2D;
Comment
Add comment · Show 3
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 Hellium · Dec 14, 2017 at 03:41 PM 0
Share

Have you tried the following?

 var texture = Resources.Load("Categories/$$anonymous$$atchsticks/1", typeof(Texture));
 var sprite = Sprite.Create(texture as Texture2D, Rect);
avatar image Psychokiller1888 Hellium · Dec 14, 2017 at 03:49 PM 0
Share

Yes I did, this does load the file, as it Loads a Texture.

But the sprite is not created for some reason

         var texture = Resources.Load(string.Format("Categories/{0}/{1}", category.GetName(), level.GetFilename()), typeof(Texture));
         if (texture != null) {
             print("Texture loaded");
             _guessImageHolder.GetComponent<Image>().sprite = Sprite.Create(texture as Texture2D, new Rect(0, 0, 345, 345), new Vector2(0.5f, 0.5f));
avatar image Psychokiller1888 Hellium · Dec 14, 2017 at 03:54 PM 0
Share

But if I do

         var bytes = File.ReadAllBytes(string.Format("Assets/Resources/Categories/{0}/{1}.png", category.GetName(), level.GetFilename()));
         var texture2D = new Texture2D(2, 2);
         if (texture2D.LoadImage(bytes)) {
             _guessImageHolder.GetComponent<Image>().sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
             _gameScreen.SetActive(true);
         }

It works... In Unity of course, not on Android, the reason why I want to switch to resources.load()

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Psychokiller1888 · Dec 14, 2017 at 07:09 PM

Ok, after more than 7 hours fighting, I found the solution.... And it's pretty dump... We copied these images back and forth, and somehow, the file lost their texture types...

Click on the image and make sure that the import settings texture type is correctly set

alt text

Loading a resource is then as easy as:

 var texture = Resources.Load<Texture2D>("path relative to Resources folder to the image, without extension");
 if (texture != null) {
     Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
 }

I've seen this question many times on here and stackoverflow, never found the answer... This topic put me on the track though: https://stackoverflow.com/questions/38757654/resources-loadsprite-returns-null-only-for-certain-files.

Hope my self answer helps others!

Comment
Add comment · Show 2 · 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 macjgames · Jan 24, 2018 at 02:40 PM 0
Share

You just saved me at least seven hours. Thanks for answering your own question ins$$anonymous$$d of disappearing into the mist Atlas Shrugged style.

avatar image lagomosker · Jul 18, 2019 at 05:07 AM 0
Share

You just saved me more than seven hours...thank you. (And there are far worse ear worms than Talking Heads 77.)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

147 People are following this question.

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

Related Questions

How to import .blend file without loosing texture quality? 0 Answers

Resources.load "object reference not set to an instance of an object unity",Resources.Load doesnt load 2d texture 1 Answer

Change Texture 0 Answers

Memory leak with creating a new Texture2D 0 Answers

TerrainLayer at runtime 2 Answers


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