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?
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?
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!
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…
public class Something : MonoBehaviour{
public GameObject Prefab;
public void DoThing(){ Instantiate( Prefab ); }
}
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?
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…
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!
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?
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.
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.)
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.
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…
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 shit. This is a pretty raw run and seeing those differences is a big deal indeed.
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!
Could you show your test code please?