6. Dependencies 탭-> app -> Gson 선택 -> 적용(apply) -> OK
7. 코드 작성해보기
@Test
public void toSJON() {
Gson g = new Gson();
Circle a = new Circle(10f, 20f, 40f);
String json = g.toJson(a);
Circle b = g.fromJson(json, Circle.class);
assertTrue(10f == b.getX());
assertTrue(20f == b.getY());
assertTrue(40f == b.getRadius());
}
Tile: 그리드에서 각 칸에 해당되는 Game Object로써, Board안에서 배열로 위치합니다.
Board: Tile과 GamePiece prefab에 위치(좌표)를 설정하는 Game Object입니다.
1. 보드 (Board)
1. GameObject 생성
Hierarchy에서Create Empty를 선택하여 GameObject를 생성합니다.
생성된 Game Object의 이름을 "Board"로 변경합니다.
2. 스크립트 추가
Project-Assets에서 C#Script를 생성합니다.
파일 이름을 "Board"로 변경합니다.
using UnityEngine;
using System.Collections;
public class Board : MonoBehaviour {
public int width; //
public int height;
public GameObject tilePrefab;
private Tile[,] m_tiles;
void Start ()
{
m_tiles = new Tile[width,height];
SetupTiles();
}
void SetupTiles()
{
for (int i=0; i < width; i++)
{
for (int j=0; j < height; j++)
{
GameObject tile = Instantiate (tilePrefab, new Vector3(i, j, 0), Quaternion.identity) as GameObject;
tile.name = "Tile ("+i+","+j+")";
m_tiles[i,j] = tile.GetComponent<Tile>();
tile.transform.parent = transform;
}
}
}
}
width: 타일의 갯수, height: 타일의 갯수.
tilePrefab: 보드에 배치하는 Game Object.
m_tiles: 좌표에 필요한 tilePrefab의 배열.
SetupTiles():
tilePrefab을 이용하여 Game Object 인스턴스를 생성하여 보드에 나열합니다.
보드에 나열할때는 Tile이라는 prefab을 이용하여 나란히 정렬합니다.
좌표는 for문으로 i는 x축을, j는 y축으로 합니다.
해당 작업이 끝났다면, Board GameObject를 선택하여 Inspector에서
Add Component를 이용하여 Board.cs를 추가합니다.
2. 타일(Tile)
1. GameObject 생성
Hierarchy에서Create Empty를 선택하여 GameObject를 생성합니다.
생성된 Game Object의 이름을 "Tile"로 변경합니다.
2. Rendering 추가
"Tile" Game Object의 Inspector에서 Add Component를 누릅니다.
Rendering > Sprite Renderer를 선택합니다.
위 그림과 같이 안쪽이 비어있는 흰택 테두리 png파일을
Inspector>Sprite Renderer의 Sprite에 추가합니다.
3. 스크립트 추가
Project-Assets에서 C#Script를 생성합니다.
파일 이름을 "Tile"로 변경합니다.
using UnityEngine;
public class Tile : MonoBehaviour {
public int xIdx;
public int yIdx;
private Board m_board;
void Start () {}
public void Init(int x, int y, Board board)
{
xIdx = x;
yIdx = y;
m_board = board;
}
}