실행 이미지

 

 

인접한 두 Cell (상,하,좌,우)을 클릭하면 두 Cell의 이미지를 스왑이 되도록 구현해 보았다.

 

 

스왑이 되면 선택이 초기화 되며,

 

첫번째 선택된 Cell과 두번째 선택한 Cell이 대각선 혹은 인접하지 않다면 선택이 초기화 된다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class PuzzleMain : MonoBehaviour
{
    public int width = 7;
    public int height = 7;

    public GameObject puzzleCell;
    public Transform grid;

    private GameObject[,] board;

    private GameObject firstSelectedPiece;
    private GameObject secondSelectedPiece;

    public Sprite[] fruits;

    void Start()
    {
        board = new GameObject[width, height];

        FillBoard();
    }

    void FillBoard()
    {
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                Vector2 position = new Vector2(i, j);
                int randomIndex = Random.Range(0, fruits.Length);
                GameObject piece = Instantiate(puzzleCell, position, Quaternion.identity, grid);
                piece.GetComponent<Cell>().fruit.sprite = fruits[randomIndex];
                piece.GetComponent<Cell>().puzzleMain = this;
                piece.GetComponent<Cell>().x = i;
                piece.GetComponent<Cell>().y = j;

                board[i, j] = piece;
            }
        }
    }


    public void SelectPiece(GameObject selectedPiece)
    {
        if (firstSelectedPiece == null)
        {
            firstSelectedPiece = selectedPiece;
            selectedPiece.GetComponent<Cell>().SetFocus(true);
        }
        else if (secondSelectedPiece == null && firstSelectedPiece != selectedPiece)
        {
            secondSelectedPiece = selectedPiece;
            // 인접한지 확인
            if (AreAdjacent(firstSelectedPiece, secondSelectedPiece))
            {
                SwapPieces(firstSelectedPiece, secondSelectedPiece);
                firstSelectedPiece.GetComponent<Cell>().SetFocus(false);
                secondSelectedPiece = null;
                firstSelectedPiece = null;
            }
            else
            {
                // 인접하지 않으면 선택 초기화
                firstSelectedPiece.GetComponent<Cell>().SetFocus(false);
                secondSelectedPiece = null;
                firstSelectedPiece = null;
            }
        }
    }

    void SwapPieces(GameObject first, GameObject second)
    {
        Cell firstCell = first.GetComponent<Cell>();
        Cell secondCell = second.GetComponent<Cell>();

        Sprite tempSprite = firstCell.fruit.sprite;
        firstCell.fruit.sprite = secondCell.fruit.sprite;
        secondCell.fruit.sprite = tempSprite;
    }

    bool AreAdjacent(GameObject first, GameObject second) //두 cell이 인접해있는지 확인하는 메소드
    {
        Cell firstCell = first.GetComponent<Cell>();
        Cell secondCell = second.GetComponent<Cell>();

        return (Mathf.Abs(firstCell.x - secondCell.x) == 1 && firstCell.y == secondCell.y) ||
               (Mathf.Abs(firstCell.y - secondCell.y) == 1 && firstCell.x == secondCell.x);
    }
}

PuzzleMain.cs

 

using UnityEngine;
using UnityEngine.UI;

public class Cell : MonoBehaviour
{
    public Image fruit;
    public Image focus;
    public Button selectBtn;
    public PuzzleMain puzzleMain;

    // 위치 정보
    public int x, y; // 셀의 격자 위치

    private void Start()
    {
        focus.gameObject.SetActive(false);
        selectBtn.onClick.AddListener(() => puzzleMain.SelectPiece(gameObject));
    }

    public void SetFocus(bool isActive)
    {
        focus.gameObject.SetActive(isActive);
    }
}

Cell.cs

+ Recent posts