• 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 /
  • Help Room /
avatar image
Question by 0xsven · Oct 29, 2015 at 05:37 PM · performanceresources

Trouble understanding resource loading. Can someone enlighten me?

Hi,

so I am working on this game that has a randomly generated world. It also has different themes for the world. Only one theme is being used at the same time in the game. I put all theme files into the reosurces folder. This is the file structure I am using:

 Assets/
 - Resources/
 - - Themes/
 - - - DesertTheme/
 - - - - Materials/
 - - - - Prefabs/
 - - - - Options/
 - - - WinterTheme/
 - - - - Materials/
 - - - - Prefabs/
 - - - - Options/
 - - - AnotherTheme/
 - - - - ...
 - - - - and so on...


My problem with that is: All files are constantly loaded into memory. I want to only have the currently used theme in memory. If i for testing remove all themes but the one that is active from the resources folder, I will save 60mb on my iPhone. Which is a lot.

This is how I am loading the theme stuff:

 // Load all prefabs of the desert theme
 Resources.LoadAll("Themes/Desert/Prefabs");

 // Load specific material
 Resources.Load("Themes/Desert/Materials/sand");

I tried Resources.UnloadUnusedAssets() but there was no effect.

How do I load only the themes I am actually using into memory? What might have caused those files to load?

Comment

People who like this

0 Show 0
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

Answer by Caruzo · Oct 29, 2015 at 08:43 PM

I can see a difference between your first and second script. In the second script you Load /Themes/Desert??? Since there is no Desert folder (it should be DesertTheme) it might do buggy things. Try to follow the pathing correctly! I am almost sure this is not the solution but might be something that can help your issue :)

Comment

People who like this

0 Show 1 · 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 0xsven · Oct 29, 2015 at 11:37 PM 0
Share

That's all pseudo code! I will correct it

avatar image

Answer by Statement · Oct 29, 2015 at 09:33 PM

Did you try this on device? Can you confirm the objects are loaded in memory? What is your method to measure that? The editor can load keep resources loaded sometimes, if you were looking at it there.

If Resources.UnloadUnusedAssets have no effect it means you have a reference to it somewhere

Make sure that there isn't any reference to the objects left lingering anywhere as described below. If you add them to a list etc, clear that list before calling UnloadUnusedAssets. Did you register an event handler on one of the objects? Clear that too. And so on.

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

An asset is deemed to be unused if it isn't reached after walking the whole game object hierarchy, including script components. Static variables are also examined.

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. This requires extra care for assets which have been modified in memory.

http://docs.unity3d.com/Manual/LoadingResourcesatRuntime.html

If you want to destroy scene objects loaded using Resources.Load() prior to loading another level, call Object.Destroy() on them. To release assets, use Resources.UnloadUnusedAssets().

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 0xsven · Oct 30, 2015 at 12:46 PM 0
Share

The method of measuring is comparing these two scenarios: a) Deploy the app with 15 themes to my iPhone. Play with theme x for one minute and check XCode's profiler for memory usage: 200 MB b) Remove all themes but theme x and deploy to iPhone. Play theme x for one minute and check XCode's profiler for memory usage: 140 MB (even CPU usage is much lower)

In both scenarios theme x is loaded using Resources.Load(path-to-theme). I do that several times for several subfolders of the theme like described in the post. Never are the other themes loaded in my code! So they don't appear in the GameObject hierarchy.

My best guess is that I use some function that loads everything without me knowing it. Or that I configured the app to do so without knowing.

avatar image Statement · Oct 30, 2015 at 01:26 PM 0
Share

@0xsven: I don't want to jump to any speculations yet (I have a vague guessplination I'd rather avoid going into before eliminating straight forward, testable ideas).

Let's for simplicity sake say that you load 3 textures, called A, B and C.

 void LoadTheme(string theme)
 {        
     a = Resources.Load<Texture2D>(theme + "/A");
     b = Resources.Load<Texture2D>(theme + "/B");
     c = Resources.Load<Texture2D>(theme + "/C");
     StartCoroutine(UnloadUnusedAssets());
 }
 
 IEnumerator UnloadUnusedAssets()
 {
     yield return Resources.UnloadUnusedAssets();
     PrintLoadedTextures();
 }
 
 void PrintLoadedTextures()
 {
     StringBuilder sb = new StringBuilder();
     sb.AppendLine("Currently loaded textures");
     foreach (var t in FindObjectsOfType<Texture2D>())
     {
         sb.Append("  "); sb.AppendLine(t.name);
     }
     print(sb);
 }

Do you still see the previous textures as loaded?

Example:

 LoadTheme("Foo");
 // You should see A, B and C among the loaded textures
 
 // ...... some time passes ....
 
 LoadTheme("Bar");
 // You should see A, B and C among the loaded textures, but not twice!
 // If your see A, B, C, A, B, C then you still have a reference to the textures somewhere! (One set of textures is from the theme Foo, the other from Bar).

If you notice that your textures are still there, use the memory profiler to figure out which object is holding on to it.

Here's an example of finding out why "DeselectOnClick" is kept loaded. Open profiler, go to memory row, select detailed mode, take a sample, find & select DeselectOnClick under Assets/MonoScript and finally get a list of objects holding on to it.

However if your textures are completely gone and you still use memory, it could be something else. Try to use the memory profiler to figure out what you have loaded.

avatar image 0xsven Statement · Oct 30, 2015 at 03:16 PM 0
Share

The textures are completely gone when loading a new theme!

avatar image Statement 0xsven · Oct 31, 2015 at 02:18 AM 0
Share

And still using memory? Well that sucks. I don't know for certain why Instruments (I guess that's what you're using to profile outside of Unity) says it uses the memory you've given back. Perhaps Unity or iOS is holding on to file(s) in a cache that can be evicted if you have to dump memory, but it's a guessplanation. Another guessplanation would be that Unity keeps sharedresources.asset in memory, but again that sounds dumb if it stays there forever and you have no direct control over that. If it really bothers you, I guess you could try loading and unloading them in as asset bundles. Please do make emphasis on "try", as I really wouldn't think it should solve anything. It's more a method of exhausting what you can do on your end.

If you can't get a hang of it and you produce out of memory crashes, I'd recommend to make a reproducible project that only load/unload assets in a way that clearly and simply demonstrates the issue without dependencies on anything else. Submit a bug report/share download link here and possibly contact support to see if they have any suggestions on how you can work around it, or where you may be going wrong.

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

35 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

Related Questions

Are there guidelines for particles on mobile? 0 Answers

Gfx.WaitForPresent performance issue 2 Answers

using namespace; vs namespace.some_method_or_class_name 1 Answer

Object is out of the sceene but fps is low 0 Answers

A better way to do it 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