The Kanda SDK provides a UserSettingsService to persist various runtime data between application sessions. This service is designed to store and retrieve user preferences, system configurations, and other values that need to be maintained across different runs of the application.
Overview
The UserSettingsService is implemented using Unity's PlayerPrefs system, with additional functionality for saving complex objects and encrypting sensitive data. It provides a convenient interface for storing and retrieving various types of data, including primitives, strings, and serializable objects.
Key Features
- Persistence of data between application sessions
- Support for primitive types (int, float, string)
- JSON serialization for complex objects
- Encryption for sensitive data
Accessing the UserSettingsService
You can access the UserSettingsService using the AppServiceLocator:
using Kanda.Foundation.DependencyInjection;
Provides a way to persist user settings between application sessions. This is implemented using Unity...
Definition UserSettingsService.cs:15
Definition UserSettingsService.cs:9
Basic Usage
Checking if a Key Exists
bool hasKey = userSettings.
HasKey(
"myKey");
bool HasKey(string key)
If true, a setting with this key exists in user settings.
Setting Values
userSettings.
SetInt(
"myIntKey", 42);
userSettings.
SetFloat(
"myFloatKey", 3.14f);
userSettings.
SetString(
"myStringKey",
"Hello, World!");
void SetFloat(string key, float value)
Commits a float to user settings.
void SetInt(string key, int value)
Commits an integer to user settings.
void SetString(string key, string value)
Commits a string to user settings.
Getting Values
int myInt = userSettings.
GetInt(
"myIntKey");
float myFloat = userSettings.
GetFloat(
"myFloatKey");
string myString = userSettings.
GetString(
"myStringKey");
int GetInt(string key)
Tries to get an int from user settings.
string GetString(string key)
Tries to get a string from user settings.
float GetFloat(string key)
Tries to get a float from user settings.
Deleting a Key
void DeleteKey(string key)
Removes a user setting by key.
Forcing a Save
This will typically happen automatically when quitting the application, but if a setting value is particularly important, forcing a save will commit it to disk immediately in case of a crash.
void ForceSave()
Forces user settings to be committed to disk. This should happen automatically when application exits...
Working with Complex Objects
The UserSettingsService can serialize and deserialize complex objects using JSON:
var myObject = new MyClass { Property1 = "Value1", Property2 = 42 };
userSettings.SetObject("myObjectKey", myObject);
MyClass retrievedObject = userSettings.GetObject<MyClass>("myObjectKey");
Encrypting Sensitive Data
For sensitive information, you can use the encryption methods:
var sensitiveObject = new SensitiveData { UserToken = "secret_token" };
userSettings.SetObjectWithEncryption("sensitiveObjectKey", sensitiveObject);
SensitiveData retrievedSensitiveObject = userSettings.GetEncryptedObject<SensitiveData>("sensitiveObjectKey");
string GetEncryptedString(string key)
Tries to get and decrypt a string from user settings.
void SetStringWithEncryption(string key, string value)
Encrypts and commits a string to user settings.
Best Practices
Use constants for keys: To avoid typos and ensure consistency, define constants for your setting keys.
public static class UserSettingsKeys
{
public const string InputSensitivity = "InputSensitivity";
public const string SelectedTheme = "SelectedTheme";
public const string LastLoginDate = "LastLoginDate";
}
Handle missing keys: When retrieving values, be prepared to handle cases where the key doesn't exist.
float sensitivity = userSettings.
HasKey(UserSettingsKeys.InputSensitivity)
? userSettings.
GetFloat(UserSettingsKeys.InputSensitivity)
: 1.0f;
- Use encryption for sensitive data: Always use the encryption methods when storing sensitive information like authentication tokens or personal data.
- Avoid storing large amounts of data: The
PlayerPrefs system is not designed for storing large datasets. Consider alternative storage solutions for substantial amounts of data.
- Be mindful of performance: While generally fast, reading and writing settings can impact performance if done frequently. Consider caching values in memory if they're accessed often.
Example: Theme Manager
Here's an example of how you might use the UserSettingsService in a theme manager:
public class ThemeManager : MonoBehaviour
{
private void Awake()
{
LoadTheme();
}
private void LoadTheme()
{
string themeName = _userSettings.
HasKey(UserSettingsKeys.SelectedTheme)
? _userSettings.
GetString(UserSettingsKeys.SelectedTheme)
: "Default";
ApplyTheme(themeName);
}
public void SetTheme(string themeName)
{
_userSettings.
SetString(UserSettingsKeys.SelectedTheme, themeName);
ApplyTheme(themeName);
}
private void ApplyTheme(string themeName)
{
}
}
By leveraging the UserSettingsService, you can easily create persistent, user-specific configurations that enhance the user experience and maintain state across application sessions.