일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 크래프톤
- ETRI 연구연수
- 크래프톤 정글랩
- unity 게임 개발
- 오픽 im1
- protobuf란
- 유니티 독학
- unity 설치
- 크래프톤 정글 게임랩
- unity 게임
- 게임 개발
- unity 강의
- 크래프톤 게임 정글 랩
- 유니티 체스
- 크래프톤 정글 게임 랩
- 게임 독학
- unity
- Unity 독학
- 유니티 스토리
- unity 공부
- 유니티 퀘스트
- 게임 개발 독학
- protobuf 란?
- unity 개발
- 오픽 im1 5일
- 유니티 대화
- 유니티
- node.js
- Unity Chess
- 유니티 미연시
- Today
- Total
하참이의 아이디어노트
유니티로 2D 체스 만들기 (2) 본문
https://www.youtube.com/watch?v=GfZeLJfk3RQ&list=PL72bFQgYwe2t_T4ha7nJWc0OduWANpqz9&index=2
글 작성에 앞서 이 주제와 관련된 글들은 위 영상을 참고하여 작성되었음을 미리 밝힙니다.
지난 글에서 제작한 환경에서 체스 말을 만들어 보도록하겠습니다.
체스말 프리팹 만들기
체스말 프리팹을 만들어봅시다. 우선 빈 게임 오브젝트를 생성합니다. 이름은 Chesspiece로 작성하였습니다.
체스 말에 이미지를 붙여주기 위하여 Chesspiece 오브젝트에 Sprite Render 컴포넌트를 추가해줍니다.
예시로 넣어줄 아무 체스말 이미지를 Sprite Render의 Sprite에 넣어줍니다. 안하여도 문제가 없는 작업이지만 이는 이후에 게임매니저가 프리팹을 잘 생성했는지 테스트 단계에서 확인을 할 수 있기 때문에 왠만한 프리팹의 이미지는 비워놓는것보단 채워넣는 것을 권장합니다.
분명 Chesspiece는 Scene에 존재하고 예시 스프라이트도 넣어주었는데 게임에서 보이지 않습니다.
이는 board와 Chesspiece의 Z가 둘 다 0이기 때문에 2D 게임상에서 곂쳐져있는 것으로 취급되어 보이지 않게 되기 때문입니다.
Chesspiece가 board보다 더 위에 보여야 하므로 Chesspiece의 Z position을 -2로 설정합니다. (이후 다룰 예정이지만 -1에 체스말이 움직일 수 있는 범위를 나타내는 타일을 사이에 두어야 하므로 -2로 설정합니다.) 또한 체스 보드도 3배 키웠기 때문에 Chesspiece의 Scale도 3, 3, 1로 설정합니다.
이제 만든 체스말 오브젝트를 프리팹화 하겠습니다. Object 폴더를 만들고 Chesspiece 오브젝트를 드래그합니다.
이제 게임 컨트롤러를 생성하겠습니다. 게임 컨트롤러는 게임 시작시 체스말을 배치하고 체스말을 잡을시 체스말을 보드에서 치우며, 체크메이트 상태를 체크하는등 게임의 전반적인 시스템을 담당합니다.
다른 빈 오브젝트를 생성합니다. 이름은 Controller로 하겠습니다.
스크립트를 작성하겠습니다. 스크립트 폴더를 생성하고 새로운 스크립트를 작성합니다. 폴더의 이름은 Scripts, 스크립트의 이름은 Game로 하겠습니다.
Game.cs Script를 간단하게 작성합니다. 다음 코드는 체스말을 유니티 게임 화면에 생성하는 코드입니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Game : MonoBehaviour
{
public GameObject chesspiece;
void start()
{
Instantiate(chesspiece, new Vector3(0, 0, -1), Quaternion.identity);
}
}
Scene이 시작되면 chesspiece로 등록한 GameObject를 생성하는 코드입니다. 이 코드를 Controller 오브젝트의 컴포넌트로 등록합시다. 이전에 제작한 Controller의 Tag를 GameController로 변경합니다.
Controller 오브젝트에 Game.cs 스크립트 컴포넌트를 추가합니다.
Game 컴포넌트에 Chesspiece에 Chesspiece 프리팹을 등록합니다.
저장을 하고 다른 스크립트를 작성합니다. Chessman.cs 스크립트를 생성합니다.
Chessman.cs 스크립트를 다음과 같이 작성합니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chessman : MonoBehaviour
{
// References
public GameObject controller;
public GameObject movePlate;
// Positions
private int xBoard = -1;
private int yBoard = -1;
// Variable to keep track of "black" player of "white" player
private string player;
// References for all the sptrites that the chesspiece can be
public Sprite black_queen, black_knight, black_bishop, black_king, black_rook, black_pawn;
public Sprite white_queen, white_knight, white_bishop, white_king, white_rook, white_pawn;
}
chessman은 체스말에 적용할 스크립트입니다.
controller와 움직일 경로를 미리 보여주는 movePlate 게임오브젝트를 등록하기 위한 참조변수를 선언합니다.
체스말의 위치를 나타내기 위한 xBoard, yBoard를 선언합니다. 위치는 -1로 초기화합니다.
체스말이 어느 팀의 것인지를 구분하기 위한 player 변수를 선언합니다. 문자열 "black"과 "white" 로 구분합니다. (열거형으로 구분하는 것이 제일 좋긴 합니다.)
스프라이트를 구분하기 위하여 각 말의 종류별로 Sprtie 변수를 선언합니다.
작성이 완료되면 저장 후에 유니티 에디터에서 스크립트를 컴포넌트로 등록해보겠습니다. 스크립트를 Chesspiece 오브젝트에 드래그하여 컴포넌트 속성을 추가합니다.
이제 프리팹에 선언했던 체스말 스프라이트 이미지들을 맞춰줍니다. 스프라이트를 이름에 맞게 드래그 하여 Chessman 컴포넌트를 채웁니다.
프리팹이 바뀌었으니 오버라이드를 잊지 않고 변경을 저장해야합니다. Chesspiece의 Override를 열고 Apply All을 클릭합니다.
다음은 체스말 프리팹이 게임컨트롤러를 찾도록 코드를 추가해줍니다. 다음코드를 Chessman 코드 하단에 추가하여 수정합니다.
public void Activate()
{
controller = GameObject.FindGameObjectWithTag("GameController");
// take the instantiated location and adjust the transform
SetCoords();
switch(this.name)
{
case "black_queen": this.GetComponent<SpriteRenderer>().sprite = black_queen; break;
case "black_knight": this.GetComponent<SpriteRenderer>().sprite = black_knight; break;
case "black_bishop": this.GetComponent<SpriteRenderer>().sprite = black_bishop; break;
case "black_king": this.GetComponent<SpriteRenderer>().sprite = black_king; break;
case "black_rook": this.GetComponent<SpriteRenderer>().sprite = black_rook; break;
case "black_pawn": this.GetComponent<SpriteRenderer>().sprite = black_pawn; break;
case "white_queen": this.GetComponent<SpriteRenderer>().sprite = white_queen; break;
case "white_knight": this.GetComponent<SpriteRenderer>().sprite = white_knight; break;
case "white_bishop": this.GetComponent<SpriteRenderer>().sprite = white_bishop; break;
case "white_king": this.GetComponent<SpriteRenderer>().sprite = white_king; break;
case "white_rook": this.GetComponent<SpriteRenderer>().sprite = white_rook; break;
case "white_pawn": this.GetComponent<SpriteRenderer>().sprite = white_pawn; break;
}
}
public void SetCoords()
{
float x = xBoard;
float y = yBoard;
x *= 0.66f;
y *= 0.66f;
x += -2.3f;
y += -2.3f;
this.transform.position = new Vector3(x, y, -1.0f);
}
수정된 코드에 대해서 설명해드리겠습니다.
게임 시작시 체스말 프리팹이 생성되고, 각각의 체스말 종류에 맞게 스프라이트를 설정해주는 Activate 메소드입니다. 각 오브젝트에게 할당된 이름에 맞게 스프라이트를 변경하는 sprtie case문을 사용합니다.
체스보드는 8*8의 보드를 사용하고, 좌측 하단이 (0,0) 우측 상단이 (7, 7)의 좌표로 코드를 작성하면, 이를 유니티 좌표로 변경(조절) 하기 위해 만드는 유틸리티 메소드인 SetCoords() 메소드 입니다.
수정된 Chessman의 전체 코드는 다음과 같습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chessman : MonoBehaviour
{
// References
public GameObject controller;
public GameObject movePlate;
// Positions
private int xBoard = -1;
private int yBoard = -1;
// Variable to keep track of "black" player of "white" player
private string player;
// References for all the sptrites that the chesspiece can be
public Sprite black_queen, black_knight, black_bishop, black_king, black_rook, black_pawn;
public Sprite white_queen, white_knight, white_bishop, white_king, white_rook, white_pawn;
public void Activate()
{
controller = GameObject.FindGameObjectWithTag("GameController");
// take the instantiated location and adjust the transform
SetCoords();
switch(this.name)
{
case "black_queen": this.GetComponent<SpriteRenderer>().sprite = black_queen; break;
case "black_knight": this.GetComponent<SpriteRenderer>().sprite = black_knight; break;
case "black_bishop": this.GetComponent<SpriteRenderer>().sprite = black_bishop; break;
case "black_king": this.GetComponent<SpriteRenderer>().sprite = black_king; break;
case "black_rook": this.GetComponent<SpriteRenderer>().sprite = black_rook; break;
case "black_pawn": this.GetComponent<SpriteRenderer>().sprite = black_pawn; break;
case "white_queen": this.GetComponent<SpriteRenderer>().sprite = white_queen; break;
case "white_knight": this.GetComponent<SpriteRenderer>().sprite = white_knight; break;
case "white_bishop": this.GetComponent<SpriteRenderer>().sprite = white_bishop; break;
case "white_king": this.GetComponent<SpriteRenderer>().sprite = white_king; break;
case "white_rook": this.GetComponent<SpriteRenderer>().sprite = white_rook; break;
case "white_pawn": this.GetComponent<SpriteRenderer>().sprite = white_pawn; break;
}
}
public void SetCoords()
{
float x = xBoard;
float y = yBoard;
x *= 0.66f;
y *= 0.66f;
x += -2.3f;
y += -2.3f;
this.transform.position = new Vector3(x, y, -1.0f);
}
}
이번 글에서는 체스말의 프리팹을 만들어보았습니다. 아직 체스말의 기본 틀만 만들었고 게임의 기초적인 기능, 예를 들어 말을 움직이고 잡고, 체크를 하는 등의 기능들이 구현이 되지 않았습니다. 이러한 기초의 전반적인 이해를 바탕으로 더욱더 고급진 기술을 배우게 될 수 있으면 좋겠습니다. 긴 글 읽어주셔서 감사합니다!
'Unity > Unity Chess' 카테고리의 다른 글
유니티로 2D 체스 만들기 (5) (0) | 2024.03.20 |
---|---|
유니티로 2D 체스 만들기 (4) (0) | 2024.03.18 |
유니티로 2D 체스 만들기 (3) (0) | 2024.03.16 |
유니티로 2D 체스 만들기 (1) (0) | 2024.03.16 |
유니티로 2D 체스 만들기 (0) (0) | 2024.03.16 |