- Home /
creating 2d table\chess board\2d array
Hi guys! I want to create a simple 2d chess-like board, I want it to be dynamic i.e. each "square" can change color, and board size can grow (for example from 10x10 to 12x12), and I also want to be able to identify if a specific "square" got clicked on.
I have read a lot of articles on the internet, and couldnt find a clear answer- from what I read, such a board would cause performance issues. From what Ive understood there are 3 options: 1. create a grid and create cubes on it - can someone please further explain what exactly is a "grid" in unity? 2. use 2d-array and manipulate it to show the squares - dont really know how 3. create 10x10 rects a locate them - can cause performance issues?
Can someone please help me understand what is the prefered option, and maybe guide me of where to start?
Thanks
I don't think you're going to hit performance issues at 10x10 (100) or 12x12 (144). 100x100 (10,000) might be a different matter.
Answer by gfallasc · May 08, 2014 at 10:00 PM
Hi, I don't see any problem creating 2d arrays.
If by a grid means using a single mesh for the whole table, it's very hard for the logic game using a single mesh.
Yes, you could use a "2D array" or matrix which easily could be manipulated by example using a List (C#), and that list could look like this: List< SquareClass[] > my2DArray supossing the "SquareClass" is a script for each square block (I don't think there would be a perfomance issue using something like this).
For the point three, I think that couldn't be the better option to implement the game, because if you're using a class behavior for each block, then each block could easily manage the input, and dynamically change the color (you could change the material).
Answer by hippysniper · May 08, 2014 at 10:14 PM
no you will not get any performance issues with an array that size or that little objects, it may take a second or less to instantiate but it will not effect the game in anyway. to give you an example i recently made a bomber man game which would instantiate boxes in accordance with the location. the player could dynamically edit the map and watch the boxes spawn on screen we only started to experience lag issues when the player made a 500x500 plus array for the level but once instantiated the game ran well.
as far as your question about a grid i am assuming that they are simply talking about the array that you will place you game objects into.
the best way to get started would to do some practice on assigning and manipulating small arrays. I would start with this method (i have not tested the code as i am on my phone =) )
private GameObject[] grid;
public Gameobject block;
public float blockSize;
void Start(){
grid = new grid[2,2];
}
void Update(){
for(int i = 0; i <grid.length; i++){
for(int j = 0; j <grid.height; j++){
temp = (GameObject) instantiate(Block, new vector3(i * blockSize, j * blockSize,0),quternion.identity);
grid[i,j] = temp;
}
}
}
Hi Thanks for the code, I played with it a bit, and figured out how to create new objects.
Your answer
Follow this Question
Related Questions
Highlighting tiles on a hexagonal grid 1 Answer
Creating a 2D Movement Grid 0 Answers
Texture grid displayed oddly when width =/= height 1 Answer
Paint Grid Texture 1 Answer