0
\$\begingroup\$

Question point

Does putting prefab asset in inspector's reference and access its content inside in runtime VS. Load the same prefab asset from Resources.Load API, will both cause entire prefab to load and use same memory footprint?

Backgroud

In UNET or it's replacement lib, Mirror, you will have to register list of prefab on NetworkManager to spawn on server and clients.

enter image description here

Under the hood, these prefab references will be sent to registration function ClientScene.RegisterPrefab which in turn access them as far as prefab.GetComponentsInChildren<NetworkIdentity>(); (I suspect that this will make entire prefab loaded?)

VS

I put those prefab in Resource folder, then use API like Resources.Load / LoadAll to send them to ClientScene.RegisterPrefab (which proved to work too) But many fellow dev warned me about how evil Resources API is and using these will load all data into memory, etc.

Didn't this 2 way ultimately need to access inside of prefab and both need to load entire prefab in? Is there clear advantage for memory footprint for approach?


Some prior research

In official document about resource management, there is some explanation about how resource will load but it still does not clear up difference between these 2 appraoches.

2.7. Resource lifecycle

To reduce loading times and manage an application's memory footprint, it's important to understand the resource lifecycle of UnityEngine.Objects. Objects are loaded into/unloaded from memory at specific and defined times. An Object is loaded automatically when:

  • The Instance ID mapped to that Object is dereferenced
  • ...

Foot note/ Some clarification

Why do you need to use Resources.Load?

Take UNET example case here in my question, and notice that it needs to know every prefab that will be use in game, that's including monsters, items, bullet. Sure you can drag and drop these to inspector (like in screenshot) but will become tedious and unorganized very fast on magnitude of 50+ prefabs. So instead I organize them to Resources folder like

Assets/Resources/EnemyPrefab/Stage1/*.*
Assets/Resources/EnemyPrefab/Stage2/*.*
Assets/Resources/ItemPrefab/*.*

To get more organized load pattern. Resources.LoadAll<GameObject>("EnemyPrefab/Stage1") will do as it suggest. And skipping tedious inspector drag and drop work, let alone checking what did I missed to drag and drop.

Can't you just profile it yourself? why asking here?

I could, but if someone already done this work or has knowledge to confirm such behavior, this could be better, thus this question here. And hey, didn't this is how knowledge is shared and this site is for?

\$\endgroup\$
2
  • \$\begingroup\$ That sounds like you have answered your question yourself. I also encouage you to avoid using Resources. Could you explain, why would want to load your assets via Resouces.Load? Maybe Streaming Assets or AssetBundles/Adressables might suit your case? \$\endgroup\$
    – loptrinho
    Mar 21, 2020 at 17:16
  • \$\begingroup\$ @loptrinho updated the question and include explanation at footnote. I admit that usecase is quite specific. "That sounds like you have answered your question yourself." I wasn't certain, so I seek public knowledge/confirmation. \$\endgroup\$
    – Wappenull
    Mar 22, 2020 at 7:32

1 Answer 1

0
\$\begingroup\$

I decided to come back to answer my own question half year later since I started explored more into this asset management thing. Including Unity's new Addressables system.

Now for my own main point of question:

Does referencing prefab in scene VS Resources.Load will use same memory footprint?

Simple answer for now is YES

According to observation by Reddit "Today I learned: Prefab references in inspector are loaded into memory even if not instantiated."

Although reddit thread is very old (2014) I assumed this behavior did not changed and that would support this claim.

  • It is become clear that Unity computes all dependencies in scene that you are loading.
  • All dependencies are loaded by scene loading process. This strategy prioritize performance when using the object but costs the memory.

So referencing "tank.prefab" by drag and drop in scene, or load it later using Resources.Load will result in same memory consumed. Just that Resources.Load will consume memory later, while referencing will consume on scene load.


So at the end of the day, the requirement still depends on how your game will need. (mobile game where memory is very restrcited? etc) Or else, ultimately current trend is to move to new Addressables system for finer control and better content update anyway.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .