Best practice for setting up a grid a looping through it

Hi

First time post so please go easy :slight_smile:

I’m trying to use some cellular automata in Unity. I have plenty of experience of this in a 2D environment but this is my first time in 3D.

In a 2D environment I create a 2d array to be the grid and often for level creation I will actually write the start position of each cell in the grid by hand. Then every tick I loop through the whole array and apply whatever game logic I need.

I figure in a 3D environment I can do the same but use a 3d array, although it seems writing out the start positions by hand would take a long long time, but I have already planned ways around that.

I would like to have a rather large grid (say 20 x 50 x 10) I think this should be possible in Unity though (although getting pretty close to the limits).

Now I have a few questions, is a 3D array the best way to implement this?

And I have very little experience drawing items in 3D, so my plan was to create a prefab and add it in wherever the code requires one. My only problems with this are often a cell will contain a prefab but won’t be visible to the user(because of other blocks on top) should I still be creating the prefab in this situation?

After reading lots of forum posts I think if I use meshes I can get a performance increase, so for a start what are meshes? and do you have any links/hints on how I should be using them in my situation?

Thanks for taking the time to read my post and any help you can provide :slight_smile:

A 3D array should be fine, but you might want to consider a 1D array because it will make looping over all items easier. You would then have a function that took x,y,z and turned it into a 1D index.

You can create the prefabs that the user doesn’t see - clearly this does have a performance impact, but Unity will do its best not to draw things which are invisible. I’d do that first and see if the performance is acceptable before moving on to optimize if you feel it is necessary. (The first level of that would be looping through your array and turning of the renderer for items you knew to be invisible).

So a prefab is just a collection of gameobjects and components, if you want to see something then one of those components is going to have to have a renderer and that renderer is going to display a 3D object either made from a mesh (a model that you import) or a primitive. Performance will be better if the model has fewer sides, so avoid very smoothed meshes as they contain a lot of sides or faces.

Welcome to the Unity Community :slight_smile:

is a 3D array the best way to implement this?

I would say so. Unless you have some very fancy algorithm for your logic, you will have to check every grid cell in every tick anyway and if I remember it correctly, a simple array is still the fastest of all data structs when it comes to iterating over the whole thing.

so my plan was to create a prefab and add it in wherever the code requires one. My only problems with this are often a cell will contain a prefab but won’t be visible to the user(because of other blocks on top) should I still be creating the prefab in this situation?

Instead of constantly creating and deleting prefabs, just create all of them at the beginning and use GameObject.enable to enable/disable them. You will also have to enable blocks that might not be visible by the user, mainly because the cost for manually checking if they are visible are normally higher than just drawing the stuff. If you do have access to the pro version of Unity, you can enable occlusion culling http://unity3d.com/support/documentation/Manual/Occlusion%20Culling so unity will automatically do that for you.

After reading lots of forum posts I think if I use meshes I can get a performance increase, so for a start what are meshes? and do you have any links/hints on how I should be using them in my situation?

A mesh is … hm … basically the basic part of a 3d object. A collection of vertices that defines the shape of an object in 3d space. It usually comes together with a material (something that defines the look and color of that object). So if you want to draw anything 3d, it will have to have a mesh :). For your task, I would recommend to use something with as few vertices as possible (like the simple cube you can create with GameObject → Create Other → Cube ) to minimize the performance impact. Also use a material with a very simple shader (the regular diffuse will be just fine) and share the material across all cubes to allow for batch rendering.

Hope I didn’t miss anything important :slight_smile:

Also I have seen mentioned hash tables might be good for this but again after reading the wikipedia I feel I am not any more enlightened on the subject.