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
3
Question by Diomich · Jun 27, 2014 at 01:15 PM · spritesprite packer

How to load sprite at runtime from atlas?

Good day to all.

At the moment I am creating the 2d game with quite a lot of art. It is more than 1k sprites there. It should be possible to load any combination of pictures at runtime. I'd already implemented all the logic, but have a trouble that is annoying me.

In the current version all the sprites are inside Resources folder and are loaded using

 Sprite sp = Resources.Load("name", typeof(Sprite)) as Sprite;

It works fine.

There are almost no POT-size images, so without packing to atlases there is a huge memory overhead. iOS build takes about 500 Mb on device. That's why I want to pack my sprites to atlases. But there is a couple of troubles with it.

First of all, after enabling, unity's Sprite packer do not wanna pack my images to atlases (I specified tags for my sprites). There is nothing happens after clicking "Pack"/"Repack". I thought that it is because location of my sprites (Resources folder), but even after moving them to different folder, Sprite packer works strange. Anyway, at the moment I created test project, copied all the pictures there and packed them (still cannot pack them in the original project).

The next trouble, that I cannot find how to load sprites from created atlases at runtime without assigning them to variables in my scripts.

I cannot find enough info in the official reference and on the forums, as this is quite a new feature, but hope that someone here can give me needed answers.

Have a nice day and thanks for reading =)

Comment
Add comment · Show 3
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 Seith · Sep 14, 2014 at 08:48 PM 0
Share

Hi, did you find out how to do this? I'm having the same problem; I don't know how to access the sprites from the atlas at runtime...

avatar image BJO · Sep 15, 2014 at 11:55 PM 0
Share

Bumping this question. I'm in the exact same situation: lots of non-POT art, developing for iOS, would like to use atlases but don't know how to reference them at runtime.

I guess my workaround will be to create a gameobject that has either an array or dictionary of sprites. Then, have an editor script that automatically populates the dictionary with all of the sprites in my project. The serious downside of this is that the sprites will always be loaded when the scene starts, rather than a controlled Resources.Load

avatar image Diomich · Dec 03, 2014 at 05:26 PM 0
Share

Yeah, sorry. Didn't post that I implemented the same logic as BJO said. And yeah, I actually don't like it =\ But don't know any other solution.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by pavlito · Oct 23, 2015 at 11:29 AM

I create a SpriteCollection class

 using UnityEngine;
 using System.Collections.Generic;
 
 public class SpriteCollection : MonoBehaviour {
 
     public List<Sprite> sprites = new List<Sprite>();
     public Sprite GetSprite(int index)
     {
         if (index >= 0 && index < sprites.Count)
             return sprites[index];
 
         return null;
     }
     public Sprite GetSprite(string spriteName)
     {
         foreach(Sprite sprite in sprites)
         {
             if (sprite.name == spriteName)
                 return sprite;
         }
         return null;
     }
 }

attach it to a game object and save it as prefab. Assign desired sprites to each prefab and then Resource.Load(prefab). Hope this helps!

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 ForceMagic · May 21, 2015 at 06:15 AM

This is currently not possible, unfortunately.

You might want to help and vote on this feedback issue "Allow access to sprite packer atlas to retrieve sprites by name"

Other related post/answers

  • Sprite packer does not pack images inside

  • Sprite packer does not pack images inside resources folder

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

25 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

Related Questions

Sprite Atlas not working 0 Answers

Sprite Packer Causes Unity to Hang on 5.5.0f3 0 Answers

sprite power of two and sprite packer 0 Answers

Why is atlas and individual sprites included in package? 0 Answers

Managing your sprites in a project question. 0 Answers


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