30초가 지나면 게임 끝내기
힌트요정 - 👻
더보기
[수정해야할 부분]
GameManager.cs의Update()부분만 수정하면 된답니다!
→ 어떤 조건이 되면 아래 코드가 실행되면 되겠죠!
endTxt.SetActive(true);
Time.timeScale = 0.0f;
결과
정답 코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public Card firstCard; // 첫 번째로 선택한 카드
public Card secondCard; // 두 번째로 선택한 카드
public Text timeTxt; // 게임 시간 표시
public GameObject endTxt; // 게임이 끝났을 때 뜨는 '끝'
public int cardCount = 0; // 현재 남아있는 카드의 개수
float time = 0.0f;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
}
void Start()
{
Application.targetFrameRate = 60; // 어떤 기기든지 초당 60프레임으로 렌더링 설정
Time.timeScale = 1.0f;
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
timeTxt.text = time.ToString("N2");
if(time > 30.0f)
{
Time.timeScale = 0.0f;
endTxt.SetActive(true); // Text 형으로 안받고 GameObject형으로 받아서 생략가능
}
}
// 카드 매치 기능
public void Matched()
{
// 같은 그림
if(firstCard.idx == secondCard.idx)
{
// 파괴해라.
firstCard.DestroyCard();
secondCard.DestroyCard();
cardCount -= 2;
if(cardCount == 0)
{
Time.timeScale = 0.0f;
endTxt.SetActive(true); // Text 형으로 안받고 GameObject형으로 받아서 생략가능
}
} // 다른 그림
else
{
// 닫아라.
firstCard.CloseCard();
secondCard.CloseCard();
}
// 파괴든, 닫는거든 정보들을 빼주어야 함
// 그래야 다음 카드 매치때 새로운 정보를 넣음
firstCard = null;
secondCard = null;
}
}
반응형
'스파르타 게임개발종합반(Unity) > 사전캠프 공부 기록' 카테고리의 다른 글
[Unity] 스플래시 이미지(앱을 켰을 때 떴다가 사라지는 이미지) 만들기 - Splash Image, Full Rect, Tight, Static, Dolly, Custom (0) | 2024.04.09 |
---|---|
[Unity/2D] 여러 이미지가 나오는 애니메이션 만들기 - Animation, Animator (0) | 2024.04.09 |
[Unity] 카드 판정 시스템 - Invoke, public, this, 싱글톤 (0) | 2024.04.09 |
[Unity/2D] 카드 뒤집기 - Button (0) | 2024.04.08 |
[Unity/2D] 코드를 통해 이미지 받아오기 - Resources, Resources.Load, 문자열 보간, $ (0) | 2024.04.08 |
댓글