• 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
Question by luniac · Aug 10, 2014 at 07:12 PM · memoryresources.loadfixed array

fixed array vs resources.load for 1000+ sprites MOBILE

I'm making a simple quiz like app with a sprite shown one at a time, and clicking some choices, and then new sprite is swapped in. Very simple app with nothing intensive.

If i create a fixed array in the editor and stick 1000+ sprites in it and enter play mode, and all i do is reference the images, is this feasible? will it overload the phones memory?

It's much easier for me to design and develop my app if i use a fixed array, because i can access it in code nicely. I've never really used resources.load but since it accesses the hard drive theres no fear of overloading memory.

I'm actually not sure about this, if i create a fixed array with 1000 sprites and starting play mode, will unity automatically allocate all the memory for the 1000 sprites possibly overflowing memory, or will it reference the sprites on the harddrive?

Comment

People who like this

0 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 robertbu · Aug 10, 2014 at 07:18 PM 1
Share
  • What is the size of each individual sprite?

  • Are they animated?

  • Do the sprites vary in size?

  • Are they randomly accessed or accessed in order?

Note figuring out if Unity will slow are crash with a 1000 of your sprites is an easy test to run. In a few minutes you could build an app with 1000 sprites (even if they were all the same), and link them in an array. But figure out the best solution for 1000 sprites needs more information.

avatar image luniac · Aug 11, 2014 at 11:11 PM 0
Share

Theyre essentialy web images, no animation,all same size to fit on iphome android screen, so not much bigger than thumbnail size. Randomly accessed as part of the game.

I guess on ipad theyll have to be bigger by a bit, i usually use png format for everything but as long as the images are decent quality its cool.

Im using them as sprites because i figured thats best 2d format, unles a texture2d is better? I dont know.

avatar image luniac · Aug 15, 2014 at 02:09 PM 0
Share

i guess ill try the array route and see if my app crashes...

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by smoggach · Aug 15, 2014 at 02:24 PM

Your app will crash. Do not use arrays of sprites. I've been dealing with this issue over the past few weeks and I can tell you that with sprites you want to get them in and out of memory as fast as possible. Putting them in a resources folder will work until the total memory taken by each asset decompressed in memory exceeds 2gb. I'd recommend StreamingAssets and loading them just before you need them.

Comment

People who like this

0 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 luniac · Aug 15, 2014 at 02:53 PM 0
Share

interesting, i havent heard of StreamingAssets so ill check it out.

I was wondering though, it states in the manual if you call Resources.UnloadUnusedAssets then "The script excecution stack, however, is not examined so an asset referenced only from within the script stack will be unloaded and, if necessary, loaded back in the next time one of its properties or methods is used."

http://docs.unity3d.com/ScriptReference/Resources.UnloadUnusedAssets.html

Doesn't this mean that i can create an array with references to images in my project and calling Resources.UnloadUnusedAssets will make sure none of the images are actually in memory unless i access some index in which case it will be loaded, but then when i change the image i can call the function again to keep clearing the last image?

Actually this does bring up one question. Does dragging objects from the project panel into an array create a copy in memory or just a reference to the file on the hard drive?

avatar image smoggach · Aug 15, 2014 at 03:17 PM 0
Share

If an object in your scene has a reference to a sprite, that sprite will be loaded into memory when the level is loaded. Calling Resources.UnloadUnusedAssets wont work because your array still has a reference to the sprite.

The only way to truly unload something is to remove all references to it. this means removing it from your array.

avatar image luniac · Aug 15, 2014 at 05:00 PM 0
Share

I see what ur saying but the docs make it sound like if object in the scene has a reference to a sprite BUT the sprite is not present anywhere in the scene then unloading assets will remove it from memory although the reference will remain or something.

I found this thread where someone was able to fix his memory error using resources, he was able to properly unload the asset from memory.

http://stackoverflow.com/questions/15820890/loading-a-png-as-a-texture-at-runtime-in-unity3d

avatar image smoggach · Aug 15, 2014 at 05:31 PM 0
Share

That might be the case but I remember reading somewhere that Resources.UnloadUnusedAssets only worked on assets that were loaded with Resources.Load. I could be wrong though.

avatar image luniac · Aug 16, 2014 at 11:50 AM 0
Share

ahhh!!! So what if i use something like this example:

 // Load all textures into an array
 Object[] textures = Resources.LoadAll("Textures", typeof(Texture2D));

If i only load them into an array for reference but none of them actually appear in the scene, then they wont actually be loaded in memory yet?

Or does it actually load them but then afterwards if i call unload everything, then only the references will remain, which doesn't work since at first they are all actually in memory which will make the app crash...

is this a catch 22?

In any case though, I guess i can just have 1 folder with all my images and pretend that's my array, and i can load in 1 image at a time and unload the previous one beforehand. That should work right?

Then i dont need StreamingAssets

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Do I need to manually call Resources.UnloadUnusedAssets? 0 Answers

Can I load textures at runtime with a smaller memory footprint? 1 Answer

Best way to play and delete many audio files 0 Answers

Best method for memory usage. 0 Answers

Where do Application.LoadLevel() and Resources.Load() load from? 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