There is a prefab in the project, there is at least one link to it in the scene, conditionally:

... public GameObject prefab; ... 

Question : in what memory is this object stored?

  • Do you have any questions? - RiotBr3aker

2 answers 2

In order not to guess, let's make a benchmark, since Unity has an excellent profiler.

What the profiler shows on an absolutely empty stage:

enter image description here

Let's create some random prefab and assign some scripts to it. Add an empty object with a simple script that references this prefab. Open debugger:

enter image description here

In fact, almost all of these figures are unstable, but we can be calm and judge by the parameters of the number of meshes and materials: when the prefab reference with 1 material and several meshes (the first model from Google) increased, the values:

  1. Meshes : 73 -> 77 ( +4 meshes )
  2. Materials : 50 -> 51 ( +1 material )
  3. Unity shared memory : 329.0MB -> 333.8MB ( + 4.8MB ), not the most reliable figure, but still an indicator

Answer

Based on the data above, we can draw the following conclusion:

  1. If the prefab is instantiated through a link - Unity loads during scene loading

     Instantiate(prefab_reference); 
  2. If the prefab is instantiated through the resource folder, Unity will load it before instantiating , and recursive

     Instantiate(Resources.Load("Prefabs/PrefabNameHere")); 
  3. If prefab number 1 is loaded from disk and in this case refers to prefab number 2 - prefab number 2 will be loaded when loading prefab number 1

And in all 3 situations, prefabs will be loaded recursively, until all, obviously, unique objects to which there are links are in memory.


Important!

The prefab will be loaded completely, not piece by piece , all the components at once, and not just a bare Transform with the subsequent loading of the remaining components. Theoretically, the Total Objects in Scene graph, which has grown from 205 to 218, theoretically speaks of this.

  • Very helpful =) - Stranger in the Q
  • @StrangerintheQ volumetric fog - RiotBr3aker
  • No, I didn’t write =) I’ve done it 2 years ago and it’s idle

A prefab is one of the types of resources intended for reuse and stored in the Project View. A prefab can be inserted into any number of scenes and repeatedly into one scene. When a prefab is added to a scene, an instance is created. All copies are links to the original prefab and in fact its clones. Regardless of how many copies there are in a project, when you change a prefab, all its instances change accordingly.

Documentation

  • so I did not understand, will it be loaded from the disk or will the scene be fully added to the RAM when the scene starts? - k0vpack
  • @kovpack I don’t know exactly when it will fully take place in memory, when a scene is loaded or when it first enters the frustum (this can be implemented differently in the engine), but it definitely needs to be remembered to be rendered and calculated - Stranger in the Q
  • it will be, and whether its components will be with it, most of it is occupied by its mesh. - k0vpack 6:38 pm