How to store game progress using PlayerPrefs in Unity

PlayerPrefs in Unity allow us to store and access player preferences in the game, including game progress. In this article, we'll learn how to use PlayerPrefs to store game progress, with a complete code example.

Implementation principle

PlayerPrefs is a key-value storage method in Unity that saves player preferences on the local hard drive. By using PlayerPrefs, we can store and read data between different scenes of the application.

When storing game progress, we can use the PlayerPrefs.SetInt() method to store integer values ​​in PlayerPrefs. For example, we could store the number of the current level in PlayerPrefs so that the next time we play, we can load the level the player last left off.

code example

Here's a complete code example of using PlayerPrefs to store game progress:

// Store game progress
int currentLevel = 4; // Current level
PlayerPrefs.SetInt("CurrentLevel", currentLevel);

// Read game progress
int savedLevel = PlayerPrefs.GetInt("CurrentLevel", 1); // If there is no previous level Once stored, it defaults to 1
Debug.Log("Current level: " + savedLevel);


In the code example above, we first store the current level's number in PlayerPrefs using the PlayerPrefs.SetInt() method. Then, we use the PlayerPrefs.GetInt() method to read the stored game progress and print it out.

advantage

  • Easy to use: PlayerPrefs is very easy to use and can easily store data on your local hard drive.
  • Cross-scene storage: By using PlayerPrefs, we can store and read data between different scenes of the application.
  • Cross-platform support: PlayerPrefs is cross-platform and can be used on Windows, Mac and mobile devices.

shortcoming

  • Not suitable for large amounts of data: PlayerPrefs are suitable for storing small amounts of data, but not suitable for storing large amounts of data. If you need to store large amounts of data, you will need to use other storage methods.
  • Unsafe: Because PlayerPrefs are stored on the local hard drive, there is a risk of malicious access or tampering. Therefore, it is not recommended to store sensitive information in PlayerPrefs.

Other preservation options

PlayerPrefs is a useful storage method in Unity, but if you need to store a large amount of data or require higher security, you can consider using other storage methods, such as local files or databases. Here is a complete code example of using a local file to store game progress:

using System.IO;
using UnityEngine;

public class GameSaveManager : MonoBehaviour
{
private string savePath;

private void Awake()
{
savePath = Application.persistentDataPath + "/gameSave.dat";
}

public void SaveGame(int currentLevel)
{
BinaryWriter writer = new BinaryWriter(File.Open(savePath, FileMode.Create));
writer.Write(currentLevel);
writer.Close();
}

public int LoadGame()
{
if (!File.Exists(savePath))
{
return 1;
}

BinaryReader reader = new BinaryReader(File.Open(savePath, FileMode.Open));
int currentLevel = reader.ReadInt32();
reader.Close();

return currentLevel;
}
}


In the above code example, we define a GameSaveManager class that uses BinaryWriter and BinaryReader to store game progress in a local file. We can use the GameSaveManager.SaveGame() method to store game progress and use the GameSaveManager.LoadGame() method to load game progress.

It should be noted that local file storage also has its disadvantages, such as not supporting cross-platform storage. Therefore, when choosing a storage method, you need to make a choice based on specific circumstances.

using UnityEngine;

public class GameSaveManager : MonoBehaviour
{ private void Start() { // Store game progress int currentLevel = 4; // Current level PlayerPrefs.SetInt("CurrentLevel", currentLevel); // Read game progress int savedLevel = PlayerPrefs .GetInt("CurrentLevel", 1); // If it has not been saved before, it defaults to 1 Debug.Log("Current level: " + savedLevel); // Use local files to store game progress GameSaveManager gameSaveManager = new GameSaveManager() ; gameSaveManager.SaveGame(currentLevel); int loadedLevel = gameSaveManager.LoadGame(); Debug.Log("Current level: " + loadedLevel); } } public class GameSaveManager { private string savePath;






















public GameSaveManager()
{
savePath = Application.persistentDataPath + "/gameSave.dat";
}

public void SaveGame(int currentLevel)
{
BinaryWriter writer = new BinaryWriter(File.Open(savePath, FileMode.Create));
writer.Write(currentLevel);
writer.Close();
}

public int LoadGame()
{
if (!File.Exists(savePath))
{
return 1;
}

BinaryReader reader = new BinaryReader(File.Open(savePath, FileMode.Open));
int currentLevel = reader.ReadInt32();
reader.Close();

return currentLevel;
}
}

9489f24322247d55613a3e5e63f10e6e.jpeg


Guess you like

Origin blog.csdn.net/shguxudong11/article/details/129536197