[2D Sparta-Unity] 4์ฃผ์ฐจ FindRtan โญโญ
์นดํ ๊ณ ๋ฆฌ: Sparta Unity
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
๋๊ธ๋จ๊ธฐ๊ธฐ