반응형

하이 라키 뷰에서 마우스 오른쪽 버튼을 누르고 UI -> Text를 만듭니다 

3개를 만들어서 하나는 이름을 LiveText 하고 화면 오른쪽 위에 놓고 나머지 두 개는 GameOver, Success 라 하고 중앙에 놓습니다 

 

 

 

 

 

 

 

 

Scripts 폴더에 GameManager 를 만들고 스크립트를 작성합니다 

 

GameManager 스크립트 작성

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    //남은 생명력
    public int lives = 3;

    //벽돌갯수
    public int bricks = 32;

    //게임 재시작 시간
    public float resetDelay;


    public Text txtLives = null;
    public GameObject gameOver = null;
    public GameObject success = null;
    public GameObject bricksPrefab;
    public GameObject paddle = null;
    public GameObject DeathParticles = null;
    public static GameManager Instance = null;

    private GameObject clonePaddle = null;


    void Start()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else if (Instance != this)
        {
            Destroy(gameObject);
        }

        SetUp();
    }

    //게임 시작시 패들과 벽돌을 불러온다
    public void SetUp()
    {
        if (paddle != null)
        {
            clonePaddle = Instantiate(paddle, paddle.transform.position, Quaternion.identity) as GameObject;
        }

        if (bricksPrefab != null)
        {
            Instantiate(bricksPrefab, bricksPrefab.transform.position, Quaternion.identity);
        }
    }


    //게임 재시작 설정
    void CheckGamOver()
    {
        //벽돌을 깻을 때
        if (bricks < 1)
        {
            if (success != null)
            {
                success.SetActive(true);
                //시간을 2.5배 빠르게
                Time.timeScale = 2.5f;
                Invoke("Reset", resetDelay);
            }
        }

        //생명력을 소진했을때
        if (lives < 1)
        {
            if (gameOver != null)
            {
                gameOver.SetActive(true);
                //시간을 0.25배 느리게 
                Time.timeScale = 0.25f;
                Invoke("Reset", resetDelay);
            }
        }

    }

    private void Reset()
    {
        //평균타임을 설정합니다
        Time.timeScale = 1f;

        Application.LoadLevel(Application.loadedLevel);
    }

    //생명력을 잃게되면 발생
    public void LoseLife()
    {
        lives--;

        if (txtLives != null)
        {
            txtLives.text = "남은 생명 : " + lives;
        }

        //파티클 발생
        if (DeathParticles != null)
        {
            Instantiate(DeathParticles, clonePaddle.transform.position, Quaternion.identity);
        }

        //패들 없애기
        Destroy(clonePaddle.gameObject);

        //딜레이 시간 만큼 지나면 패들 생산
        Invoke("SetupPaddle", resetDelay);
        CheckGamOver();
    }

    //패들 생산 
    private void SetupPaddle()
    {
        clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject;
    }

    public void DestroyBrick()
    {
        bricks--;
        CheckGamOver();
    }

}

 

 

 

 

 

 

 

작성한 스크립트를 하이라키뷰에 GameManager 오브젝트를 만들 과 스크립트를 붙입니다

 

하이 라키 뷰에 오른쪽 마우스 버튼을 클릭하여 Effects ->Particle System을 생성시켜 이름을 hitParticle이라고 합니다 

그리고 아래 그림과 같이 속성을 바꿉니다

 

 

 

 

 

 

 

 만든  이팩트의 모습

 

이팩트를 프리 팹으로 만듭니다

 

Scripts 폴더에  BlocklCtrl 스크립트를 만듭니다

 

 

 

 

 

 

 

 

BlockCtrl 스크립트 작성

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BlockCtrl : MonoBehaviour
{

    public GameObject brickParticle ;

    //공과의 충동할때
    private void OnCollisionEnter(Collision other)
    {
        if (brickParticle != null)
        {
            //이팩트를  발생시킨다
            Instantiate(brickParticle, transform.position, Quaternion.identity);

            GameManager.Instance.DestroyBrick();
            Destroy(gameObject);
        }

    }
}

 

block 오브젝트 을 열고 각각 32개 블록 Cube에 스크립트를 붙이고 BlockCtrl 스크립트의 Brick Particle에 hitParticle을 붙입니다

 

 

 

 

 

 

 

하이 라키 뷰에 있는 프리 팹들을 Overrides 합니다 

 

Prefabs 폴더에 있는 hitParticle을 Ctrl + D를 눌러서 복사합니다 

그리고 이름을 DethParticle이라고 합니다 

 

DethParticle의 Material을 red로 바꿉니다 

 

하이 라키 뷰에 GameOver, Success 텍스쳐를 Disable(안 보이게)합니다 

 

Scripts폴더에 DeathZone 스크립트를 만들고 작성합니다 

 

 

 

 

 

 

 

DeathZone 스크립트 작성

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DeathZone : MonoBehaviour
{
    private void OnTriggerEnter()
    {
        GameManager.Instance.LoseLife();
    }
}

 

하이 라키 뷰에서  화면 맨 아래 있는 오브젝트인 Cube (3)을 선택하여 DeathZone 스크립트를 붙입니다 그리고 

is Trigger를 체크합니다 

 

 

하이 라키 뷰에서 GameManager 오브젝트를 선택하고 아래 그림과 같이 속성을 바꾸고 텍스쳐와 프리 팹을 연결합니다 

 

 

 

 

게임을 실행하여 정상으로 게임이 잘되는지 확인합니다 

 

 

작업 파일을 올려놓습니다 

참고자료가 조그 미나 마 도움이 되었으면 합니다 

부자 되시고 항상 복 받으세요 ㅎㅎ

BreakGame_Export3.unitypackage
0.06MB

 

반응형

+ Recent posts