Join Kelley Hecker for an in-depth discussion in this video Create a grid, part of Building a Match 3 Game with Unity.
- [Voiceover] Our Game will take place on a grid, that hold all the Game pieces. When you clear pieces from the grid new ones will fill in from the top. I've opened up the main scene and the exercise files, and I just have a camera, a sprite, with a background texture. And a grid object with a grid script. The project also has some assets we'll need like textures and generic piece prefab that we'll build on later. If you open up the grid script, you can see it doesn't have much in it right now.
But we're going to add to it to create a grid to hold and display all of our game pieces. We'll start by adding some parameters that we can adjust in the inspector. Right now I'm adding two integers, which are going to be X and Y dimensions of our grid. This will let us control the size of our grid. If you save the file and go back to the editor, look in the inspector you can see that we can set these values here. So go a head and enter nine for both which will give as a nine by nine grid. Going back to the grid script the grid needs to know what type of pieces we can have in it.
I'm going to add an enum for the piece type. Right now we just have one piece type. which is the Normal type. I'm also adding count, which is, just so we know how many piece types the're. We want to associate each piece type with the prefab. we can do this by using a Dictionary, which associates a key which will be our piece type with the value which will be our GameObject. At the top, add using Systems.Collections.Generic, so we have access to the Dictionary class.
We'll then define a Private Dictionary, which has keys of piece type, and values of type GameObject. The reason I made this private is because dictionaries can't be displayed in the inspector. We'll need to create struct with our key and value, and have an array of those instead. I'm creating a struct called Piece Prefab, that has a piece type, and a GameObject. I'm also going to add the System.Serializable flag, so at our customs struct will show up in our inspector.
Now that I've defined our struct, we can have an array of them, though we can edit them in our inspector. Go a head and save, and go back to the editor. Expand the piece prefab of array and set the size to one. Here we can asigh the prefab for the Normal type of Game piece. Go to the prefabs folder and drag in the normal piece prefab object. To the prefab variable and in the inspector. This now associates this prefab with the Normal Game piece type. Now like I said before, we wanna use a Dictionary but Dictionaries can't be displayed in the inspector, but what will have to do is copy the values from our piece prefab array, which we set the inspector, into our Private Dictionary Object.
First on instantiate Dictionary, as a new Dictionary, we type piece type, and GameObject. And then I'll loop through all of the prefabs in our piece prefabs array. Since the dictionary should only contain one value for each key. We first check if the Dictionary already contains the key. Remember that our key is our piece type. And our value, is our GameObject. If it doesn't already contain the key, then we add a new key value pair to our Dictionary.
Now we want to display a background behind each of our pieces to make them easier to see. So we'll expose another GameObject in the inspector that we can a sigh. Go back to the editor, and then the inspector, drag the piece BG prefab, to the background prefab variable. Now that we have a reference to the background prefab, we can instantiated for each cell of the grid. We first loop through all the rows, from zero to X dimension, And then we loop through all the columns from zero to the Y dimensions.
Then for each cell of the grid we instantiate our background prefab. we pass in the reference to our background prefab, and the position we want the prefab to be instantiated. In this case we want the position to be the X and Y values from our loop. And finally passing the rotation, Quanternion.identity just means that its not rotated at all. And finally we'll make this new background, a child of our grid object . If you go to editor and hit play, you can see the real background tile for each cell of the grid.
Next we'll look at how to place a Game piece on top of each of these background cells.
Game developer Kelley Hecker covers topics like creating a grid-based game board, using inheritance to create different types of game pieces, adding obstacles and new levels, detecting matches and clearing pieces, and implementing a user interface complete with a score screen. By the end of the course, members will have a completed game and learned new techniques to apply to their next Unity project.
- Creating a game board grid
- Creating and scripting game pieces
- Filling the board
- Creating obstacles
- Swapping pieces
- Matching pieces
- Clearing obstacles and pieces
- Creating new levels
- Creating the user interface: HUD, game over screen, etc.