• Hello
Search Results for

    Show / Hide Table of Contents

    Protected PlayerPrefs

    Unity PlayerPrefs are stored as plain text on the player's device. They are easy to find and edit. Protected PlayerPrefs can hide the name of an entry, encrypt its value, and notice when someone edited it by hand. To encrypt means to scramble the value so it cannot be read without a key.

    Protected PlayerPrefs are part of the Free and the Pro version.

    Walkthrough: Protecting saved data.

    Three ways to use them

    Way Use it when
    ProtectedPlayerPrefs You want a drop-in replacement. Saves stay in Unity's usual place
    ProtectedFileBasedPlayerPrefs You want the saves in a file you choose
    Inspector types, such as ProtectedIntPref You want to set the entry name and a default value in the Inspector

    All of them are in the namespace GUPS.AntiCheat.Protected.Storage.Prefs.

    ProtectedPlayerPrefs

    Replace PlayerPrefs with ProtectedPlayerPrefs. The method names stay the same.

    Before:

    PlayerPrefs.SetInt("player.score", 10);
    int score = PlayerPrefs.GetInt("player.score");
    

    After:

    using GUPS.AntiCheat.Protected.Storage.Prefs;
    
    ProtectedPlayerPrefs.SetInt("player.score", 10);
    int score = ProtectedPlayerPrefs.GetInt("player.score");
    

    HasKey, DeleteKey, and Save work as in Unity.

    It also stores more types than Unity's PlayerPrefs. A few examples:

    ProtectedPlayerPrefs.SetBool("sound.on", true);
    bool soundOn = ProtectedPlayerPrefs.GetBool("sound.on");
    
    ProtectedPlayerPrefs.SetVector3("player.position", new Vector3(1, 2, 3));
    Vector3 position = ProtectedPlayerPrefs.GetVector3("player.position");
    
    ProtectedPlayerPrefs.SetQuaternion("player.rotation", Quaternion.identity);
    Quaternion rotation = ProtectedPlayerPrefs.GetQuaternion("player.rotation");
    

    Supported types:

    Group Types
    Numbers Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal
    Text and flags String, Char, Boolean, ByteArray
    Unity types Vector2, Vector2Int, Vector3, Vector3Int, Vector4, Quaternion, Color, Color32, Rect, Plane, Ray, Matrix4x4

    ProtectedFileBasedPlayerPrefs

    It has the same methods as ProtectedPlayerPrefs. Only the save place is different. The default is the file playerprefs.dat in Application.persistentDataPath.

    Set FilePath before the first read or write to use another file:

    ProtectedFileBasedPlayerPrefs.FilePath = System.IO.Path.Combine(Application.persistentDataPath, "save.dat");
    
    ProtectedFileBasedPlayerPrefs.SetInt("player.score", 10);
    

    Inspector types

    Add a field of an Inspector type to your component. You then set the name of the entry and a default value in the Inspector. In code you read and write through Value.

    using GUPS.AntiCheat.Protected.Storage.Prefs;
    using UnityEngine;
    
    public class PlayerScore : MonoBehaviour
    {
        public ProtectedIntPref score;
    
        public void AddScore(int amount)
        {
            score.Value += amount;
        }
    }
    

    Protected PlayerPrefs set in the Inspector.

    The types are ProtectedBoolPref, ProtectedIntPref, ProtectedFloatPref, ProtectedStringPref, ProtectedVector2Pref, ProtectedVector3Pref, ProtectedVector4Pref, and ProtectedQuaternionPref.

    Settings

    Open Edit > Project Settings > GuardingPearSoftware > AntiCheat. Go to the Global tab and find PlayerPrefs - Settings. These settings apply to all three ways.

    The PlayerPrefs settings.

    • Hash Key (on by default): Stores the name of the entry as a hash, not as readable text. A hash is a short code made from the name.
    • Value Encryption Key (empty by default): The secret used to encrypt the value. While it is empty, the value is not encrypted. If you change the key later, old saves can no longer be read.
    • Allow Read Any Owner (on by default): On means any device can read the save, so players can share it. Off means only the device that wrote it can read it.
    • Verify Integrity (on by default): Stores a signature next to the value. If a player edits the save by hand, the signature no longer matches.
    Important

    Set a Value Encryption Key before you ship. The shipped settings leave it empty, so values stay readable until you do.

    Good to know

    Protected PlayerPrefs do not report to a detector. When a save fails a check, the Get call throws an exception. That happens when:

    • the save was edited and Verify Integrity is on, or
    • the save comes from another device and Allow Read Any Owner is off.

    Catch the exception where you load your save:

    int score;
    
    try
    {
        score = ProtectedPlayerPrefs.GetInt("player.score");
    }
    catch (Exception)
    {
        // The save was changed or copied. Reset it, or flag the player.
        score = 0;
    }
    
    Warning

    Anything stored on the player's device can be read in the end. Never store real secrets in PlayerPrefs, protected or not. That includes server API keys, passwords, and unlock codes.


    Back to the Protected overview.

    In This Article
    Back to top GuardingPearSoftware documentation