나만의 것으로 만들기
Coddy의 Unity 게임: 벽돌 깨기 코스 레슨. 19개 중 19번째.
마지막 기능은 게임이 바뀌어도 유지되는 최고 점수입니다. PlayerPrefs는 플레이어 기기에 작은 값을 저장합니다. PlayerPrefs.GetInt(key, 0)는 값을 읽으며, 아직 저장된 값이 없으면 0을 읽고, PlayerPrefs.SetInt(key, value)는 값을 저장합니다. 다시 플레이하면 장면이 다시 로드되고, 새 GameManager가 최고 점수를 다시 불러옵니다.
이제 게임을 원하는 대로 조정할 수 있습니다. 게임 플레이 방식을 결정하는 모든 숫자는 코드에 시작 값이 지정된 필드이며, 플레이어의 Inspector는 읽기 전용이므로 다음 값은 코드에서 변경하세요.
Ball:speed(9) 및minVertical(0.3).Paddle:speed(14),wideFactor(1.6) 및wideSeconds(8).GameManager:lives(3) 및dropChance(0.15).
챌린지
어려움게임 사이에 최고 점수를 유지한 다음 게임을 조정하세요.
GameManager에서public TMP_Text bestText;와int best;를 추가하고, Start()를best = PlayerPrefs.GetInt("BestScore", 0);로 시작하세요.- ShowHud()에서
bestText.text = "Best: " + best;도 설정하세요. - 마지막 목숨을 잃을 때
score > best이면 best를 설정하고,PlayerPrefs.SetInt("BestScore", best);를 실행한 뒤 ShowHud()를 호출하세요. - 코드에서 선언된 속도, 목숨 수, dropChance를 조정하세요.
‘플레이’를 눌러 최고 점수를 기록하세요. ‘다시 플레이’를 해도 점수가 유지됩니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public Brick brickPrefab;
public Transform wall;
public string[] levels;
public float spacingX = 1.75f;
public float spacingY = 0.75f;
public float topY = 6f;
public Ball ball;
public int lives = 3;
public TMP_Text scoreText;
public TMP_Text livesText;
public GameObject gameOverPanel;
public GameObject[] powerUps;
public float dropChance = 0.15f;
int score;
int level;
int bricksLeft;
List<Ball> balls = new List<Ball>();
void Awake()
{
Instance = this;
}
void Start()
{
balls.Add(ball);
BuildLevel(0);
ShowHud();
}
void BuildLevel(int index)
{
level = index;
string[] rows = levels[index].Split('/');
for (int row = 0; row < rows.Length; row++)
{
string line = rows[row];
float left = -(line.Length - 1) * spacingX / 2f;
for (int col = 0; col < line.Length; col++)
{
char c = line[col];
if (c == '.') continue;
Vector3 pos = new Vector3(left + col * spacingX, topY - row * spacingY, 0f);
Brick brick = Instantiate(brickPrefab, pos, Quaternion.identity, wall);
brick.SetHits(c - '0');
bricksLeft++;
}
}
}
public void BrickBroken(Brick brick)
{
score += brick.points;
bricksLeft--;
ShowHud();
if (Random.value < dropChance)
{
GameObject prefab = powerUps[Random.Range(0, powerUps.Length)];
Instantiate(prefab, brick.transform.position, Quaternion.identity);
}
if (bricksLeft == 0)
{
balls[0].ResetToPaddle();
BuildLevel((level + 1) % levels.Length);
}
}
public void BallLost(Ball lost)
{
if (balls.Count > 1)
{
balls.Remove(lost);
Destroy(lost.gameObject);
return;
}
lives--;
ShowHud();
if (lives == 0)
{
gameOverPanel.SetActive(true);
lost.gameObject.SetActive(false);
}
else lost.ResetToPaddle();
}
public void SplitBall()
{
Ball source = balls[0];
Ball left = Instantiate(source, source.transform.position, Quaternion.identity);
left.Launch(new Vector3(-0.6f, 1f, 0f));
balls.Add(left);
Ball right = Instantiate(source, source.transform.position, Quaternion.identity);
right.Launch(new Vector3(0.6f, 1f, 0f));
balls.Add(right);
}
public void PlayAgain()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
void ShowHud()
{
scoreText.text = "Score: " + score;
livesText.text = "Lives: " + lives;
}
}
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
Unity 게임: 벽돌 깨기의 모든 레슨
직접 연습해 보세요: Unity 플레이그라운드