# 📝 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 PlayerPrefs
  • EazyEditorPrefs — 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");

# 🔨 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();

# ⚙️ 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();

# ⚖️ Which Should You Use?

Feature Static Access Instance-Based Access
Setup Minimal, direct Requires explicit object creation
Reusability Not reusable (uses static calls) Highly reusable, DI-friendly
Type Safety
Encryption/Compression Limited at point of call Full control per instance
Abstraction & Injection Not ideal Clean, testable, and fully injectable
Editor/Tooling Integration
Best For Simple projects, quick globals Scalable systems, architecture-first projects