In my game I am instantiating a large group of objects at runtime, and I have noticed that the Instantiate method is actually async, the method return almost immediately but the real object than take much more do appear.
For example let’s say I instantiate 1000 object, it finish in 1 sec but then it take like 4-5 second to appear.
Is there a way to know when it has actually finished?
It doesn’t affect me in any way, because the game doesn’t run until the instantiation has actually finished, but I am trying to optimize some parts and I can’t know how long it does really take right now.
I am using Unity 4.6.3, but it’s the same with Unity 5.
I am sure that unless the instantiation of object is finished, the “parser” will not continue further.
Example:
Instantiate(someObject…);
print(“object spawned”); //This will not run unless the above code finished running, so the object is spawned
So if your game will unfreeze after 1 sec, all the objects should be there. If you want better eprformance you can spawn objects in Coroutine so you will not have that big of an freeze spike.
The main thread maybe,but not the current function.
I am preloading everything manually and am trying to benchmark how long it takes to instantiate the prefab at runtime, to see which area of the game optimize to shorten loading times and if I need to optimized also my code( part of the level is procedural) but I don’t know how to measure the actual time, because the instantiate method return soon but then unity do something after the instantiation.
@Wrymnn
Actually it does run! I tested it by generating 1000 prefab, and calculating the time difference between the start of the loop and the end, the loop complete in 1-2 seconds, but if the instantiated mesh is large it may take several seconds to appear.
Instantiate is completed immediately after the function is called, like every single other function, except those specifically labeled “Async”. (Of which there are only a few.) It would be impossible to program if functions weren’t 100% deterministic. All code is run on the main thread, unless you manually use System.Threading, however most of the Unity API can’t be used in threads.
Note, however, that it’s possible that, technically speaking, the GPU may not execute the commands to RENDER the object until later, as its has its own queue separate from what the CPU is doing, if I understand it right. But probably in the big picture you never have to worry about that.
The computer always finishes to execute the task you have given it, so if you spawn those 1000 objects in one “pass” of main engine loop the objects will spawn and you will see them unless told otherwise in the background from other piece of code.
Remember rendering only happens once a frame. So everything you Instantiate won’t actually appear till te render step. Normally not a problem, unless you are doing some very heavy processing in the same frame.
Its entirely possible to write a frame that takes 4-5 seconds. Especially if its a loading frame. Best explanation I can come up with for the OPs symptoms.
As I said before it’s not an issue of seeing it, or lags. It’s about measuring the time it take, in order to see which part of the level I can optimize to shorten loading times.
There is no problem at all, just I don’t know how to measure the time since something happen outside the update or start method and I would like to know if there is any way to measure the time spent in that part, that’s all.
Time.realtimeSinceStartup is what I am using, but right now I am calculating the delta inside the start method, maybe I can try calculating it between the loading frame and the next one, so that it will account for the gpu part too hopefully.
Didn’t know about Profiler.BeginSample,look handy. Cool, gotta try it.
Who cares what you said. It doesn’t match up with what the OP said. Instantiate takes one frame. So if its taking four seconds for instantiate to complete, that means that one frame is taking four seconds. Totally plausible if you are creating 1000+ objects.
I don’t know if this is also an issue with the call completion vs the mesh being rendered, but if you wanna know when something is completed, maybe your object could have a script on it that removes itself on Start but also tells some static variable somewhere that it’s ready to use?