본문 바로가기

21 - 2학기/슬기로운 코딩생활

[TIL] 유니티2d로 게임 개발 4주차

TAB 2021 겨울방학 학회활동 [슬기로운 코딩생활]

4주차 TIL입니다.

작성자 : 38기_이병헌


4주차 - 되돌리기, 리셋 기능 만들기, 이동 횟수 표시


리셋 버튼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Board_controller : MonoBehaviour
{
    public static int block_count, move_count; //블럭의 개수, 움직인 횟수
    float[] block_pos; //블럭의 좌표 배열

    // Start is called before the first frame update
    void Start()
    {
        block_count = transform.childCount; //자식 오브젝트의 개수로 블럭 개수 저장
        block_pos = new float[block_count];

        for (int i = 0; i < block_count; i++)
        {
            if(transform.GetChild(i).name.Substring(0,1) == "X") //블럭의 이름의 앞글자에 따라 x, y좌표 저장
            {
                block_pos[i] = transform.GetChild(i).transform.position.x;
            }
            else
            {
                block_pos[i] = transform.GetChild(i).transform.position.y;
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
    
    }

    public void resetButton()
    {
        for (int i = 0; i < block_count; i++)
        {
            if (transform.GetChild(i).name.Substring(0, 1) == "X") //블럭의 이름의 앞글자에 따라 x, y좌표 저장
            {
                transform.GetChild(i).transform.position = new Vector2(block_pos[i], transform.GetChild(i).transform.position.y);
            }
            else
            {
                transform.GetChild(i).transform.position = new Vector2(transform.GetChild(i).transform.position.x, block_pos[i]);
            }
        }
    }

}

보드의 스크립트를 만들고 블럭들에 자식으로 접근하여 좌표를 배열에 저장한다.

리셋 버튼이 눌리면 저장된 좌표로 블럭들을 이동시킨다.


되돌리기 버튼
움직인 횟수

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Board_controller : MonoBehaviour
{
    public static int move_count; //움직인 횟수
    public static GameObject block_before; //선택 블럭
    public static Vector2 block_vec; //블럭의 이전 위치
    Text moves; //텍스트

    // Start is called before the first frame update
    void Start()
    {
        move_count = 0;
        moves = GameObject.Find("Moves").GetComponent<Text>(); //Moves 텍스트 변수 적용
    }

    // Update is called once per frame
    void Update()
    {
        moves.text = "Moves" + move_count.ToString(); //Moves 횟수 업데이트
    }

    public void backButton()
    {
        if (block_before) //선택 된 블럭이 있을시
        {
            transform.Find(block_before.name).transform.position = block_vec; //블럭 원위치
            move_count -= 1; //이동횟수 - 1
            block_before = null; //선택 블럭 초기화
        }
    }
}

마지막으로 옮겼던 블럭을 블럭 스크립트에서 가져와서 저장한 후, 되돌리기 버튼을 누르면 원위치 시킨다.

움직인 횟수를 텍스트로 나타내어 준다.


결과 화면

    public void checkResult()
    {
        if (block_before)
        {
            if (transform.Find(block_before.name).transform.position.x >= 300) //블럭이 출구를 통과하면 종료화면 표시
            {
                end_screen.SetActive(true);
                if (move_count == minimum_moves) //이동횟수에 따라 문구 변화
                {
                    result.text = "Perfect!!";
                }
                else
                {
                    result.text = "Win!";
                }
            }
        }
    }

게임을 클리어하면 결과화면이 나오도록 한다. 최소이동횟수로 클리어시 다른 문구가 나온다.

테스트2.gif