Menu
CoddyTech

打造专属风格

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)。
challenge icon

挑战

困难

在不同游戏之间保留最高分,然后调整游戏。

  1. 在 GameManager 中,添加 public TMP_Text bestText; 和 int best;;Start() 开头写入 best = PlayerPrefs.GetInt("BestScore", 0);
  2. ShowHud() 还要设置 bestText.text = "Best: " + best;
  3. 当最后一条命即将失去,且满足 score > best 时,设置 best,调用 PlayerPrefs.SetInt("BestScore", best);,然后调用 ShowHud()。
  4. 在代码中声明速度、生命数和 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;
    }
}
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

Unity 游戏:打砖块中的所有课

自己练习: Unity 在线编辑器