The following function takes 2-4ms to run. This may not seem like a lot, but since this is a part of my level loading routine (I load levels from a custom format that contains object IDs and some metadata that allows it to function), if you multiply it by few thousands objects in a level (which isn't unreasonable in case of my game as some objects act as level geometry), the loading times become very long (I've already optimized it to be more or less 2x faster, however it still load the level I'm working on in appr. 1 minute - and the level is pretty blank now, just having a lot of fencing around).
Can someone help me get this optimized? I fear the problem is Instantiate, however I need to create objects out of thin air for this so pooling is not an option, unfortunately.
The function is as follows:
public static void SpawnObject(LevelObject lo){
//Debug.Log("[" + System.DateTime.Now.ToLongTimeString() + " @ " + System.DateTime.Now.Millisecond +"ms] Spawning object " + Globals.categories[lo.CategoryID].objects[lo.ObjectID].prefabName);
GameObject go = Instantiate(Globals.EditorObjectCache[Globals.categories[lo.CategoryID].objects[lo.ObjectID].prefabName]);
//Debug.Log("[" + System.DateTime.Now.ToLongTimeString() + " @ " + System.DateTime.Now.Millisecond + "ms] Instantiate done");
//setting up position
go.transform.position = new Vector3 (lo.posX,lo.posY,lo.posZ);
//setting up rotation. We have to do this the convoluted way, because fucking Unity didn't gave us fucking way to just modify fucking
//quaternions fucking directly. Fuck.
Quaternion quat = go.transform.rotation;
Vector3 newrot = new Vector3(lo.rotX,lo.rotY,lo.rotZ);
quat.eulerAngles = newrot;
go.transform.rotation = quat;
//setting up scale
go.transform.localScale = new Vector3(lo.scaleX,lo.scaleY,lo.scaleZ);
LevelObjectData lod = go.GetComponent<LevelObjectData>();
//setting up level object data - this will take care of color and trigger data
lod.setLevelObject(lo);
lod.refreshColor();
lod.refreshMaterial();
//setting up child-parent relation via Unity's child services.
go.transform.parent = GameObject.Find("LevelRoot").transform;
lod.setLevelObject(lo);
//TODO: Stop being so humorous in game's code or there will be no humor left for actual game dialogs!
}
Thanks in advance for any and all help!