How can I Instantiate a GameObject async ?

Hello everyone.

The first time I Instantiate a GameObject. It always stop for a while.

I can accept that the GameObject can show after a while.

But I can not accept that the Game stops for a while which is really uncomfortable.

could anyone help me?:smile:

Explain yourself in more detail

GameObjects can’t be instantiated asynchronously. You’ve got two options:

  1. Look into why your particular GameObject takes so long to instantiate, and optimize it. For example, if you’re doing lots of work in Awake() functions, consider moving the work to coroutines so you can spread it out over a few frames.

  2. Create a bunch of your object during game startup and ‘recycle’ them (an approach known as ‘object pooling’).

3 Likes

Ok

Sorry my poor English. I hope you can know what I want.

The first time I Instantiate a GameObject. Unity needs load some resources , so it stops for a while for it.

I just want to know is there an AsyncOperation to Instantiate a prefab.

"AssetBundle.LoadAsync "can solve this?

Thank you for your advice.

I am interested in your second solution.

do you mean I needs split my prefab then instantiate it one by one?

Another things you can do if the calculations take a long time (for example your calculating a lot of positions or vertex data) is you can create your own threadpool class (unfortunately C#'s base one isn’t supported by this version of monodevelop but threading is) and do the long calculations async but when they are finished you send them back to the mainthread through a dispatch class to actually be instantiated but now it just has to add an empty game object and give it the data you calculated async. If an object is taking a long time to instantiate that’s because your doing some hefty lifting in your Start() function or your trying to load something within the mainthread.

Pooling is another option if you are just wanting a lot of the same type of object or you are creating and destroying a lot of objects. It’s basically controlling memory by preventing the need to dealloc and realloc objects. Instead when one is destroyed you simply disable it and when it needs to be recreated you reset it to some set of values and simply reenable it. Lots of ways to do this all dependent on what your situation is.

Object means having the gameObject already there when you start the game, but it is not active. You make it active when needed, and make it inactive rather than destroy it so that you can make it active again when needed.

If you might have more than one of an object active at one time, you have enough copies waiting.