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
2
Question by peterworth · May 19, 2014 at 04:54 PM · 2dtexturespriteatlas

loading sprite from atlas in script

there are questions already on this, but as far as i can see none of them have an answer that works..

how can a sprite packed in an atlas (packed by giving a packing tag to the sprites) be loaded in code?

we can't use Resources.Load, because the sprites can't be in a resources folder (they won't pack into an atlas if they're in a resources folder).

we can't use AssetDatabase, because it only works when running in the editor.

the idea is to avoid prefabs, and load the sprite directly, because i need to do a lot of stuff dynamically, and also don't want to have to create prefabs for hundreds of sprites.

any way to do this?

Comment
Add comment · Show 4
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 Loius · May 19, 2014 at 06:19 PM 1
Share

Can't be done. Unless something has changed drastically, you have to have a reference to your sprites somewhere set up from in the editor.

avatar image peterworth · May 20, 2014 at 07:28 AM 0
Share

Ok thanks. Seems surprising! $$anonymous$$aybe i should somehow use the editor to add a single reference to each sprite in my singleton main scene, then i can use those references throughout the rest of the game? Or can it just be one of the sprites from the atlas, which can somehow give me access to the rest of the atlas?

avatar image peterworth · Jan 19, 2015 at 04:01 PM 0
Share

just to bump this up - is there really no way to load a sprite from an atlas in a script?

does anyone know of a way to do it manually, e.g. getting all the uv rect data and manually cutting the sprites out.. or does that defeat the point of using atlases at all?

avatar image peterworth · Jan 22, 2015 at 02:23 PM 0
Share

for the moment i've written an editor script to go through every sprite in the asset database, and create and save a prefab for it, so i've got a few thousand prefabs. then in the game i can load the prefab, take the sprite out of it, and then use that the way i need to.

it's an incredibly clunky workflow - every time i add new sprites to the game i have to run a few different editor scripts, wait for importing to happen, etc etc. surely i'm missing something obvious...

4 Replies

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

Answer by DanSuperGP · Jan 23, 2015 at 09:44 PM

Rather than making prefabs, you can make a game object with a public list of sprites... and then drag all your sprites into that list...

Then, on start... put all the sprites into a public dictionary by name and clear the list...

Then you can load them by name from the dictionary.

Comment
Add comment · Show 6 · 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 peterworth · Jan 23, 2015 at 10:37 PM 0
Share

that's a good idea, i think this will be easier than running my auto prefab maker script all the time...

avatar image peterworth · Jan 23, 2015 at 10:40 PM 0
Share

...but i still think there should be a way of just loading a sprite that's packed into an atlas :)

avatar image DanSuperGP · Jan 23, 2015 at 11:10 PM 0
Share

Yeah, I agree, there definitely SHOULD be a way to load a sprite from the atlas by name.

For that matter, there should be a way to tell Unity to pack your atlas into an asset bundle...

Unfortunately neither are true.

avatar image peterworth · Feb 03, 2015 at 06:49 PM 0
Share

quick update: i'm now thinking this isn't possible, because as long as you have the array/dictionary of sprites around, then that spritesheet will be in memory, so there's no way to unload it when you no longer need it (if you remove the references to sprites in the atlas, in order to get it to unload, then you've got no way to get them back again later). so it has to be prefabs it seems

avatar image DanSuperGP · Feb 03, 2015 at 06:51 PM 0
Share

$$anonymous$$ake a prefab of the game object with the references to all the sprites. Load it when you need it, destroy it when you don't.

Show more comments
avatar image
3

Answer by Bentley · Jan 22, 2015 at 09:32 PM

Maybe you're looking for something like this?

 public Dictionary<string, Sprite> dictSprites = new Dictionary<string, Sprite>();
  
      void Start()
      {
           Sprite[] sprites = Resources.LoadAll<Sprite>("SpriteSheet");
          
           foreach (Sprite sprite in sprites)
           {
                dictSprites.Add(sprite.name, sprite)
           }
      }
      GetComponent<SpriteRenderer>().sprite = dictSprites[name];
 

You could obviously also leave it in just an array and access through indices, although if there are 10+ sprites per sheet counting an index can get irritating.

Hope this helps,

Bentley

Comment
Add comment · Show 5 · 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 peterworth · Jan 23, 2015 at 08:48 AM 0
Share

looks potentially like the answer... i'm not sure what "SpriteSheet" refers to though - it has to be a folder in Resources doesn't it? where does the actual atlas/spritesheet go after sprite packer does its thing?

avatar image Bentley · Jan 23, 2015 at 09:24 PM 0
Share

When you import a file with sprites in it and then select "multiple" and slice up your sprites, then when you look at that file in the project files, it will have child files. "SpriteSheet" refers to the path from the resources folder to that parent file.

alt text

In this case "SpriteSheet" would be "Weapons/Weapons". Then the array will be populated with "Weapons_0" through "Weapons_11". While you're slicing your sprites you can, however change their names to a specific weapon name ins$$anonymous$$d of "Weapon_X" and so that if you decide to put them in a dictionary you can just say dictSprites["A$$anonymous$$47"] ins$$anonymous$$d of sprites[6]. Again just a matter of preference.

unityscreenshot.png (19.9 kB)
avatar image peterworth · Jan 23, 2015 at 10:40 PM 0
Share

ah i see, i think this is a different workflow - i've got each sprite as an individual image file, and then letting unity pack them into an atlas using the sprite packer.

the individual image files are outside of resources folder, because otherwise the sprite packer won't pack them

avatar image elopez7 · Feb 28, 2016 at 05:57 AM 0
Share

You are God to me! At least for this week! This snippet solved a problem I was having! Now I can get an inventory system going!

avatar image Mosredna · Aug 30, 2017 at 05:34 PM 0
Share

This snippet works great ! Just what I needed. Thanks !

avatar image
0

Answer by SuperUltraHyper · Sep 24, 2015 at 07:13 AM

Without some convoluted method it isn't possible but you can up vote it!! http://feedback.unity3d.com/suggestions/allow-access-to-sprite-packer-atlas-to-retrieve-sprites-by-name

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
0

Answer by anagh · Jun 30, 2017 at 12:33 PM

I wrote this code I have 42 png avatars But when i run new Game Object gets initialized 42 times Please help!

public GameObject gridGameObject; public Dictionary dictSprites; void Awake() { dictSprites = new Dictionary (); } // Use this for initialization void Start () { Sprite[] sprites = Resources.LoadAll("Avatars");

     foreach (Sprite sprite in sprites)
     {
         dictSprites.Add(sprite.name, sprite);
         NGUITools.AddChild(gridGameObject, sprite);
         
     }
 }

screenshot-60.png (439.7 kB)
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

27 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

Related Questions

How can I use atlas sprite as texture for a material? 2 Answers

Sprite.Create is not working 1 Answer

Unity3d 4.3 get sprite from atlas 4 Answers

Overlay texture on sprite 0 Answers

Replace a sprite by another in every game object using it 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