Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GameObjects and Performance

Discussion in 'Scripting' started by Corpse0327, Oct 30, 2014.

  1. Corpse0327

    Corpse0327

    Joined:
    Oct 4, 2014
    Posts:
    26
    Hi all.

    I am currently working on a game project. I was thinking about how unity handles gameobjects that are added in a scene from editor which are a clone of a prefab.

    Possible scenerio : At level 1 we have two gameobjects. At level 2 other two gameobjects. At level 3 other other two gameobjects. At level 4 two gameobjects that we used at level 1 just with the difference of rotation and transform differences.

    Now my question is what unity does in this process. I want to be sure unity doesnt loads prefabs each time and instanstiates which were loaded at game start.

    Thanks in advance.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Depends on what's on them.

    Say you have a prefab that simply has a MonoBehaviour, a Collider, maybe a couple of empty children. Those kinds of things, it loads completely fresh. If it takes N milliseconds to load a scene with 1 prefab, it'll probably take N*100 milliseconds to load 100 copies of the same prefab.

    On the other hand, most prefabs aren't like that. Some data is shared. Fortunately, this includes the biggest pieces of data - textures, materials, meshes, and sounds (I'm sure others too, but those come to mind). If those 100 prefabs all use the same mesh and the same texture, then Unity only loads that texture once.

    Hopefully this answers your question.
     
  3. Corpse0327

    Corpse0327

    Joined:
    Oct 4, 2014
    Posts:
    26
    Well thanks it answered my questions.

    But thinking it increases load time, since it will reach hard disk each time. What kind of algorithm can i use.

    It comes to mind i can hold an array of objects that can be reused. But doing it in the meantime with giving level designers more freedom(Editing the levels freely again and again). I can't come up with an algorithm.

    Is there a known algorithm for me to use or should i work on an algorithm.

    I dont know, should i not care?
     
    Last edited: Oct 30, 2014
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    I wouldn't worry about hard drives in a case like this. Worry about them for loading the big stuff, but GameObjects and the like load very quickly from the drive. It also streams in the whole ~100kb scene file at once, so it's not repeatedly reaching the disk. It's not a good reason to generate things procedurally, because instantiation of the objects (whether they're loaded or generated) will take much longer than streaming the file in from the drive.

    I would not care, TBH. Don't optimize until you've confirmed with profiling that something is a problem.
     
  5. Corpse0327

    Corpse0327

    Joined:
    Oct 4, 2014
    Posts:
    26
    Thanks for the help. I will not care for now and see if it causes loading time problems.
     
  6. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    If you're not streaming assets don't worry about load times as any optimisations you do will probably go unnoticed anyway.