#
📝 Working with prefs
EazyPrefs gives you two ways to work with prefs: static access (like traditional Unity prefs) or instance-based access (for more flexibility and cleaner architecture).
Both are fully supported and can even be used together in the same project. Choose the style that best fits your needs!
#
🏷️ Static Access
This style mimics Unity's native PlayerPrefs and EditorPrefs — but with strong typing and better structure. Static access in EazyPrefs is done through two auto-generated classes (called Prefs Wrappers):
EazyPlayerPrefs
— for PlayerPrefsEazyEditorPrefs
— for EditorPrefs
They provide strongly-typed, easy-to-use methods for accessing your preferences using static methods — similar to Unity’s own API, but much more powerful.
// Set and get a simple bool value
EazyPlayerPrefs.SetBoolPrefValue("MyBool", true);
bool myBool = EazyPlayerPrefs.GetBoolPrefValue("MyBool");
// Set and get a JSON-based pref (e.g. a custom class)
EazyPlayerPrefs.SetJSONObjPrefValue<UserData>("UserData", new UserData());
UserData data = EazyPlayerPrefs.GetJSONObjPrefValue<UserData>("UserData");
// Set and get a Vector2 stored as an EditorPref
EazyEditorPrefs.SetVector2PrefValue("MyVector2", new Vector2(5, 10));
Vector2 vec = EazyEditorPrefs.GetVector2PrefValue("MyVector2");
This approach is great when you want quick and simple pref access with minimal setup. It’s ideal for small projects or utility values that don’t need to move around.
Prefs Wrappers are automatically generated based on the prefs you have in your project. You can regenerate or update them at any time through the Settings tab.
#
🔨 Instance-Based Access
Instance-based prefs give you more flexibility and are better suited to clean code principles.
// Create a PlayerPref for a custom class
var dataSource = new PlayerPrefStorageSource("UserData");
var dataPref = new JSONObjPref<UserData>(dataSource);
dataPref.SetValue(new UserData(), encrypted: true, compressed: false);
UserData loadedData = dataPref.GetValue();
// Create an EditorPref for a string
var stringSource = new EditorPrefStorageSource("MyString");
var stringPref = new StringPref(stringSource);
stringPref.SetValue("Hello World");
string myString = stringPref.GetValue();
Use this when you want full control over how prefs are created, stored, and passed around in your app.
#
⚙️ How Instance-Based Access Works
When using instance-based prefs, the first step is to define the data source — either in PlayerPrefs or EditorPrefs. This is done through IPrefStorageSource
objects. IPrefStorageSource
is an interface that astracts how and where pref data is saved and loaded. This decouples storage from logic, so code is clean, testable, and reusable.
EazyPrefs comes with two ready-to-use implementations: PlayerPrefStorageSource
and EditorPrefStorageSource
.
- Use
PlayerPrefStorageSource
when accessing prefs from PlayerPrefs - Use
EditorPrefStorageSource
when accessing prefs from PlayerPrefs
// Access pref key using Unity's PlayerPrefs
var source = new PlayerPrefStorageSource("MyPlayerPrefKey");
// Access pref key using Unity's EditorPrefs
var source = new EditorPrefStorageSource("MyEditorPrefKey");
You then pass this source into any Pref object (e.g., StringPref
, IntPref
, JSONObjPref<T>
, etc.):
var source = new PlayerPrefStorageSource("MyPlayerPrefKey");
var stringPref = new StringPref(myStringSource);
stringPref.SetValue("Hello EazyPrefs");
string myString = stringPref.GetValue();
PlayerPrefs and EditorPrefs are separate systems
PlayerPrefs and EditorPrefs store data in completely different locations. This means that the same key can exist in both, but with different values.
For example, "myKey"
in PlayerPrefs might store player data, while "myKey"
in EditorPrefs could store an editor setting. If you try to access a pref using the wrong source (e.g., using EditorPrefStorageSource
to read a PlayerPref key), you’ll either get incorrect data or a default value.
Always make sure you're using the correct storage source for the type of data you're working with.
#
⚖️ Which Should You Use?
Pro tip
You can mix both styles in the same project. Use static access for simple or global values, and instance-based access where architecture matters.