Class UnityGameSettingsService
Unity-specific implementation of game settings service using MonoBehaviour with persistent storage.
Manages game settings dictionary with type-safe value operations (string, int, float, bool),
event dispatching for settings changes, and file-based persistence through PersistenceUseCases.
Provides initialization with logging, persistence, and path services for Unity applications
with foundation settings identification and Unity editor integration.
Namespace: Serenity.GameSettings.Infrastructure.Services
Assembly: Serenity.UnityGlobal.Infrastructure.dll
Syntax
public class UnityGameSettingsService : MonoBehaviour, IGameSettingsService, IService, IFoundationSettings
Constructors
UnityGameSettingsService()
Declaration
public UnityGameSettingsService()
Properties
Guid
Declaration
public string Guid { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Id
Declaration
public string Id { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
LogService
Declaration
public ILogService LogService { get; set; }
Property Value
| Type | Description |
|---|---|
| ILogService |
Methods
GetBoolValue(GameSettingsGetBoolValueInput)
Retrieves a boolean game setting value with PlayerPrefs fallback.
Gets the boolean value for the specified setting ID, using PlayerPrefs as the data source
and falling back to the default value if not found.
Declaration
public GameSettingsGetBoolValueOutput GetBoolValue(GameSettingsGetBoolValueInput input)
Parameters
| Type | Name | Description |
|---|---|---|
| GameSettingsGetBoolValueInput | input | Input DTO containing the setting ID to retrieve |
Returns
| Type | Description |
|---|---|
| GameSettingsGetBoolValueOutput | Output DTO containing the setting ID and boolean value |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the setting is not of type BOOL |
GetEventDispatcherService()
Retrieves the event dispatcher service instance
Declaration
public IEventDispatcherService GetEventDispatcherService()
Returns
| Type | Description |
|---|---|
| IEventDispatcherService | Current event dispatcher service instance, or null if not set |
GetFloatValue(GameSettingsGetFloatValueInput)
Retrieves a float game setting value with PlayerPrefs fallback.
Gets the float value for the specified setting ID, using PlayerPrefs as the data source
and falling back to the default value if not found.
Declaration
public GameSettingsGetFloatValueOutput GetFloatValue(GameSettingsGetFloatValueInput input)
Parameters
| Type | Name | Description |
|---|---|---|
| GameSettingsGetFloatValueInput | input | Input DTO containing the setting ID to retrieve |
Returns
| Type | Description |
|---|---|
| GameSettingsGetFloatValueOutput | Output DTO containing the setting ID and float value |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the setting is not of type FLOAT |
GetGameSettings()
Retrieves all game settings as a dictionary.
Returns the complete collection of settings with string keys mapped to GameSettings values.
Declaration
public Dictionary<string, GameSettingsEntity> GetGameSettings()
Returns
| Type | Description |
|---|---|
| Dictionary<string, GameSettingsEntity> | Dictionary containing all game settings, or null if not initialized |
GetGameSettings(string)
Retrieves game settings for a specific identifier.
Looks up settings by ID and returns the corresponding GameSettings object.
Declaration
public GameSettingsEntity GetGameSettings(string id)
Parameters
| Type | Name | Description |
|---|---|---|
| string | id | Unique identifier for the game settings to retrieve |
Returns
| Type | Description |
|---|---|
| GameSettingsEntity | GameSettings object if found, null if not found |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when game settings dictionary is not initialized |
GetIntValue(GameSettingsGetIntValueInput)
Retrieves an integer game setting value with PlayerPrefs fallback.
Gets the integer value for the specified setting ID, using PlayerPrefs as the data source
and falling back to the default value if not found.
Declaration
public GameSettingsGetIntValueOutput GetIntValue(GameSettingsGetIntValueInput input)
Parameters
| Type | Name | Description |
|---|---|---|
| GameSettingsGetIntValueInput | input | Input DTO containing the setting ID to retrieve |
Returns
| Type | Description |
|---|---|
| GameSettingsGetIntValueOutput | Output DTO containing the setting ID and integer value |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the setting is not of type INT |
GetStringValue(GameSettingsGetStringValueInput)
Retrieves a string game setting value with PlayerPrefs fallback.
Gets the string value for the specified setting ID, using PlayerPrefs as the data source
and falling back to the default value if not found.
Declaration
public GameSettingsGetStringValueOutput GetStringValue(GameSettingsGetStringValueInput input)
Parameters
| Type | Name | Description |
|---|---|---|
| GameSettingsGetStringValueInput | input | Input DTO containing the setting ID to retrieve |
Returns
| Type | Description |
|---|---|
| GameSettingsGetStringValueOutput | Output DTO containing the setting ID and string value |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the setting is not of type STRING |
HasPersistedGameSettings()
Checks synchronously whether persisted game settings exist on disk.
Declaration
public bool HasPersistedGameSettings()
Returns
| Type | Description |
|---|---|
| bool | True if a saved settings file exists on disk, false otherwise. |
Initialize(ILogService, PersistenceUseCases, IPathService, string)
Initializes the Unity game settings service with required dependencies.
Sets up logging service for debug output, persistence use cases for file operations,
and path service for file path resolution. Must be called before using other methods.
Declaration
public void Initialize(ILogService logService, PersistenceUseCases persistenceUseCases, IPathService pathService, string persistedFileName = null)
Parameters
| Type | Name | Description |
|---|---|---|
| ILogService | logService | Logging service for debug output and error reporting |
| PersistenceUseCases | persistenceUseCases | Persistence use cases for file-based settings storage |
| IPathService | pathService | Path service for platform-specific file path resolution |
| string | persistedFileName | The filename used for persisted game settings. Uses DefaultPersistedFileName when null. |
InitializeEventDispatcher(IEventDispatcherService)
Initializes the event dispatcher service and subscribes to game settings events.
Sets up the event dispatcher and automatically subscribes to reset and persist events.
Declaration
public void InitializeEventDispatcher(IEventDispatcherService eventDispatcherService)
Parameters
| Type | Name | Description |
|---|---|---|
| IEventDispatcherService | eventDispatcherService | Event dispatcher service to use for game settings events |
LoadPersistedGameSettings()
Loads persisted game settings from configuration file asynchronously.
Reads game settings from the user data directory CFG file with flexible parsing
that ignores comments, unknown keys, and handles various value formats.
Declaration
public void LoadPersistedGameSettings()
LoadPersistedGameSettingsSync()
Loads persisted game settings synchronously from disk.
Blocks until settings are loaded, ensuring they are available before method returns.
Declaration
public void LoadPersistedGameSettingsSync()
OnValueUpdated(string)
Notifies that game settings values have been updated.
Dispatches an event to inform other systems that game settings have changed.
Declaration
public void OnValueUpdated(string changedSettingId = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | changedSettingId | The ID of the specific setting that changed. |
PersistGameSettings()
Persists game settings to storage asynchronously.
Saves all storable game settings to a formatted text file with proper type-specific serialization.
Only includes settings marked as storable and formats them with consistent padding.
Declaration
public void PersistGameSettings()
SetBoolValue(GameSettingsSetBoolValueInput)
Sets a boolean game setting value using PlayerPrefs for persistence.
Stores the boolean value for the specified setting ID and triggers value update events.
Declaration
public void SetBoolValue(GameSettingsSetBoolValueInput input)
Parameters
| Type | Name | Description |
|---|---|---|
| GameSettingsSetBoolValueInput | input | Input DTO containing the setting ID and boolean value to set |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the setting is not of type BOOL |
SetEventDispatcherService(IEventDispatcherService)
Sets the event dispatcher service instance.
Configures the event dispatcher to be used for game settings event handling.
Declaration
public void SetEventDispatcherService(IEventDispatcherService eventDispatcherService)
Parameters
| Type | Name | Description |
|---|---|---|
| IEventDispatcherService | eventDispatcherService | Event dispatcher service to set for this game settings service |
SetFloatValue(GameSettingsSetFloatValueInput)
Sets a float game setting value using PlayerPrefs for persistence.
Stores the float value for the specified setting ID and triggers value update events.
Declaration
public void SetFloatValue(GameSettingsSetFloatValueInput input)
Parameters
| Type | Name | Description |
|---|---|---|
| GameSettingsSetFloatValueInput | input | Input DTO containing the setting ID and float value to set |
SetGameSettings(GameSettingsEntity)
Updates or adds a specific game settings entry.
Adds the GameSettings to the dictionary using its Id property as the key.
Declaration
public void SetGameSettings(GameSettingsEntity gameSettings)
Parameters
| Type | Name | Description |
|---|---|---|
| GameSettingsEntity | gameSettings | GameSettings object to add or update in the collection |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when game settings dictionary is not initialized |
SetGameSettings(Dictionary<string, GameSettingsEntity>)
Replaces the entire game settings dictionary.
Sets the complete collection of game settings, overriding any existing data.
Declaration
public void SetGameSettings(Dictionary<string, GameSettingsEntity> gameSettings)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, GameSettingsEntity> | gameSettings | Dictionary of game settings to set as the new collection |
SetIntValue(GameSettingsSetIntValueInput)
Sets an integer game setting value using PlayerPrefs for persistence.
Stores the integer value for the specified setting ID and triggers value update events.
Declaration
public void SetIntValue(GameSettingsSetIntValueInput input)
Parameters
| Type | Name | Description |
|---|---|---|
| GameSettingsSetIntValueInput | input | Input DTO containing the setting ID and integer value to set |
SetStringValue(GameSettingsSetStringValueInput)
Sets a string game setting value using PlayerPrefs for persistence.
Stores the string value for the specified setting ID and triggers value update events.
Includes error handling with logging for failed operations.
Declaration
public void SetStringValue(GameSettingsSetStringValueInput input)
Parameters
| Type | Name | Description |
|---|---|---|
| GameSettingsSetStringValueInput | input | Input DTO containing the setting ID and string value to set |
SubscribeToEventDispatcherEvents()
Subscribes to game settings-related event dispatcher events.
Sets up event handlers for reset to default and persist game settings operations.
Declaration
public void SubscribeToEventDispatcherEvents()