I’m using Instantiate to load a large number of low-poly objects in my scene and for some reason it begins to run through this code very slowly after getting about halfway through (no matter how many objects I have set to load).
for(int i = -gridSize; i < gridSize; i++)
{
yield return new WaitForSeconds (0.01F);
for(int j = -gridSize; j < gridSize; j++)
{
string btnName = "Grid/GridPoint";
GameObject pointButton =
GameObject.Find(btnName);
GameObject pointButtonClone = Instantiate(pointButton) as GameObject;
pointButtonClone.name = "GridPoint(" + i + "," + j + ")";
pointButtonClone.transform.parent = pointButton.transform.parent;
pointButtonClone.transform.localPosition =
new Vector3(0 + (i * 25), 0 - (j * 25), 0);
pointButtonClone.transform.localScale =
new Vector3(5,5,5);
int gs = (gridSize * 2) * (gridSize * 2);
pb.sliderValue += 1.0F / gs;
}
}
gridSize could be set to a number like 32 which would Instantiate 4,096 objects. Once the for loop gets i to 0, it begins to load very slowly. I can load the same number of objects very quickly by changing the loop to for(i = -gridSize*2; i < 0; i++) … the i var never has to pass 0 so it never slows down.
Any clues? The code looks alright? You might could tell from the yield, it’s a Coroutine if that matters.
It must have something to do with it adding objects at the origin, when i hits 0, it just suddenly bogs down until it finished the couroutine (down to <10fps and then back up to 60+fps after finishing…) I wondered if it had something to do with objects suddenly being within the camera view, so I moved the camera that can see the objects, no difference. Although there are 3 cameras involved (main camera, space unity camera, and ngui camera).
Instantiation objects in runtime definitely takes a toll in Unity.
What I recommend, is going with the “objects pool” approach.
Basically, you instantiate everything at the Initialization of the scene. And then when required, oyu move the objects into view, or do whatever you need to do with them.
Thanks for the advice. That’s actually what I’m doing if I understand you correctly. The coroutine is started in my Start method. Is it because it’s being instantiated within the cam’s frustum? I guess I could go further with this and Instantiate them all first without changing their position, then once they’re all Instantiated, loop through all of them and update their position… still doesn’t make full sense why it lags so bad all of a sudden then goes back to normal.
I believe there is a bug here somewhere. I have determined that it is caused by what the game object’s name is set to.
Line: pointButtonClone.name = “GridPoint(” + i + “,” + j + “)”;
This is meant to give the game object a unique name that will allow me to easily recall it.
Cases:
Not changing anything: Once i = 0, the loading process becomes slow until it has completed, then comes back to 60FPS.
Commenting out this line causes it to load very slowly the entire time. note: with it commented out each object has the same name.
Changing the line to pointButtonClone.name = “GridPoint(” + (i - 100000) + “,” + j + “)”; makes the entire loading process very fast. note: the i var within the name will always be a negative number, and somehow that matters? That’s weird to me.