Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
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 which is saved in the resource file within Unity Project. I then want to take this saved image, and apply it to a cube within my scene (the material).

Right now, I've taken out the 'take a screenshot' function, and I'm just using a pre-saved image within 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
Add comment
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
4
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 which 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 this 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
Add comment · 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
0

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
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

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

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