I have this game in unity with a lot of objects, i’m using this code so i would imagine that the game would be efficient no matter how many objects there is:
void OnBecameVisible(){
foreach (Transform obj in transform) {
obj.gameObject.SetActive(true);
}
}
void OnBecameInvisible(){
if (stay == false) {
foreach (Transform obj in transform) {
obj.gameObject.SetActive(false);
}
}
}
So why is the game running efficient at < 100k objects but starts to lag at around 200k, i though that code would eliminate the use of updating or rendering objects outside of the Players camera. Why is it lagging with more objects if i’m using this code. The game only has to render about 10-100 objects at a time no matter how many objects altogether so why is it lagging with more objects?
Doesn’t sound like a gpu bottleneck. Yeah, you don’t have to render more objects, but you still have to keep the transforms/etc of all the models in memory so that you can turn them back on when necessary. also, it still takes processing power to do the enabling/disabling. Maybe more than you’d think if you’re working with something on the order of hundreds of thousands of objects. What on earth are you using all of those objects for, anyway?
I was planning to have over 10M objects but i guess that sounds like a bad idea, unless i store all the transforms in a main memory script, i think this would work because all the objects transforms have something in common, they are all in a predictable pattern.
I would just have to think of an algorithm to calculate the pattern of the transforms + the players transform then do some maths to estimate the positions they would be in, instead of storing all the values in an array. Do you think that would work?
As for your question “What on earth are you using all of those objects”:
I’m making a minecraft-like game!
It’s commonly used to track down bottlenecks and to find the pieces of code that would give the biggest performance increase if they could be optimized (just because something does take up a lot of processing power doesn’t necessarily mean it can be optimized. Maybe it’s just something that’s computationally very expensive, even if done right).
Unity might be rendering only 10-100 objects at the time, but deciding weather to render them or not does take some processing power.
But if you are only showing 10-100 objects at the time there might not be any need to overwhelm unity with 200k objects in it’s hierarchy, instead you could instantiate objects when they are needed. This way the program works the same if you have 1k or 1M objects. This shouldn’t be a problem to implement as you said objects are in a predictable pattern.
I hope you are not handling every every voxel / block as a separate object?
That would be madness. Handling a chunk as a separate object sound much more feasible.
Of course i’m not handling them all separate, just like you said i’m handling it in chunks. Cant you tell by my code that i use a for-each statement to disable the blocks in a chunk?
You can’t foreach through so many objects, sooner or later you will hit a performance limit. I have never implemented gameplay like this, but my assumption is that the elements that make up the world of such a game are stored in some sort of pool. From which they are fetched when some view frustum manager says they are required. My guess is that for such a game you need some pretty good knowledge of Octrees, procedural level generation and such. I would also suggest to study other people’s code. Searching for “Minecraft” or “Octree” on the Unity Asset Store gives me some results, and a search outside of Unity’s realm might be helpful too.
I can say that although it probably isn’t a good idea for a minecraft game, I have had success with procedural works using lots of prefabs. In my case though, the amount of objects rarely goes over 250-500, and the game runs fast enough on mobile for me to simply leave it at that.
I also heard somewhere that foreach can be a bit slow; you could try instead caching how many transforms to change, and use that number in a normal for-loop, that way, it knows how many there are ahead of time, giving a small boost.
EDIT: I will also point out that my game’s procedural generator has no scripts attached to the building blocks of my levels… that’s something to consider.