Search Unity

How to store many prefabs?

Discussion in 'Scripting' started by leo_dev76, Nov 25, 2019.

  1. leo_dev76

    leo_dev76

    Joined:
    Oct 5, 2018
    Posts:
    32
    Hi everybody,

    I'd like to know how i can store 5 000 prefabs in my map editor script because i need to have all this objects loaded to use at runtime.
    I tried first to put all the prefabs in a GameObject array but it freezes/crashes in editor and use too much memory.
    I tried now to load all prefabs in Resources folder with Resources.LoadAll but i don't now if its good for memory. I'd like to have another variable with all the sprites associated with each prefab but when I add the sprite array, i have a problem with memory.
    So I don't really know how to do this. I think it is good to load only objects i use but for example, with the search tool of the map editor i need to check if all objects corresponds with the search.
    So i'd like to know how to store many prefabs in c# script or if there is another method to load objects and access to it quickly at runtime.
    So I hope you can help me about this,
    Thank you so much!
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    Instead of loading all prefabs simultaneously, you should only load them one at time as needed.

    To support search functionality, you could have a separate database that holds only the minimum necessary amount of of data for this. If you need to display thumbnails for the prefabs, you could have them be added here too.

    You could use AssetPostprocessor / AssetModificationProcessor to detect when new prefabs are added to specific folders or existing prefabs are modified, and then update the database accordingly.

    Something along these lines:

    Code (CSharp):
    1. public class MapEditorPrefabDatabase : ScriptableObject
    2. {
    3.     public PrefabData[] prefabs;
    4. }
    5.  
    6. [Serializable]
    7. public class PrefabData
    8. {
    9.     public string name;
    10.     public string[] assetLabels;
    11.     public Texture thumbnail;
    12. }
    You can use Resources.Load to actually load the prefab when needed. If you run into issues with this approach, due to the file size of the resources asset growing too large for example, you might have to split your prefabs under multiple smaller asset bundles.
     
  3. leo_dev76

    leo_dev76

    Joined:
    Oct 5, 2018
    Posts:
    32
  4. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    Could be that you have too much data in your resources folder and need to split it under multiple asset bundles. The contents of your resources folders can't have so much content that the resulting resources file gets larger than 4GB (at least that was true when I last used them).
     
    Joe-Censored likes this.
  5. leo_dev76

    leo_dev76

    Joined:
    Oct 5, 2018
    Posts:
    32
    Thanks! But I have AssetBundle with a size of 1GB, what is the recommanded max size of an AssetBundle to load all assets in AssetBundle quickly ?

    And AssetBundle files are on my HDD but how to put them in my project for all players, not only on my pc ?
     
  6. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    You can place asset bundles in the streaming assets folder and use AssetBundle.LoadFromFile.

    The maximum AssetBundle size depends a lot on your target platform / minimum system requirements, and how much memory everything else in your application uses. However, if you are using the method I described above, you should not be loading all assets from an AssetBundle in one go.
     
  7. leo_dev76

    leo_dev76

    Joined:
    Oct 5, 2018
    Posts:
    32
    Yes, i have 31 asset bundles associated with each object category and i load only all assets of category (asset bundle) in use.
     
  8. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    Roger. Well, in terms of maximum memory capacity 1GB should be doable on a PC as long as you have at least 8GB RAM and you're not using any compression for your asset bundles.

    However, I'm not sure how long loading such a huge asset bundle would take, it could be a while. You'll have to test it to see how bad it is.

    Also note that when you would switch between asset bundles and unload the contents of the previous asset bundle, your garbage collector would have a lot of work to do, causing huge hiccups in performance.