public static void SpawnObject(LevelObject lo, GameObject levelRoot){
//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], new Vector3(lo.posX, lo.posY, lo.posZ),Quaternion.Euler(lo.rotX, lo.rotY, lo.rotZ),levelRoot.transform);
//Debug.Log("[" + System.DateTime.Now.ToLongTimeString() + " @ " + System.DateTime.Now.Millisecond + "ms] Instantiate done");
go.transform.localScale = new Vector3(lo.scaleX,lo.scaleY,lo.scaleZ);
LevelObjectData lod = go.GetComponent<LevelObjectData>();
lod.setLevelObject(lo);
//TODO: Stop being so humorous in game's code or there will be no humor left for actual game dialogs!
}
The difference between outputs of the commented debug logs is about 2-3ms and that’s instantiate alone. Frankly, the instantiate takes the bulk of time it takes to run this function and unfortunately I haven’t been able to optimize it further. The rest of the logic takes 1ms at most. Not very good when said function is a part of custom load routine (for maximum modability, I’ve developed my own editor and use it instead of Unity’s scene editor which is only used for menus) and as such can be called tens of thousands times in a row!
31.05.2018: Updated the function to closely resemble what it does look like now. Yep, definitely Instantiate is to blame, cut other cruft (some functions seemed to be called twice or even thrice), but it still made no difference. Still my level loads in about 1 minute (it has about 10k objects, not counting those that are in the scene to begin with like UIs and managers). Unity definitely needs to improve Instantiate’s performance.