Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Prefabs : Build time vs Run time implications

Discussion in 'Scripting' started by amigueloferreira, Apr 1, 2015.

  1. amigueloferreira

    amigueloferreira

    Joined:
    Jul 8, 2014
    Posts:
    4
    Hi,

    Let's imagine I have a Prefab that corresponds to a GameObject with many components, children, something really complex.

    I can access and use it via scripting in two ways:
    1 - Through a class instance member variable reference that can be set on the Editor (Early binding)
    2 - Through Resources.Load (Late binding)

    I know that the second option implies a disk read to load the prefab itself, and that prefab is just a file, and doesn't take any ram memory before I call Resources.Load.

    But I am somewhat intrigued with the first option. Where and how is the prefab stored if it is only used through early binding. If my scene has a reference to 20 prefabs, even if it doesn't use them, all those referenced prefabs are essentially loaded in memory when the scene loads right?

    Let me give a concrete example. Imagine I have a game with many levels and each level is a prefab.
    In the Selection Level scene, there are two ways to know what levels are available:
    1 - List<LevelPrefab> that is filled in the editor with the references to the level prefabs (Early binding)
    2 - Iterate through all of the files in the resources folder to know what levels are available

    If a level prefab takes 5mb of ram on average, then, in the first option, it was guaranteed that, when the scene has entered, I would have at least X (number of levels) * 5mb ram usage. Am I thinking correctly here?
     
    Nanako likes this.
  2. amigueloferreira

    amigueloferreira

    Joined:
    Jul 8, 2014
    Posts:
    4
    Ok, I am now sure that the asset references are loaded when the object that references them is loaded.

    This Unite presentation reflects on this problem and presents a solution based on a custom inspector for dealing with Resources paths as if they were asset references:



    @ 30:32