Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 chalkesonali · Jun 09, 2021 at 08:01 AM · texture2dtexture atlaspacking

Reading texture from texture atlas by name or path

I need to create a texture Atlas dynamically, so I have used Texture2D.PackTextures to pack all textures in a folder named "Images" in my Resources folder.

 Texture2D[] atlasTextures = Resources.LoadAll<Texture2D>("Images");
 
             Texture2D atlas = new Texture2D(4096, 4096);
             Rect[] rects = atlas.PackTextures(atlasTextures, 2, 4096);
 
             string atlasDirPath = Application.persistentDataPath + "/Resources/Images";
             if (!Directory.Exists(atlasDirPath))
             {
                 Directory.CreateDirectory(atlasDirPath);
             }
             FileStream fs = new FileStream(atlasDirPath+"Atlas.png", FileMode.Create);
             BinaryWriter bw = new BinaryWriter(fs);
             bw.Write(atlas.EncodeToPNG());
             bw.Close();
             fs.Close();

So an Atlas Images is perfectly generated and saved in the persistent path. Now, I need to get a particluar texture individually and convert it to sprite and add it to spriteRenderer in my scene. But to select a texture, I only have the name/path of the texture. But Texture2D.PackTextures returns Rect[] array. Now I don't understand how should I pick a particular texture from the atlas with me only having the name of the texture.

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

1 Reply

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

Answer by Namey5 · Jun 11, 2021 at 02:18 AM

In theory, the rect array returned from the packing function should match the order of the input textures - as such, if you were to also cache the source textures (or at least some of their data) you would be able to use that and match the textures up with their positions;

 Dictionary<string, Rect> m_AtlasTextures = new Dictionary<string, Rect>();
 
 ...
 
 Texture2D[] atlasTextures = Resources.LoadAll<Texture2D>("Images");
 
 Texture2D atlas = new Texture2D(4096, 4096);
 Rect[] rects = atlas.PackTextures (atlasTextures, 2, 4096);
 
 for (int i = 0; i < atlasTextures.Length; i++)
 {
     // Cache each texture rect by its corresponding texture name
     m_AtlasTextures[atlasTextures[i].name] = rects[i];
 }
 
 ...
 
 // Then, you can fetch the texture coordinates like so
 public Rect GetAtlasTextureCoordsByName (string a_TextureName)
 {
     return m_AtlasTextures[a_TextureName];
 }

I have not tested this, but I can't imagine the rect array not matching the order of the input textures, otherwise the data would be useless - there would be no way of knowing what element a rect would be pointing to.

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

126 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

Related Questions

Using TexturePacker for Texture2ds 0 Answers

What s does "Texture Atlas needs textures to have a readable flag set mean"? 1 Answer

yield in for loop breaking render texture 1 Answer

Memory Leak with creating a new Textrure2D 2 Answers

How to script GUI Texture Window to see it in Script (Object's component) 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