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

Unity中PlayerPrefs在Android设备上无法显示高分问题求助

Troubleshooting PlayerPrefs High Score Not Showing on Android

Hey there, let's work through this frustrating issue—when PlayerPrefs works perfectly in the Unity editor but breaks on Android, it's almost always tied to subtle platform-specific storage behaviors or easy-to-miss code oversights. Here are the most actionable fixes to try:

1. Always Call PlayerPrefs.Save() After Updating Scores

The Unity editor auto-saves PlayerPrefs more aggressively, but Android's file system buffers writes. If you skip explicitly calling PlayerPrefs.Save() right after setting the high score, the change might never persist before the app closes or goes to the background.

Fix this in your code with:

if (currentScore > PlayerPrefs.GetInt("HighScore", 0))
{
    PlayerPrefs.SetInt("HighScore", currentScore);
    PlayerPrefs.Save(); // Critical for Android persistence!
}

2. Double-Check Key Case Sensitivity

PlayerPrefs keys are case-sensitive on Android, even if the editor lets you get away with mismatched cases. Make sure every save/load call uses the exact same key—no switching between "HighScore", "highscore", or "Highscore".

3. Clear Corrupted Device Data

Old cached PlayerPrefs on your phone might be causing conflicts. Go to your device's Settings > Apps > [Your App] > Storage > Clear Data, then re-install the app and test from scratch. This eliminates any corrupted pre-existing data that could be overriding your new scores.

4. Watch for Accidental Resets

Scan your code for unintended calls to PlayerPrefs.DeleteAll() or PlayerPrefs.DeleteKey("HighScore") that might only trigger on Android. For example, a misplaced platform conditional like this would wipe your scores on device only:

#if UNITY_ANDROID
PlayerPrefs.DeleteAll(); // Oops! This wipes all saved data
#endif

5. Debug with Logcat to Track Values

Make a debug build and add log statements to see exactly what's happening when loading/saving:

// When loading the high score
int loadedHighScore = PlayerPrefs.GetInt("HighScore", -1);
Debug.Log($"Loaded High Score: {loadedHighScore}");

// When saving a new high score
PlayerPrefs.SetInt("HighScore", newHighScore);
PlayerPrefs.Save();
Debug.Log($"Saved New High Score: {newHighScore}");

Connect your device to Unity or use Android Studio's Logcat to check these logs. If you see -1 (the default value) when you expect a saved score, that means the key isn't being persisted correctly.

6. Verify Android Player Settings

Head to Edit > Project Settings > Player > Android > Other Settings:

  • Ensure "Write Permission" is set to either "Internal Only" or "External (SDCard)"—if using external storage, confirm the device has an accessible SD card.
  • For Android 10 (API 29) and below, check that the WRITE_EXTERNAL_STORAGE permission is enabled (Unity usually adds this automatically, but double-check if you've modified the AndroidManifest.xml).

7. Initialize the High Score on First Launch

Sometimes the key might not exist on the first run, leading to unexpected defaults. Use PlayerPrefs.HasKey() to ensure the key is properly initialized:

int highScore;
if (PlayerPrefs.HasKey("HighScore"))
{
    highScore = PlayerPrefs.GetInt("HighScore");
}
else
{
    highScore = 0;
    PlayerPrefs.SetInt("HighScore", 0);
    PlayerPrefs.Save();
}

This guarantees the key exists from the app's first launch, avoiding issues where Android might not write the initial value without an explicit save.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:25:47