npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

extensions.unity.playerprefsex

v2.0.1

Published

Lightweight package with optimized advanced version of PlayerPrefs. Under the hood it uses the same PlayerPrefs system, but creates flexible wrapper for default system.

Downloads

17

Readme

Unity PlayerPrefsEx

npm openupm License Stand With Ukraine

PlayerPrefsEx is a lightweight package that is an extended version of PlayerPrefs from Unity. Under the hood, it uses the same PlayerPrefs system but creates a flexible wrapper for the default system.

Static API Usage

using Extensions.Unity.PlayerPrefsEx;

// Static API Getters                    // Static API Setters
PlayerPrefsEx.GetInt("key");             PlayerPrefsEx.SetInt("key", 10);         
PlayerPrefsEx.GetBool("key");            PlayerPrefsEx.SetBool("key", false);        
PlayerPrefsEx.GetFloat("key");           PlayerPrefsEx.SetFloat("key", 2.123f);       
PlayerPrefsEx.GetString("key");          PlayerPrefsEx.SetString("key", "hello world");      
PlayerPrefsEx.GetBigInt("key");          PlayerPrefsEx.SetBigInt("key", BigInteger.Parse("100"));
PlayerPrefsEx.GetDateTime("key");        PlayerPrefsEx.SetDateTime("key", DateTime.Now);    
PlayerPrefsEx.GetVector2("key");         PlayerPrefsEx.SetVector2("key", Vector2.up);     
PlayerPrefsEx.GetVector2Int("key");      PlayerPrefsEx.SetVector2Int("key", Vector2Int.up);  
PlayerPrefsEx.GetVector3("key");         PlayerPrefsEx.SetVector3("key", Vector3.up);     
PlayerPrefsEx.GetVector3Int("key");      PlayerPrefsEx.SetVector3Int("key", Vector3Int.up);

// Static API Getters Generic            // Static API Setters Generic
PlayerPrefsEx.GetJson<Player>("key");    PlayerPrefsEx.SetJson<Player>("key", new Player());
PlayerPrefsEx.GetJson<Enemy>("key");     PlayerPrefsEx.SetJson<Enemy>("key", new Enemy());
PlayerPrefsEx.GetJson<Data>("key");      PlayerPrefsEx.SetJson<Data>("key", new Data());

Variables API Usage

// Variables API
var score = new PlayerPrefsInt("score");
score.Value = 100;

// Variables API Generic
var player = new PlayerPrefsJson<Player>("player");
var enemy = new PlayerPrefsJson<Enemy>("enemy");

var tempPlayer = player.Value; // take value from PlayerPrefs
tempPlayer.Health -= enemy.Value.Damage; // change internal properties
player.Value = tempPlayer; // save back changed instance

Extended default list of types to store

PlayerPrefsInt     PlayerPrefsVector2     PlayerPrefsBigInt
PlayerPrefsFloat   PlayerPrefsVector2Int  PlayerPrefsDateTime
PlayerPrefsBool    PlayerPrefsVector3     PlayerPrefsJson<T>
PlayerPrefsString  PlayerPrefsVector3Int  PlayerPrefsEx<T>

Installation

When you package is distributed, you can install it into any Unity project.

openupm add extensions.unity.playerprefsex

Features

✔️ Key is encrypted. Encrypted depends on a device. Much more harder for hackers to hack your data. Saved data at one device won't work on another one if someone copied it from device to device. In the same time for UnityEditor the device identifier is a constant. That means data copied between devices could be opened if you work on multiple machines and want to save/sent/load saved data on different machines.

✔️ Create variable instance of any PlayerPrefs, work with it as a regular variable

var score = PlayerPrefsInt("score");
score.Value = 100;

✔️ Use generic types for creating PlayerPrefs variables

var player = PlayerPrefsJson<Player>("player");
player.Value = new Player();

✔️ Using multiple variables with the same Type and Key shares data between them.

var variableA = new PlayerPrefsString("message");
var variableB = new PlayerPrefsString("message");

variableA.Value = "hello world";

Debug.Log(variableA.Value); // "hello world";
Debug.Log(variableB.Value); // "hello world";

// variableA.Value == variableB.Value
// the same memory spot, just two links 

✔️ Generating unique Key for each type. No way to overlap value of another type by same key.

var variableInt = new PlayerPrefsInt("SAME_KEY");
var variableString = new PlayerPrefsString("SAME_KEY");

variableInt.value = 100;
variableString.Value = "abcd"

// variableInt.value != variableString.value
// there are dedicated storage for each type, generic inclusive

Unique key per device

PlayerPrefsEx supports simple protection from hackers who change default values for PlayerPrefs. That is approach to use unique key per device keys, simply adding device id to the end of a key. As a result external modification of PlayerPrefs could work only at single device and can't be spread like a hacked build of a game.

⚠️ Keep in mind! Cloud safe could not work for single user with multiple devices.

⚠️ Highly not recommended to change the feature in runtime. Because data stored under disabled unique key feature are unaccessible from enabled unique key, the same works in opposite way.

Activate Unique Key: option 1 (recommended)

Add PLAYER_PREFS_UNIQUE_KEY define to a project. Follow Edit -> Project Settings -> Player to add the define.

This approach is better, because you have 100% guarantee that no one PlayerPrefsEx API gonna be used before your setup Unique Key option at runtime.

image

Activate Unique Key: option 2 (not recommended)

You can make it in runtime by simply change value of PlayerPrefsEx.Settings.UniqueKeyPerDevice.

⚠️ Highly not recommended to change the feature in runtime. Because data stored under disabled unique key feature are unaccessible from enabled unique key, the same works in opposite way.

PlayerPrefsEx.Settings.UniqueKeyPerDevice = true;

Custom data types

There are two ways to do that.

1. Using PlayerPrefsJson<T>

// Static API
var player = PlayerPrefsEx.GetJson<Player>("player");
PlayerPrefsEx.SetJson<Player>("player", new Player());

// Variables API
var player = new PlayerPrefsJson<Player>("player");
player.Value = new Player();

2. Using PlayerPrefsEx<T>

Create dedicated class for more clean usage.

// Variables API
var enemy = PlayerPrefsEnemy("enemy");
enemy.Value = new Enemy();

public class PlayerPrefsEnemy : PlayerPrefsEx<Enemy>
{
    public PlayerPrefsEnemy(string key, Enemy defaultValue = default) : base(key, defaultValue) 
    { 

    }

    protected override Enemy Deserialize(string value)
    {
        return JsonUtility.FromJson<Enemy>(value);
    }
    protected override string Serialize(Enemy value)
    {
        return JsonUtility.ToJson(value);
    }
}