[2D Sparta-Unity] 4์ฃผ์ฐจ FindRtan โญโญ

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , ,




4์ฃผ์ฐจ FindRtan ๊ฐ™์€๊ทธ๋ฆผ ์ฐพ๊ธฐ

๋กœ์ง ์ค‘์š”, ๊ฒŒ์ž„๋งค๋‹ˆ์ €โญ

4์ฃผ์ฐจ FindRtan






1. card

  • ์นด๋ฉ”๋ผ ์‚ฌ์ด์ฆˆ ์กฐ์ •
  • ์นด๋“œ ๋’ค์ง‘๊ธฐ, ์—†์• ๊ธฐ ๋“ฑ๋“ฑ
  • ์ด๋ฏธ์ง€ pixels per unit ์œผ๋กœ ์‚ฌ์ด์ฆˆ ์กฐ์ •

card.cs

card.cs

using UnityEngine;

public class card : MonoBehaviour
{
    public Animator anim;

    public void openCard()
    {
        anim.SetBool("isOpen", true);
        transform.Find("front").gameObject.SetActive(true);
        transform.Find("back").gameObject.SetActive(false);

        if (gamemanager.I.firstCard == null)
        {
            gamemanager.I.firstCard = gameObject;
        }else
        {
            gamemanager.I.secondCard = gameObject;
            gamemanager.I.isMatched();
        }
    }

    public void destroyCard()
    {
        Invoke("destroyCardInvoke", 0.5f);
    }

    void destroyCardInvoke()
    {
        Destroy(gameObject);
    }

    public void closeCard()
    {
        Invoke("closeCardInvoke", 0.5f);
    }

    void closeCardInvoke()
    {
        anim.SetBool("isOpen", false);
        transform.Find("back").gameObject.SetActive(true);
        transform.Find("front").gameObject.SetActive(false);
    }
}








2. GameManager

  • list ์‚ฌ์šฉ, list ๋žœ๋ค ์ •๋ ฌ OrderBy(item => Random.Range(-1.0f, 1.0f)).ToArray();
  • for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ์นด๋“œ ์•Œ๋งž๊ฒŒ ๋ฐฐ์น˜
  • ์นด๋“œ ๋ชจ๋‘์™„์„ฑ or 30์ดˆ ํ›„ ๊ฒŒ์ž„์ข…๋ฃŒ
  • card.cs์—์„œ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉ GetComponent().destroyCard();

GameManager.cs

GameManager.cs

using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class gamemanager : MonoBehaviour
{
    
    public Text timeTxt;
    public GameObject endTxt;
    public GameObject card;
    float time = 0.0f;
    public GameObject firstCard;
    public GameObject secondCard;

    public static gamemanager I;

    void Awake()
    {
        I = this;
    }

    void Start() {
        Time.timeScale = 1.0f;

        int[] rtans = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 };
        //๋žœ๋ค์ •๋ ฌ
        rtans = rtans.OrderBy(item => Random.Range(-1.0f, 1.0f)).ToArray();
            
        for (int i = 0; i < 16; i++) // for๋ฌธ์œผ๋กœ ์นด๋“œ๋ฐฐ์น˜ 
        {
            
            GameObject newCard = Instantiate(card);
            newCard.transform.parent = GameObject.Find("cards").transform;

            float x = (i / 4) * 1.4f - 2.1f;
            float y = (i % 4) * 1.4f - 3.0f;
            newCard.transform.position = new Vector3(x, y, 0);

            string rtanName = "rtan" + rtans[i].ToString();
            newCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(rtanName);
        }
    }

    void Update()
    {
        time += Time.deltaTime;
        timeTxt.text = time.ToString("N2");
        if (time>=30)
        {
            Invoke("GameEnd",0);
        }
    }
    
    public void isMatched()
    {
        string firstCardImage = firstCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite.name;
        string secondCardImage = secondCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite.name;

        if (firstCardImage == secondCardImage)
        {
            firstCard.GetComponent<card>().destroyCard();
            secondCard.GetComponent<card>().destroyCard();

            int cardsLeft = GameObject.Find("cards").transform.childCount;
            if (cardsLeft == 2)
            {
                endTxt.SetActive(true);
                Time.timeScale = 0.0f;
                //Invoke("GameEnd",1f);
            }
        }
        else
        {
            firstCard.GetComponent<card>().closeCard();
            secondCard.GetComponent<card>().closeCard();
        }

        firstCard = null;
        secondCard = null;
    }

    void GameEnd(){
        endTxt.SetActive(true);
        Time.timeScale = 0.0f;
    }
}
  • Instantiate( ์ด๋ฏธ ๋งŒ๋“ค์–ด์ง„ ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋งŒ๋“ ๋‹ค๋Š” ์˜๋ฏธ)








3. ๊ฒŒ์ž„ ๋๋‚ด๊ธฐ

์ „ ๊ฐ•์˜๋“ค๊ณผ ๊ฐ™์ด

endtxt.cs

endtxt.cs

using UnityEngine;
using UnityEngine.SceneManagement;

public class endtxt : MonoBehaviour
{
    public void Regame(){
        SceneManager.LoadScene("MainScene");
    }
}
  • ์”ฌ ๋กœ๋“œ








4. ์ •๋ฆฌ

  • 4์ฃผ์ฐจ์—์„  ์ด์ „๊ฐ•์˜๋ฅผ ํฌํ•จํ•˜๊ณ  for๋ฌธ์„ ํ™œ์šฉํ•œ ๊ฒŒ์ž„.
  • rtans = rtans.OrderBy(item => Random.Range(-1.0f, 1.0f)).ToArray();
  • FIND, LOAD ์ž˜ ์‚ฌ์šฉํ•˜๊ธฐ
  • newCard.transform.Find(โ€œfrontโ€).GetComponent().sprite = Resources.Load(rtanName);
  • ์• ๋‹ˆ๋ฉ”์ด์…˜ ์–‘๋ฐฉํ–ฅ ํŠธ๋žœ์ง€์…˜ ํ™•์ธ BOOL๊ฐ’์œผ๋กœ ์Šคํฌ๋ฆฝํŠธ์—์„œ ์กฐ์ •




1ํšŒ 23/06/14
2ํšŒ 23/10/27 ๋ณต์Šต, ์ˆ˜์ •
[Unity] FindRtan

์ฐธ๊ณ  : ์œ ๋‹ˆํ‹ฐ TOP


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ