You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中PlayerPrefs保存最高分及跨GameOver场景传递的问题排查

我来帮你搞定这两个问题——先修复当前脚本的最高分显示与保存bug,再聊聊GameOver场景的分数传递方案选择。

一、修复PickUp脚本的最高分问题

你的代码已经用到了PlayerPrefs来保存最高分,但有两个关键疏漏导致重启后不显示、UI不更新:

  1. 游戏启动时没有从PlayerPrefs加载已保存的最高分并初始化UI
  2. 只有在当前分数超过最高分的瞬间才更新文本,首次进入游戏或分数未超过时,最高分UI是空的

下面是修复后的完整代码,我标注了核心修改点:

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

public class PickUp : MonoBehaviour 
{
    public GameObject explosionEffect;
    public Rigidbody2D Clock;
    public AudioClip pickupSound;
    AudioSource audioSource;
    public Text Score;
    public Text highscore;
    int Amount = 0;
    public float SpeedUp = 1;

    void Start() 
    {
        audioSource = GetComponent<AudioSource>();
        // 新增:启动时加载保存的最高分并更新UI
        LoadSavedHighscore();
    }

    void Update() { }

    private void OnTriggerEnter2D(Collider2D other) 
    {
        if (other.tag == "Clock") 
        {
            Destroy(Instantiate(explosionEffect.gameObject, transform.position, transform.rotation), 5f);
            audioSource.PlayOneShot(pickupSound, 1F);
            Amount++;
            Score.text = Amount.ToString() + " Clocks";

            // 优化最高分更新逻辑
            int currentSavedHighscore = PlayerPrefs.GetInt("Highscore", 0);
            if (Amount > currentSavedHighscore) 
            {
                PlayerPrefs.SetInt("Highscore", Amount);
                PlayerPrefs.Save();
                highscore.text = "Highscore: " + Amount.ToString();
                Debug.Log("New highscore saved!");
            }

            // 时钟加速逻辑保留
            if (Clock.velocity.y >= -30) 
            {
                Clock.velocity += Vector2.down * SpeedUp;
                Debug.Log("Speed is now " + Clock.velocity);
            }
        }
    }

    // 新增:专门的加载最高分方法
    void LoadSavedHighscore()
    {
        int savedScore = PlayerPrefs.GetInt("Highscore", 0);
        highscore.text = "Highscore: " + savedScore.ToString();
    }
}

修改点说明:

  • 在Start()里调用LoadSavedHighscore(),确保游戏一启动就显示已保存的最高分
  • 把获取当前最高分的操作抽出来,避免重复调用PlayerPrefs.GetInt,逻辑更清晰
  • 确保每次保存新最高分后立即更新UI,同时解决了首次进入游戏时最高分UI为空的问题
二、GameOver场景传递最高分:PlayerPrefs vs JSON方案对比

这两个方案各有适用场景,我帮你梳理清楚:

1. PlayerPrefs(推荐当前场景使用)

  • 优点:
    • 实现超简单,不需要额外的序列化/反序列化代码
    • 跨场景直接读取,不需要手动传递数据
    • 自动持久化到本地,重启游戏后依然存在
  • 缺点:
    • 只适合存储简单的键值对(整数、字符串、浮点数),如果以后要保存复杂玩家数据(比如关卡进度、多维度统计)就不够用了
  • GameOver场景使用示例:
    在GameOver场景的UI脚本里直接读取:
    public Text gameOverHighscoreText;
    
    void Start()
    {
        int highscore = PlayerPrefs.GetInt("Highscore", 0);
        gameOverHighscoreText.text = "Best Score: " + highscore.ToString();
    }
    

2. JSON方案(适合未来扩展复杂数据)

  • 优点:
    • 可以存储结构化的复杂数据,比如包含最高分、总游戏次数、最快通关时间的玩家数据类
    • 数据组织更规范,便于后续功能扩展
  • 缺点:
    • 需要写序列化/反序列化代码,比PlayerPrefs繁琐
    • 需要手动处理文件读写(依赖Unity的JsonUtility或第三方库)
  • 简单示例:
    先定义一个玩家数据类:
    [System.Serializable]
    public class PlayerData
    {
        public int highscore;
        public int totalGamesPlayed;
    }
    
    在PickUp脚本中保存数据:
    // 当更新最高分的时候同时保存结构化数据
    PlayerData data = new PlayerData();
    data.highscore = Amount;
    data.totalGamesPlayed = PlayerPrefs.GetInt("TotalGames", 0) + 1;
    string json = JsonUtility.ToJson(data);
    System.IO.File.WriteAllText(Application.persistentDataPath + "/playerData.json", json);
    
    在GameOver场景读取:
    public Text gameOverHighscoreText;
    
    void Start()
    {
        string path = Application.persistentDataPath + "/playerData.json";
        if (System.IO.File.Exists(path))
        {
            string json = System.IO.File.ReadAllText(path);
            PlayerData data = JsonUtility.FromJson<PlayerData>(json);
            gameOverHighscoreText.text = "Best Score: " + data.highscore.ToString();
        }
    }
    

总结建议

如果你的游戏目前只需要保存最高分这一项简单数据,直接用PlayerPrefs就足够了,快速又省心。如果以后计划添加更多玩家统计数据,再考虑切换到JSON方案。

内容的提问来源于stack exchange,提问作者ReignBeaux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:29:37