Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411511 Posts in 69375 Topics- by 58430 Members - Latest Member: Jesse Webb

April 26, 2024, 01:38:51 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Can you help me optimize this function?
Pages: [1]
Print
Author Topic: Can you help me optimize this function?  (Read 934 times)
darkhog
Level 7
**


Dragon Agent


View Profile
« on: May 30, 2018, 10:13:49 AM »

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:
Code:
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!
Logged


Be a computer virus!


I cannot C well, so I stick with simpler languages.

There are no impossible things, there is only lack of skill.
pixeye
Level 1
*


You have my mace


View Profile WWW
« Reply #1 on: May 30, 2018, 10:44:09 AM »

It's difficult to say as there are many other functions involved as far as I can see.

Code:
go.transform.parent = GameObject.Find("LevelRoot").transform;
Why would you want to Find LevelRoot each time? Instead you can make a simple service that cache the root and reuse it.

If you have tons of objects to spawn it will save you a bit of time.
Logged

darkhog
Level 7
**


Dragon Agent


View Profile
« Reply #2 on: May 30, 2018, 11:05:39 AM »

You're right, I really missed that! As to other functions, they're minuscule in the time footprint (timeprint? lol). I'll do that optimization you've suggested and see where I'm at.
Logged


Be a computer virus!


I cannot C well, so I stick with simpler languages.

There are no impossible things, there is only lack of skill.
sundance
Level 0
*


View Profile
« Reply #3 on: June 25, 2018, 10:45:27 AM »

If you are reusing the same meshes with the same materials you could look into GPU instancing. I'll leave the link here: https://docs.unity3d.com/Manual/GPUInstancing.html

To create variation from there you'll need to edit your shaders to adjust based on each unique instance. But its a little hard to know whats going on without looking into those other functions.
Logged
oahda
Level 10
*****



View Profile
« Reply #4 on: June 25, 2018, 10:53:08 AM »

Code:
//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;

Code:
go.transform.rotation = Quaternion.Euler(lo.rotX, lo.rotY, lo.rotZ);

c;
Logged

darkhog
Level 7
**


Dragon Agent


View Profile
« Reply #5 on: June 25, 2018, 11:09:21 AM »

Thanks, but this thread is no longer relevant. Tried to do all possible suggested optimization suggested here and on Unity Answers and none fixed the problem, because it looks like Instantiate is terribly slow and it's a kind of function that I can't modify and speed up. Instead unfortunately I had to implement a loading screen.
Logged


Be a computer virus!


I cannot C well, so I stick with simpler languages.

There are no impossible things, there is only lack of skill.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic