Hello,
How do I instantiate 1k Gameobject without freezing the application on load?
Thanks
#newbiehere
Hello,
How do I instantiate 1k Gameobject without freezing the application on load?
Thanks
#newbiehere
You could do it in a coroutine. Even if you need to hold off on starting the game, that would keep the app from freeze and let you do something like drive a little loading wheel or update a progress bar.
Currently, I’ve implemented IEnumerator function calling it using StartCaroutine and Progress status in a form of display text (percentage) but then my application still freeze and the Winform indicate not responding.
Do you yield return in the coroutine?
Coroutines are not parallel, you have to use return to free up the next frame. Use the value 100 to determine how often the script returns, if it still lags then you can reduce this.
IEnumerator LoadObjects()
{
int ObjectCount = 1000;
for (int i = 0; i < ObjectCount; i++)
{
InstantiateObbject();
if (i % 100 == 0)
yield return null;
}
}
Thanks guys . I tried to limit it by instantiating on 3 object at a time something like this
IEnumerator LoadObjects()
{
int ObjectCount = 1000;
int j = 0;
for (int i = 0; i < ObjectCount; i++)
{
InstantiateObbject();
j++;
if (j == 3)
{
yield return new WaitForSeconds(0.1f);
j = 0;
}
}
}
looks like a charm but is this still acceptable on standard coding approach?
You can also increase the value, 3 objects isn’t that much. That script would take 333Frames which is over 5 seconds assuming that it runs at 60FPS If your instantiation process is much faster than you waste time. Adjust (j == 3) so that the game doesn’t lack. Otherwise this is an acceptable solution in my view.
Edit: Use return null here. All you do is add unecessary time of waiting. In this cae you add 33 Seconds! 333 calls of 0.1 Second.
//Always use this, it will simply wait for the next frame.
yield return null;
//Only use that if you have to wait for a specific time
yield return new WaitForSeconds(0.1f)
One question to consider is - do you really need all of these objects? People often create unnecessary GameObjects, when an object could easily be represented as pure data, possibly being rendered with something like a particle system - if you can get away with this, you can dramatically improve your performance in terms of both speed and memory. In my experience, if you have 1000 of something, it’s extremely likely that whatever you’re doing doesn’t need GameObjects. If you can give us a little more information about what those 1000 objects are, we might be able to give you some advice on how you might be able to accomplish this.
Another solution that will be available probably within the next 6 months or so will be the Entity system. This was demo’ed at Unite this year and is designed to give great performance in scenes where you need to instantiate thousands of objects. (This doesn’t help you now, but something to keep an eye on in the future.)
Adding to @StarManta 's post above, I noted that beyond a certain number of GameObjects instantiated in a given frame, Unity really begins to slow down a lot. The actual number seems to vary but I speculate that it is due to new objects being placed into some less-than-optimal data structure prior to actually being added into the main scene graph.
When I noticed it, it was around 500 or so objects, and then subsequent object additions took a LOT of additional time, so I did the coroutine approach and yielded before getting anywhere near that number.
I would further speculate that this behavior may vary in the future, and indeed may even vary based on your version of Unity now, and perhaps even which target platform you are building for, but it is definitely “a thing.”
@StarManta Basically all Gameobject I instantiated are all 2D Objects. I have a search functionality in my application but then when I tried to instantiate it runtime it causes a freeze on the app. what I did was destroy the all object and then instantiate a new object. That’s why I load all game objects do some simple Object pooling so that I can prevent the freeze of the app.
If you have something best approach on particular solution I’m willing to try/apply it.
Thank You.
You can also throw in a stopwatch to Instantiate for a set frame length, rather then to Instantiate a set number of GameObjects per frame.
But really consider @StarManta 's suggestion. Chances are you don’t actually need 1000 game objects.
Based on this sentence it definitely sounds like the objects in question here would be better served by a data model, but there’s still not enough information here to be able to give you any solid advice. What are the “2D objects” you’re searching? How are you searching them (like, is the payer typing something into a search bar, and then it tries to find a GameObject of that name, for example)? How does instantiating objects make your search easier?