• Hello
Search Results for

    Show / Hide Table of Contents

    Protected PlayerPrefs

    Unity PlayerPrefs are easy to edit on disk. Protected PlayerPrefs add a check, and encryption when you set a key.

    Walkthrough: Protecting saved data.

    How to use

    You can use them in three ways:

    1. ProtectedPlayerPrefs: Same calls as Unity PlayerPrefs, with the extra protection. Saves stay in Unity's usual place.
    2. ProtectedFileBasedPlayerPrefs: Saves to a file you choose. The default folder is Application.persistentDataPath. Set ProtectedFileBasedPlayerPrefs.FilePath to change it. The calls are the same.
    3. Inspector: Assign the values on a component. You still read them in code.

    Protected PlayerPrefs

    The class is in GUPS.AntiCheat.Protected.Storage.Prefs. Replace PlayerPrefs with ProtectedPlayerPrefs. PlayerPrefs.GetInt("MyKey") becomes ProtectedPlayerPrefs.GetInt("MyKey"). Saves stay in Unity's usual place on Windows, Android, and iOS.

    You can also store more types than Unity PlayerPrefs:

    Byte,
    ByteArray,
    Boolean,
    Int16,
    Int32,
    Int64,
    UInt16,
    UInt32,
    UInt64,
    Single,
    Double,
    Decimal,
    Char,
    String,
    Color,
    Color32,
    Vector2,
    Vector2Int,
    Vector3,
    Vector3Int,
    Vector4,
    Quaternion,
    Rect,
    Plane,
    Ray,
    Matrix4x4
    

    Code 1: Supported data types.

    The usual PlayerPrefs methods are there, plus a few more:

    //Bool
    ProtectedPlayerPrefs.SetBool("My Bool Key", true);
    bool protectedBoolValue = ProtectedPlayerPrefs.GetBool("My Bool Key");
    Debug.Log("Real Value: " + protectedBoolValue);
    
    //Int
    ProtectedPlayerPrefs.SetInt("My Int Key", 1234);
    int protectedIntValue = ProtectedPlayerPrefs.GetInt("My Int Key");
    Debug.Log("Real Value: " + protectedIntValue);
    
    //Float
    ProtectedPlayerPrefs.SetFloat("My Float Key", 1234.56f);
    float protectedFloatValue = ProtectedPlayerPrefs.GetFloat("My Float Key");
    Debug.Log("Real Value: " + protectedFloatValue);
    
    //String
    ProtectedPlayerPrefs.SetString("My String Key", "Hello World!");
    string protectedStringValue = ProtectedPlayerPrefs.GetString("My String Key");
    Debug.Log("Real Value: " + protectedStringValue);
    
    //Vector2
    ProtectedPlayerPrefs.SetVector2("My Vector2 Key", new Vector2(123.55f, -10012.2f));
    Vector2 protectedVector2Value = ProtectedPlayerPrefs.GetVector2("My Vector2 Key");
    Debug.Log("Real Value: " + protectedVector2Value);
    
    //Vector3
    ProtectedPlayerPrefs.SetVector3("My Vector3 Key", new Vector3(123.55f, -10012.2f, 771123f));
    Vector3 protectedVector3Value = ProtectedPlayerPrefs.GetVector3("My Vector3 Key");
    Debug.Log("Real Value: " + protectedVector3Value);
    
    //Vector4
    ProtectedPlayerPrefs.SetVector4("My Vector4 Key", new Vector4(1, 2, 3, 4));
    Vector4 protectedVector4Value = ProtectedPlayerPrefs.GetVector4("My Vector4 Key");
    Debug.Log("Real Value: " + protectedVector4Value);
    
    //Quaternion
    ProtectedPlayerPrefs.SetQuaternion("My Quaternion Key", new Quaternion(1, 2, 3, 4));
    Quaternion protectedQuaternionValue = ProtectedPlayerPrefs.GetQuaternion("My Quaternion Key");
    Debug.Log("Real Value: " + protectedQuaternionValue);
    

    Code 2: Simply replace the PlayerPrefs methods with ProtectedPlayerPrefs methods.

    Protected File Based PlayerPrefs

    Use ProtectedFileBasedPlayerPrefs when you do not want Unity's usual save place. It is in GUPS.AntiCheat.Protected.Storage.Prefs. The default file is under Application.persistentDataPath. You can set another path.

    public static class ProtectedFileBasedPlayerPrefs
    {
        // ...
    
        /// <summary>
        /// Gets or sets the custom file path for storing player preferences.
        /// Default is: Application.persistentDataPath + System.IO.Path.PathSeparator + "playerprefs.dat"
        /// </summary>
        public static String FilePath { get; set; } = Application.persistentDataPath + System.IO.Path.PathSeparator + "playerprefs.dat";
    
        // ...
    }
    

    Code 3: Assign a custom file path to ProtectedFileBasedPlayerPrefs.FilePath.

    The methods are the same as ProtectedPlayerPrefs. Only the save place is different.

    Inspector

    You can also assign common types in the Inspector and read them from fields:

    public class PlayerPrefs_Behaviour : MonoBehaviour
    {
        public ProtectedBoolPref protectedBool;
        public ProtectedFloatPref protectedFloat;
        public ProtectedIntPref protectedInt;
        public ProtectedStringPref protectedString;
        public ProtectedVector2Pref protectedVector2;
        public ProtectedVector3Pref protectedVector3;
        public ProtectedVector4Pref protectedVector4;
        public ProtectedQuaternionPref protectedQuaternion;
    }
    

    Code 4: Easily access your protected PlayerPrefs through properties in your code.

    In the Unity Inspector this might look like:

    Figure 1: Assign your protected PlayerPrefs through the Unity Inspector.

    Settings

    Open Edit > Project Settings > GuardingPearSoftware > AntiCheat. Go to PlayerPrefs - Settings.

    Figure 2: In this section you can configure the security settings for your player preferences (PlayerPrefs).

    • Hash Key: On means the key name is stored as a hash, not as plain text. Recommended: on.
    • Value Encryption Key: The key used to encrypt the value. If this is empty, the value is not encrypted. If you change the key later, old saves cannot be read.
    • Allow Read Any Owner: On means any device can read the save, so players can share it. Off means only the device that wrote it can read it. The owner id comes from SystemInfo.deviceUniqueIdentifier.
    • Verify Integrity: On means a signature is stored next to the value. It is built from the type, the value, and the owner. A changed save then fails the check.
    In This Article
    Back to top GuardingPearSoftware documentation