Deixe com a sua cara
Lição 16 de 16 do curso Jogo no Unity: Quebra-Tijolos da Coddy.
O último recurso é um recorde que permanece entre as partidas. PlayerPrefs mantém valores pequenos no dispositivo do jogador: PlayerPrefs.GetInt(key, 0) lê um, usando 0 quando nada foi salvo ainda, e PlayerPrefs.SetInt(key, value) salva um. Carregue o recorde quando o jogo começar e salve-o quando uma partida terminar com uma pontuação maior. Jogar novamente recarrega a cena, e o novo GameManager carrega o recorde outra vez.
Agora o jogo é seu para ajustar. Cada número que define como ele funciona é um campo com um valor inicial no código:
Ball:speed(9) eminVertical(0.3).Paddle:speed(14),wideFactor(1.6) ewideSeconds(8).GameManager:lives(3),dropChance(0.15) e o espaçamento entre os tijolos.
Os layouts são a lista levels do GameManager no Inspector: um dígito representa um tijolo com essa quantidade de acertos, um ponto representa um espaço vazio e / inicia a próxima linha, como em 1.3.1/22222. No seu próprio projeto Unity, altere os valores no Inspector: quando um script está em um objeto, o Inspector mantém sua própria cópia, e um novo valor inicial no código não chega mais até ele.
Desafio
Difícil- Em
GameManager, adicionepublic TMP_Text bestText(a cena vincula BestText) e umint best. EmStart(), carreguebestde PlayerPrefs usando a chaveBestScore, 0 quando nada estiver salvo.ShowHud()também mostraBest: 0embestText, com o recorde. - Quando o jogo terminar com uma pontuação maior que
best, armazene-a embest, salve-a emBestScoree mostre-a.
Depois, deixe o jogo com a sua cara: altere as velocidades, as vidas, a chance de queda e a força dos power-ups, e jogue até ficar do jeito certo.
Pressione Play, estabeleça um recorde e depois Check.
Experimente você mesmo
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;
}
}
Esta lição inclui um quiz rápido. Comece a lição para respondê-lo e acompanhar seu progresso.