728x90
1. CoinGenerator 스크립트 수정
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinGenerator : MonoBehaviour
{
// 시작 시간
public float genStartTime;
// 생성 주기
public float genDelayTime;
// 아이템 프리팹 참조 변수
public GameObject coinPrefab;
// Start is called before the first frame update
void Start()
{
// 위치 정보를 저장한 벡터 생성 (x:0, y:2, z:0)
//Vector3 tempPosition = new Vector3(0f, 2f, 0f);
// 프리팹을 게임오브젝트로 생성하는 메소드 : Instantiate()
// -> Object.Instantiate(프리팹참조변수, 생성위치, 생성시회전);
//Instantiate(itemPrefab, tempPosition, Quaternion.identity);
//for (int i = 0; i < 10; i++)
//{
// // 랜덤한 x 좌표값 추출
// float x = Random.Range(-6f, 6f);
// // 랜덤한 x 좌표값 추출
// float y = Random.Range(-4.3f, 4.3f);
// Vector3 randPos = new Vector3(x, y, 0f);
// // 프리팹을 게임오브젝트로 생성하는 메소드 : Instantiate()
// // -> Object.Instantiate(프리팹참조변수, 생성위치, 생성시회전);
// Instantiate(itemPrefab, randPos, Quaternion.identity);
//}
// Timer 메소드
// -> 일정 시간 간격으로 실행되는 메소드
// Timer 메소드를 지정
// -> Invoke : 일정 시간 뒤에 지정한 메소드를 실행
// -> InvokeRepeating : 일정 시간 간격으로 지정한 메소드를 실행
// InvokeRepeating(타이머메소드명, 시작시간, 실행주기)
InvokeRepeating("CreateCoin", genStartTime, genDelayTime);
}
void CreateCoin()
{
// 랜덤한 x 좌표값 추출
float x = Random.Range(-6f, 6f);
// 랜덤한 x 좌표값 추출
float y = Random.Range(-4.3f, 4.3f);
Vector3 randPos = new Vector3(x, y, 0f);
// 프리팹을 게임오브젝트로 생성하는 메소드 : Instantiate()
// -> Object.Instantiate(프리팹참조변수, 생성위치, 생성시회전);
Instantiate(coinPrefab, randPos, Quaternion.identity);
}
// Update is called once per frame
void Update()
{
}
}
2. GameManager에 시작시간/끝시간 지정
3. C# Script 생성 -> 이름바꾸기 GameManager (톱니바퀴로 바뀜)
GameManager에 드레그&드롭 -> CoinCountText 드레그&드롭
Game Play Time 10으로 설정
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 씬 관리 사용
using UnityEngine.SceneManagement;
// UI 사용
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
// 아이템 갯수를 표시하는 카운트 텍스트
public Text coinCountText;
// 아이템 카운트 static 변수 생성
public static int coinCount = 0;
// 게임 시간
public float gamePlayTime;
// Start is called before the first frame update
void Start()
{
// 게임 진행 시간이 종료되면 OnGameOver 메소드 실행
Invoke("OnGameOver", gamePlayTime);
}
void OnGameOver()
{
// SceneManager.LoadScene("씬이름");
// -> 지정한 이름의 씬으로 이동
// EndScene으로 씬 전환
SceneManager.LoadScene("EndScene");
}
private void Update()
{
// Text 컴포넌트의 text 속성값을 정수값으로 변환
// itemCount로 저장
coinCount = int.Parse(coinCountText.text);
}
}
4. 게임오버 씬 추가
Create -> Scence -> 이름바꾸기 EndScence -> UI Canvas 생성 -> UI Text 하위에 3개 추가 -> 이름바꾸기 GameOverText/CoinCountText/RestartMsgText
위치/Text/Font Size/Color 원하는데로 지정
기존 SampleScene -> 이름 바꾸기 GameScene
5. Create Empty -> 이름바꾸기 EndManager
C# Script 생성 -> 이름바꾸기 EndManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class EndManager : MonoBehaviour
{
public Text coinCountText;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// static 변수인 itemCount값을 문자열로 변환해서
// Text에 표시함
coinCountText.text = GameManager.coinCount.ToString();
//Input.GetKey(KeyCode.Space);
bool isKeyDown = Input.GetKeyDown(KeyCode.Space);
//if (isKeyDown == true)
if (isKeyDown) // if (조건식의결과값)
{
GameManager.coinCount = 0;
SceneManager.LoadScene("GameScene");
}
}
}
6. EndManager에 스크립트 드레그&드롭 ->CoinCountText 드레그&드롭
7. 게임 시간 제한 기능 추가
C# Script 생성 -> 이름바꾸기 TimeDestory
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 지정된 시간뒤에 현재 컴포넌트를 가진 게임오브젝틀 파괴함
public class TimeDestory : MonoBehaviour
{
// 파괴 지연 시간 속성
public float destroyTime;
// Start is called before the first frame update
void Start()
{
// Invoke : 특정 지연시간 뒤에 특정 메소드를 실행할 때 사용
// Invoke(지연메소드이름, 지연시간);
Invoke("OnTimeDestroy", destroyTime);
}
// 파괴 타임 이벤트 메소드
void OnTimeDestroy()
{
// 게임오브젝트 파괴 static 메소드
// -> Destroy(게임오브젝트참조);
Destroy(gameObject);
}
// Update is called once per frame
void Update()
{
}
}
8. Build Settings에 씬 추가하기
9. 게임을 실행해보자!
코인이 랜덤한 장소에서 시간간격을 두고 생성되고
코인을 먹으면 점수가 올라가고
종료시간이 되면 EndScene으로 전환
스페이스바를 누르면 다시 GameScene으로 전환되는 것을 볼 수 있다.
(코인이 영역 밖에서 생성되는 건 위치 수정이 필요함...;)
'Unity > 코인먹기 게임' 카테고리의 다른 글
[유니티] 랜덤한 아이템 생성하기 (0) | 2021.04.27 |
---|---|
[유니티] 코인 prefab 만들기 (0) | 2021.04.25 |
[유니티] 캐릭터가 화면을 넘어가지 않도록 만들기 (0) | 2021.04.25 |
[유니티] 2D 코인 먹기 게임 만들기 (0) | 2021.04.24 |
댓글