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 /
avatar image
0
Question by Oukalakakou · Sep 11, 2017 at 09:13 AM · texture2dserializationloadproceduralresources

How to properly load textures?

Hello everyone I have this particular problem where I don't know how to load and reference a texture in a prototype I'm making.

I want to be able to procedurally generate level, depending on a texture. This texture is located on a folder where there is other informations about the level (name, size, etc.), stored in JSON text files. Loading those files is easy using deserialization. There could be hundred of different levels, so hundred of folders containing different textures.

How can I load and reference a particular texture given the correct folder among the others?

Is using many "Resources" folders the only solution?

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
0
Best Answer

Answer by Dave-Carlile · Sep 12, 2017 at 08:49 PM

You can load image data using Texture2D.LoadImage.

You can get the image data into a byte array using File.ReadAllBytes.

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
avatar image
1

Answer by guido-paglie · Sep 11, 2017 at 07:53 PM

Hi !

Yes, it seems it's the only solution. I think the names should be something

Level-1

Level-2

....

Level-11

Level-36

.....

Level-101

Then you can:

 Resources.Load("LevelTextures/Level-" + level) as Texture;
         
         

And also you can have in another folder the jsons, and so almost the same to get them.

 Resources.Load("LevelJsons/Level-" + level) as TextAsset;
         

Hope it helps, cheers !

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 Oukalakakou · Sep 12, 2017 at 08:46 AM 0
Share

Thanks guido-paglie for your answer ! As I was saying, getting the JSONs files and referencing them doesn't imply Resources folder.

 var serializedData = File.ReadAllText(path);
 var storedData = JsonUtility.FromJson<T>(serializedData);

I need the same kind of logic to read an image into a texture given a path.

I found something that is Texture.LoadImage but the documentation seems missing and I didn't succeed to make it work.

Any thoughts?

avatar image guido-paglie Oukalakakou · Sep 12, 2017 at 08:40 PM 0
Share

Sorry, but i dont understand what you need.

  • If you need to read the pixels of the texture ? If you do you need to do a render texture, and then read that pixels.

  • If you just need to get the sprite (2D) from resources you can:

    public Image img;

    void Start () { img.sprite = Resources.Load< Sprite >("LevelTextures/Level-" + level ); }

  • If you are using a 3d model, and you want to put the image you are reading, onto that model, you have to do almost the same but assign it to the mainTexture of the material. And this texture is going to be a texture (haha) not a sprite, because is 3d, not 2d.

    public $$anonymous$$eshRenderer renderer;

    void Start () { renderer.material.mainTexture = Resources.Load< Texture >("LevelTextures/Level-" + level); }

Hope it helps!

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

81 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

Related Questions

How to set a texture resource to texture type "GUI" by script? 1 Answer

Images added to the Resource folder should reflect in thee build? 0 Answers

Unity3D error: Assets/Scripts/Item.cs(25,38): error CS0308: The non-generic method `UnityEngine.Resources.Load(string)' cannot be used with the type arguments 1 Answer

Coding help: How to use xml serialization 1 Answer

Loading textures into an array after build 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