I'm building a game using a 3d grid based, procedurally generated world (same world concept as Dwarf Fortress/Towns).
For the Blocks i use a 3D array of Block instances (normal class, not GameObject), like this: Block[,,] blocks = new Block[sizeX,sizeY,sizeZ]. The block object contains all relevant block info like positions, and bools like "isWalkable" and vertice positions for rendering (they are then combined into "Chunks" for rendering)
This runs quite well, even with a million Blocks (128x128x64)
My question: Would it be better to store the data in seperate arrays? For example have arrays "BlockWalkable[,,]", "BlockVerts[,,]"...
This way i could get rid of Objects.
I'd like to get rid of as much overhead as possible to make room for implementing pathfinding later... Would there be any advantages for lookups for example? Or memory savings? I woult test it but it would require quite a large rewrite, so i thought i'd ask first.
Answer by senad
Feb 20, 2012 at 10:05 AM
I do not remember the numbers exactly, but the overhead for having a class in say C# is in the magnitude of a few bytes. If you have a few million objects that makes a few megabytes, it could still be tolerable on the PC. Depends a lot on your memory budget.
The performance may suffer if you use methods (and I guess properties count as methods) to look up values as opposed to just look up a value in an array.
However, my opinion is this: the readability (and maintaining and testing) of your code will likely be much harder if you do these optimisations. So if it was my project, I would definitively do some profiling to identify the bottlenecks before I start optimising.
Thanks for the information. I think I'll leave it as it is for now (with Objects) and if i REALLY need to optimise something I'll look into it again.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
6 People are following this question.
Clicking on objects
Creating custom objects?
Instantiate an object next to an object
Beginner : Random gameobject from array
Getting data from Arraylist within Arraylist