Search Unity

GameObject.Instantiate Prefab vs Scene Instance?

Discussion in 'Scripting' started by frosted, Jul 28, 2014.

  1. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Just wondering, is there a performance hit when doing a GameObject.Instantiate on a prefab vs just referencing a game object in the scene?

    Does unity have some sneaky prefab specific optimizations or is it all the same?
     
    cloutiertyler likes this.
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Object Pooling is the way to go, when dealing with GameObjects that need to be used relatively often. Instantiating objects is expensive, so try to avoid using it too often!
     
  3. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Hrm... I'm not entirely sure if object pooling is relevant. You can pool either way no?

    For my use case, I could probably also pre-allocate - but I'm still curious if anyone knows...
    Code (csharp):
    1.  
    2. public class Something : MonoBehaviour{
    3.   public GameObject Prefab;
    4.   public void DoThing(){ Instantiate( Prefab ); }
    5. }
    6.  
    If the object is a prefab, or just a reference to a random thing in scene - does it make any difference in terms of performance?
     
  4. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    See if there is any little breaks / lag after release the Instantiated method. mean as soon is active.!?
    I tested my self, if many instantiated object, like particles, other GameObjects clones staff, there is some screen breaks like lagging.
    The other test i use is my car parts for example , i do not create individual prefabs parts anymore, i create on fly via script adding regidbody, meshcollider on the respective body part etc... performance is little better and no lag or breaks.
    Probably using bool , is better than using Instantiate, the problem not good for other staff, like particles, sounds , rigid body etc..
     
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    My message didn't get through to you.
    You should spawn the object in advance to prevent a large framedrop during instantiation.
    Just deactivate the object until you need it.
    http://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

    Instantiating causes sudden framedrops.
    Pooling the Object Might increase memory usage when the object isn't needed.
    But in alot of cases it's a better solution than instantiating it.
    Cause a stable framerate is high on the priority list!
     
    AwakeMeerkat and canadaleem like this.
  6. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Look, there are plenty of times that you may either need to instance, or it just happens to be really convenient.

    Let's say you have a lot of random generation at scene start and you're looking to clip off a half second from your scene startup time.

    Does instancing from a prefab make any difference in terms of speed from an item that already exists in scene? If nobody knows the answer that's fine, but the question is - when unity makes a copy of a game object, does it copy faster when it's a prefab than when it isn't?
     
    cloutiertyler likes this.
  7. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Like most people say here, the prefabs lots of them in screen results large frame drop, that results lag = (breaking screen, if draw calls ,freeze). You need control the Instantiate process, destroy the prefabs of the screen if no needed.
     
  8. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Here, we have a lot of things in prefabs so people can work on the prefab without needing access to the scene.

    Eg: Our entire HUD is contained in a single prefab that we instantiate at runtime. The main reason we do this is so a GUI artist can work on it without needing to lock access to the scene.

    I would instantiate things on load, or when you can hide it. If you find yourself instantiating every frame, then there is probably some way around it. (Hence why people are talking about object pools.)
     
  9. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I'm going to guess that it's about one extra reference look up to instantiate the prefab. We're talking microseconds or nanoseconds here. Nothing even close to half a second or even a millisecond.
     
  10. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Thanks for the response. I think you're probably right.

    The only thing that makes me wonder if this isn't accurate is that when unity is copying an instance directly, it can't do any optimization - it needs to do a field by field assignment somewhere. When it's working from a prefab its possible that it could have the data in a form thats optimized for duplication. That kind of thing could represent more than nanos.

    I need to just upgrade to pro and get the friggin profiler...
     
  11. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    So I decided to just run a test on this, since I was curious...

    Prefabs are in fact highly optimized for this process. I didn't expect any results from a test this simple, considering this was a raw gameobject without any monobehaviours attached.

    Instance 1,000 empty game object prefabs: 47ms, 19ms, 21ms
    Instance 1,000 empty game object scene instances: 89ms, 58ms, 56ms

    Average time per instance
    prefab: .029 milliseconds per instance
    scene instance: .067 milliseconds per instance

    It's also VERY IMPORTANT to note that prefabs seem to scale linearly:
    prefab x 10,000 avg: .027 milliseconds per instance
    scene instance: 10,000 avg: 1.624 milliseconds per instance

    so when you're instancing 10,000 game objects you're looking at a prefab being somewhere around 60x faster than using a raw scene instance.

    I imagine this gets much, much worse when you start using monobehaviours and S***. This is a pretty raw run and seeing those differences is a big deal indeed.
     
  12. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    It sounds like when doing a regular copy, Unity has to serialize all the fields before doing the copy, while in a prefab the values are already serialized. That's my hypothesis anyway.

    Thanks for doing the test! The difference is actually much bigger than I guessed!
     
  13. 806202971

    806202971

    Joined:
    Aug 26, 2014
    Posts:
    1
    Could you show your test code please?