To begin with, in order to access and update your board with ease, you can make use of the Dictionary<Key, Value>
class together with the Vector2
structure defined by Unity.
Dictionary<Vector2, Tile> tiles = new Dictionary<Vector2, Tile>();
Tile
is a class defined by you. It should contain information such as its coordinates on the board, and whether it is occupied by any entity etc. You can define a simple Tile
class as follows.
using UnityEngine;
using System.Collections;
public class Tile : MonoBehaviour {
public Vector2 coordinates;
public ChessPiece chessPiece;
// add other fields as you want to
}
Now, let's wrap things up by a Board
class.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Board : MonoBehaviour {
public Dictionary<Vector2, Tile> tiles = new Dictionary<Vector2, Tile>();
// add other functions later
}
Of course, you need chess pieces to play with.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider))]
public class ChessPiece : MonoBehaviour {
// add other functions later
}
The management of the board should be centralized with the presence of a "Manager". This "Manager" is in charge of handling the transition of states of the board. Again, this "Manager" should be implemented by yourself.
using UnityEngine;
using System.Collections;
public class BoardManager : MonoBehaviour {
public Board board;
void Awake() {
GameObject boardObject = new GameObject("board");
boardObject.transform.parent = transform;
board = boardObject.AddComponent<Board>();
}
// add other functions later
}
Colliders are often used by different physics-related functionalities. For instance, in a game of chess, each chess piece should have a collider attached to it. The collider, in this case, is mainly used for receiving user input, such as tapping and dragging. There are many excellent plugins available in the Unity Asset Store for handling different kinds of user input/gestures. By using these plugins, you are being freed from handling those tedious input-related logic. For example, there is certain plugin which allows to write functions like this.
// a function for handling 'tapping' on a chess piece
void OnTap(Gesture gesture) {
if (gesture.pickObject == gameObject) {
// process 'tapping' on this chess piece...
}
}
Of course, the code snippet above covers only the basic usage of these plugins. You should refer to the official manuals provided by the authors of those plugins for more detailed explanations.
To summarize, you should at least have the following stuff prepared:
- Manager, responsible for managing the board
- Dictionary, storing the layout of the board
- Chess Piece, represents each individual chess piece on the board
- Tile, represents each individual tile on the board
UPDATE
Here is a screenshot which shows a hierarchy of GameObject
in a simple chess game.

Of course, you don't need to follow it strictly. The structure can be very flexible. It is really important to experiment with different structures, so as to find a way to handle gaming logic which you feel comfortable while giving good performance.