• 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 /
avatar image
Question by oliver-jones · Sep 22, 2013 at 11:28 AM · texturematerialrendererfile

Load Material Texture from File

Hello,

I'm trying to load in a texture file (png) from my resources file, into my material.

Basically, during runtime, a screenshot is taken after an event w$$anonymous$$ch is saved in the resource file wit$$anonymous$$n Unity Project. I then want to take t$$anonymous$$s saved image, and apply it to a cube wit$$anonymous$$n my scene (the material).

Right now, I've taken out the 'take a screenshot' function, and I'm just using a pre-saved image wit$$anonymous$$n my resource file:

 uvLocation = "Assets/Resources/UV_1";
 gameObject.renderer.material.mainTexture = Resources.Load(uvLocation) as Texture;

When I run it, it doesn't load in the texture into the material.

What am I doing wrong?

Comment
Xanatos_88

People who like this

1 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by ArkaneX · Sep 22, 2013 at 11:41 AM

Resources.Load requires path relative to Assets\\Resources, so in your case you have to use

 Resources.Load("UV_1")

To load images w$$anonymous$$ch you save at runtime, you must use different method though. You can for example use Texture2D.LoadImage

 var filePath = Application.dataPath + "/Resources/UV_1.png";
 guiText.text = filePath;
 if (System.IO.File.Exists(filePath))
 {
     guiText.text += " EXISTS!!!";
     var bytes = System.IO.File.ReadAllBytes(filePath);
     var tex = new Texture2D(1, 1);
     tex.LoadImage(bytes);
     renderer.material.mainTexture = tex;
 }

Please note that you can only load jpg and png images in t$$anonymous$$s case, and in opposite to Resource.Load, you have to use file extension in path.

Btw - you don't have to put these images in Resources folder.

Comment
Barrett-Fox
KwahuNashoba
Dragate
QwertyHJKL1234

People who like this

4 Show 3 · 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 oliver-jones · Sep 22, 2013 at 11:51 AM 0
Share

Okay, I tried this:

 Application.CaptureScreenshot(Application.dataPath + "/Resources/UV_1.png");
 savedImage = Resources.Load("UV_1") as Texture;
 gameObject.renderer.material.mainTexture = savedImage;

The new image never seems to load in. When I stop the app, and hit refresh, only then is when the new image file loaded in. I want it to load in at runtime (which is what Resource load is suppose to do isnt it?) Thanks

avatar image ArkaneX · Sep 22, 2013 at 12:29 PM 0
Share

Seems I missed the clue of your question. Yes, Resources folder is just for this, but these resources must be imported in Unity Editor.

Answer updated.

avatar image oliver-jones · Sep 22, 2013 at 07:24 PM 0
Share

Perfect, thank you

avatar image

Answer by Paulo_stopEatingAnimals · Sep 10, 2014 at 07:28 PM

do you have a folder named Resources in Assets? sorry for my bad english

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Changing two different objects renderer colour 1 Answer

texture.width not returning actual image pixels 2 Answers

Modifying material modifies the actual .mat file! 0 Answers

How can i change detail texture? 1 Answer

renderer.material.mainTexture not changing texture... 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