๐23.12.21
์ค๋๋ถํฐ ๋ด์ผ๋ฐฐ์์บ ํ Unity 3๊ธฐ์ ๋ณธ ์บ ํ๊ฐ ์์๋์๋ค. ์ฌ์ ์บ ํ์ ๋ฌ๋ฆฌ ๋งค์ผ ์์นจ 9์๋ถํฐ ์คํ 9์๊น์ง ์ด 12์๊ฐ์ ๋ฌ๋ ค๊ฐ์ผ ํ๋ ๋ง๊ทธ๋๋ก ์คํ๋ฅดํ ์บ ํ์ด๋ค.
๊ฐ์ฅ ์ฒ์์ผ๋ก ์งํํ๋ ๊ฒ์ ์ฌ์ ์บ ํ 4์ฃผ์ฐจ์ ์๊ฐํ์๋ ๋ฅดํ์ด ์นด๋ ๋ค์ง๊ธฐ ๊ฒ์์ ์์ฉํ ํ์ ์๊ฐ ์นด๋ ๋ค์ง๊ธฐ ๊ฒ์ ์ ๋ง๋๋ ๊ฒ์ด๋ค. ์ค๋(๋ชฉ์์ผ)๋ถํฐ ์งํํ์ฌ ๋ค์์ฃผ ๋ชฉ์์ผ์ ๋ฐํํ๋, ์ผ์ฃผ์ผ๋์ ์งํํ๋ ๋ฏธ๋ ํ๋ก์ ํธ์ด๋ค.
์ผ๋จ ์ค๋์ ํ์๋ค๊ณผ ์ธ์ฌ๋ฅผ ๋๋๊ณ ํ์ฃผ๋์ ์ด๋ค ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์ง ๊ณํ์ ์๋ฆฝํ์๋ค. ํ์์ ์ผ๋ก ์ถ๊ฐํด์ผํ๋ ๊ธฐ๋ฅ๋ค๊ณผ ๋์ ํ ๋งํ ๊ณผ์ ๋ค์ ๋ชจ๋ ํฌํจํ์ฌ ๊ณํ์ ์ธ์ ๊ณ ๋ด์ผ๋ถํฐ ๋ณธ๊ฒฉ์ ์ผ๋ก ๊ฐ๋ฐ ์์ ์ ํ๊ธฐ๋ก ํ์๋ค. ๋ํ, ๋ฐฐ๊ฒฝ ์์ ๊ณผ ํ์ ์ด๋ฏธ์ง๋ก ์ธ ์ฌ์ง๊น์ง ๋ชจ๋ ์์งํด๋์๋ค.
๋๋ ์ถ๊ฐ ๊ธฐ๋ฅ ์ค '๋งค์นญ ์ฑ๊ณต ์, ํ์์ ์ด๋ฆ ํ์/์คํจ ์ ์คํจ ํ์' ๊ธฐ๋ฅ์ ๋งก์์ ์งํํ๊ฒ ๋์๋ค.
๋งค์ปค๋์ฆ
- ์ ์ ํ ๊ณณ์ ๋น text UI๋ฅผ ๋ง๋ ๋ค.
- ๋งค์นญ ์ text UI์ text๋ฅผ ๋ฐ๊พธ์ด์ค๋ค.
ํด๋น ๋ฐฉ์์ผ๋ก ์งํํ์๋๋ ์ ๋๋ก ๊ตฌํ๋์๋ค.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Linq;
public class GameManager : MonoBehaviour
{
public Text timeTxt;
float time;
public Text matchTxt;
public GameObject card;
public GameObject firstCard;
public GameObject secondCard;
public GameObject endTxt;
public AudioClip match;
public AudioSource audioSource;
public static GameManager I;
void Awake()
{
I = this;
}
// Start is called before the first frame update
void Start()
{
Time.timeScale = 1.0f;
int[] rtans = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 };
//OrderBy ์ ๋ ฌํ๊ฒ ๋ค Random.Range ๋๋คํ ์์๋ก ToArray() ๋ฐฐ์ด๋ก ๋ง๋ ๋ค
rtans = rtans.OrderBy(item => Random.Range(-1.0f, 1.0f)).ToArray();
for (int i = 0; i < 16; i++)
{
GameObject newCard = Instantiate(card);
//newCard๋ฅผ cards ๋ฐ์ผ๋ก ์ฎ๊ฒจ์ค
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.GetComponent<card>().cardName = rtanName;
newCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(rtanName);
}
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
timeTxt.text = time.ToString("N2");
if(time > 30.0f)
{
GameEnd();
}
}
public void isMatched()
{
//firstCard์ secondCard ๊ฐ์์ง ํ๋จ
string firstCardImage = firstCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite.name;
string secondCardImage = secondCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite.name;
if(firstCardImage == secondCardImage)
{
//๋งค์นญ ์ฑ๊ณต -> ์์ ์
audioSource.PlayOneShot(match);
matchTxt.text = firstCard.GetComponent<card>().cardName;
firstCard.GetComponent<card>().destroyCard();
secondCard.GetComponent<card>().destroyCard();
int cardsLeft = GameObject.Find("cards").transform.childCount;
if(cardsLeft == 2)
{
//์ข
๋ฃ์ํค์!!
GameEnd();
}
} else
{
//๋ฎ์
matchTxt.text = "์คํจ!";
firstCard.GetComponent<card>().closeCard();
secondCard.GetComponent<card>().closeCard();
}
firstCard = null;
secondCard = null;
}
void GameEnd()
{
endTxt.SetActive(true);
Time.timeScale = 0f;
}
public void retryGame()
{
SceneManager.LoadScene("MainScene");
}
}
ํ์๋ค ๋๋ค์๊ฐ ๊น์ ์ต์ํ์ง ์๊ณ , ์์ง ๊น์ ๋ง๋ค์ง ์์ผ์ ๋ถ๋ ๊ฒ์ ์ ๋ด์ผ ๐๊นํ๋ธ ํน๊ฐ์ ์๊ฐํ ํ ๊ฐ๋ฐํ ์์ ์ด๋ค. ๋ด์ผ์ด ๊ธฐ๋๋๋ค.
+) ์ด ๋ถ๋ถ์ ๋ง์น๊ณ ๋๋ ์ค๋์ ํ ํ๋ก์ ํธ๋ฅผ ๋ ์งํํ๊ธฐ ํ๋ ์ํฉ์ด๋ผ ๋จ์ ์๊ฐ์ ์งํํ๋ '์ดํ๋ ์ด์ ๋ณด๋ค' ๋ฆฌ๋ด์ผ์ ์กฐ๊ธ ์งํํ์๋ค. GameManager์ ์์ ํ๋ค๋ณด๋ ๊ฐ์ฅ ๊ฑฐ์ฌ๋ ท๋ ๊ณ ์ฉ ๊ธฐ๋ฅ ์ต์ ํ ์์ ์ ์งํํ์๋ค. ํด๋น ๋ถ๋ถ์ ๋ฆฌ๋ด์ผ ํฌ์คํธ๋ก ๋ฐ๋ก ์์ฑํ์ฌ ์ ๋ก๋ํ ์์ ์ด๋ค.
๊ทธ๋ฐ๋ฐ ์ด ๊ณผ์ ์์ ๊น๋จน๊ณ ์๋ ์ค์ํ ์ฌ์ค์ ๋ค์ ๋ฐฐ์ ๋ค.
Unity์์ try-catch๋ฌธ์์ Exception e๋ฅผ ์ฌ์ฉํ ๋์๋ ํ์์ ์ผ๋ก
using System;
์ ์ถ๊ฐํด์ผ ํ๋ค๋ ๊ฒ์ด๋ค. Unity์์ C# ๋ฌธ์ ์์ฑ ์์ ๋ฌธ์ ๊ธฐ๋ณธ ํ ํ๋ฆฟ์๋ ํด๋น ๋ค์์คํ์ด์ค๊ฐ ์ถ๊ฐ๋์ด ์์ง ์์ผ๋ฏ๋ก ์ฃผ์ํด์ผ ํ๋ค.