Is it possible to instantiate a single, large prefab over multiple frames?

Hello Unity Community,

I’ve got some large, complex prefabs (400-600 GameObjects with Rigidbodies, Colliders, Renderers, etc) that I’m trying to instantiate at run-time like so:

public GameObject MyLargePrefab;
void Start() {

but I’m getting a large single frame FPS spike due to calling Instantiate() and “Mesh.Bake Scaled Mesh PhysX CollisionData” in the Profiler.

I would say these large prefabs are not “optimized” and make heavy use of Kinematic Rigidbodies with MeshColliders, but I would like to know if there is an elegant way to split the instantiation up over multiple frames while still keeping everything in a single, original prefab.

ie: A coroutine that calls Instantiate() once per frame on the children of MyLargePrefab and assigns their transform.parent to “reconstruct” the object. (however this is not ideal because it enforces requirements on how the prefabs are authored)


There is no way to spawn a prefab over multiple frames. As others have mentioned, if you have Pro then you can use scenes + LoadLevelAdditiveAsync to achieve a similar effect.

SECTR STREAM basically provides tools and runtime that build on top of that very foundation.

Sadly, if you use LoadLevelAdditiveAsync to load a scene and then move or reparent that scene, then you’ll pay the full physx static collider rebuild cost a second time :frowning:

I don’t know if this is going to work, but here’s what I would do:

    using System;
    using System.Collections;
    using UnityEngine;
    		public class Test : MonoBehaviour
    		public GameObject prefab;
    		public float noOfObjectsPerUpdate;
    		private GameObject[] singleGameObjects;
    		private int noOfGameObjectsInPrefab;
    		private int itterator = 0;
    		public void Create() {
    			Transform[] t = prefab.GetComponentsInChildren<Transform>();
    			singleGameObjects = new GameObject[t.Length];
    			for(int i =0; i< t.Length; i++) 
    				singleGameObjects _= t*.gameObject;*_

* }*
* noOfGameObjectsInPrefab = prefab.GetComponentsInChildren().Length;*
* StartCoroutine(“make”);*
* }*
* IEnumerator make() {*
* yield return new WaitForSeconds(Time.deltaTime);*
* for(int i = 0; i< noOfObjectsPerUpdate; i++)*
* {*
* GameObject g = Instantiate(singleGameObjects[itterator],singleGameObjects[itterator].transform.position, singleGameObjects[itterator].transform.rotation) as GameObject;*
* }*
* }*

* }*