Unity 내배캠 TIL

Unity 입문 게임 개발 - 팀프로젝트(5주차5일)

오늘도즐겨 2024. 10. 22. 18:00

🔥 10/21 월요일 목표 🔥

🔎 베스트 스코어 저장하기

🔎 남은 프로젝트 진행 및 PPT완성하기

 

 10:00 팀프로젝트진행 

 12:00 수준별 꾸준 실습 

 16:00 수준별 꾸준 실습 UI특강

 

GameOverPanal도 위의 Text와  동일하게 세팅해주기

 

UIManager Script작성

public class UIManager : MonoBehaviour
{
    public static UIManager instance;
    [SerializeField] private GameObject gameOverCanvas;
    [SerializeField] private TMP_Text curTxt;
    [SerializeField] private TMP_Text bestTxt;
    [SerializeField] private TMP_Text gameOverPanalcurTxt;
    [SerializeField] private TMP_Text gameOverPanalbestTxt;
    private int curScore;
    private int bestScore;

    public int CurScore
    {
        get { return curScore; }
        set { curScore = value; }
    }


    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            return;
        }

    }
    void Start()
    {
        gameOverCanvas.SetActive(false);
        curTxt.text = $"현재스코어 : {curScore}";
        bestScore = PlayerPrefs.GetInt("BestScore", 0);
        bestTxt.text = $"베스트스코어 : {bestScore}";
    }

    void Update()
    {
        AddScore();
    }

    void AddScore()
    {
        curTxt.text = $"현재스코어 : {curScore}";        

        if (curScore > bestScore)
        {
            bestScore = curScore;
            bestTxt.text = $"베스트스코어 : {bestScore}";
            PlayerPrefs.SetInt("BestScore", bestScore);
            PlayerPrefs.Save();
        }
        else
        {
            bestTxt.text = $"베스트스코어 : {bestScore}";
        }
    }
    public void GameOver()
    {
        gameOverCanvas.SetActive(true);
        gameOverPanalcurTxt.text = curTxt.text;
        gameOverPanalbestTxt.text = bestTxt.text;
        Time.timeScale = 0;
    }
    public void PressRestartBtn()
    {
        SceneManager.LoadScene("BrickOutGame");
    }
}

 

Ball Script에서 Ball이 벽이나, 플레이어에 닿았을경우

TouchSoundClip을 오디오소스에 넣어주고, 플레이 시켜주는 간단한 코드구현

OnCollisionEnter2D를 이용하여, 사운드재생

 

public AudioClip touchBlockClip;
private AudioSource touchedBlockSound;
    
private void Awake()
{    
    touchedBlockSound = GetComponent<AudioSource>();
}
 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.tag == "Wall")
     {         
         touchedBlockSound.clip = touchBlockClip;
         touchedBlockSound.Play();
     }
     else if (collision.gameObject.tag == "Player")
     {
         touchedBlockSound.clip = touchBlockClip;
         touchedBlockSound.Play();        
     }
 }

 

🔥 10/22 화요일 목표 🔥 

🔎 팀프로젝트 제출하기

🔎 팀 프로젝트 PPT 발표하기