Search Unity

public Array of Prefabs and performance

Discussion in 'Editor & General Support' started by lifeisabeach, Apr 26, 2020.

  1. lifeisabeach

    lifeisabeach

    Joined:
    Apr 26, 2020
    Posts:
    47
    Hello!

    I have a public array of prefabs that I am populating via the editor. There are about 100 prefabs in the array. It's one big array where I am storing modules that make up levels. I need to instantiate about 10 of those prefabs per level, but not always in numerical order.

    My question is if, by having all prefabs in the scene, in the array, are all of them consuming resources?

    Because I have to instantiate each of the prefabs when I need to use them, I'm assuming it does not have a significant impact before that. But I'm not sure, maybe it does consume resources by being on the scene ready to be instantiated?

    Would like to know if anyone could shed some light on this.

    I can, if necessary, break this single array into a smaller one for each level. But the way things are now, it is very convenient to have them all collected on a single array.

    Also, I'm using a SharedInstance to access it for now. I used that to put what I needed together fast, but can change. Open to suggestions.

    Some more information, I'm targeting mobile and for some levels instantiate the prefabs during gameplay.

    Thanks!
     
  2. japhib

    japhib

    Joined:
    May 26, 2017
    Posts:
    65
    I don’t believe they use many resources because the array is just a pointer to them, and they’re not actually doing anything until you instantiate them. I could be wrong though. If you’re worried about performance you could remove the references from the array when you know they’re not being used in the current level. That way they can be garbage collected and free up any memory they were taking up.

    Another option is to use the Resources API. That’ll probably be easier in the long run https://docs.unity3d.com/ScriptReference/Resources.Load.html
     
    lifeisabeach likes this.
  3. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,461
    Yes, everything in your scene that has a reference to an asset via GUID (in the editor, assignments like you described are tracked via GUIDs) will be baked into a reference tree (meaning this goes through all reference chains) that will be loaded into memory when the scene is loaded.
    So you won't be using time every frame on CPU and GPU for them, but it will use more memory and take longer to load.

    Also, anything you place in the Resources folder will go into the build because Unity doesn't know what of this you're going to load ad-hoc.
     
    Last edited: Apr 27, 2020
    lifeisabeach and japhib like this.
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,461
    If you want to see what your memory usage looks like in detail, take a snapshot with the Memory Profiler. If e.g. a Texture is loaded you didn't expect to be loaded yet, check the references to it.
     
    lifeisabeach and japhib like this.
  5. lifeisabeach

    lifeisabeach

    Joined:
    Apr 26, 2020
    Posts:
    47
    Thank you for the response @MartinTilo , that makes sense. Thanks for the memory profiler recommendation, I will take a look at it, haven't use it before, looks useful!
     
    MartinTilo likes this.
  6. japhib

    japhib

    Joined:
    May 26, 2017
    Posts:
    65
    @MartinTilo does that mean everything in the Resources folder will be loaded into memory in every scene?
     
    lifeisabeach likes this.
  7. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,461
    No, not loaded into memory but included in the build.
     
    lifeisabeach likes this.