Settings System
An advanced settings system based on Reflection
Last updated
Was this helpful?
An advanced settings system based on Reflection
Last updated
Was this helpful?
The Settings System in the FPS Framework is designed to provide everything you need to create and manage a settings menu using a single ScriptableObject. This system handles the organization and storage of settings, making it easy to integrate with your UI and game preferences.
However, if you prefer, you can create a simpler system on your own and still use the built-in to handle saving and loading settings. This gives you flexibility depending on how complex you want your settings management to be.
The Settings Preset is a ScriptableObject that can be created through the Create Asset menu. To create one:
Right-click in the Project window.
Navigate to Create > Akila > FPS Framework > Settings System.
Choose any of the default preset options available.
This will generate a new Settings Preset asset that you can use to manage and configure game settings. These ScriptableObjects serve as examples, and while you can use them as they are, it's recommended to write your own custom ones or modify them to suit your specific needs, along with the default settings provided.
The message “There are no sections.” appears because the settings preset currently has no sections assigned.
But first—what is a section? A section is a logical grouping of related settings within the preset. For example, you might have sections like Graphics, Audio, Controls or Gameplay. Create/Remove one by clicking on + or - buttons.
Each section contains customizable Options. To modify them:
Click + button to add an option
Click - button to remove an option
These controls appear at the bottom of each section (as shown in the screenshot below the message "There are no options...").
Each section is made up of 2 fields.
Name
The name identifies the option and is used to find or trigger it.
Function
The function called when the option is triggered.
The function called when the option is triggered. For testing, use the built-in print()
function to understand how the system works.
Each option can be triggered using the Setting Applier component. The Setting Applier handles everything for you: it locates the appropriate UI elements, saves and loads data, applies the selected settings, and ensures they are preserved across sessions.
Make sure to attach the Setting Applier component only to UI elements such as Button, Slider, Dropdown, or similar components.
The warning in the Setting Applier component is happening because there is no Settings Manager component in the scene. It is recommended to add the Settings Manager to a dedicated GameObject in your scene or to your Game Manager.
The Settings Manager has two fields: AutoApply and Settings Presets List. The Settings Presets List holds all your settings presets. If AutoApply is enabled, any change in the UI will automatically trigger the Setting Applier to apply the updated settings.
Next, if you go back to your Setting Applier component, it should no longer display any errors.
If you click on the path to modify it, you will see the options and sections you defined, sorted in the format: PresetName/SectionName/OptionName. Select the option you want in the desired section, as this will be the option triggered when the Setting Applier component is activated from the UI element.
With this setup (When a UI element is attached to the Setting Applier or to one of its children or parents), whenever the UI element is interacted with, it will apply the option it refers to from the preset it's linked to.
If AutoApply is disabled, you can manually apply all settings by calling the function SettingApplier.ApplyAll().
The Settings Preset was designed this way to allow for easy scripting. You have a lot of flexibility to code, as everything interacts indirectly using C# Reflection, which simplifies the coding process.
To code a custom settings preset, start by creating a new class, add the namespace Akila.FPSFramework
, and change the inheritance to SettingsPreset.
Prints a message to the console.
OnAwake
Called from the settings manager before the first frame is drawn.
OnStart
Called from the settings manager after the first frame is drawn.
OnUpdate
Called from the settings manager each frame.
OnApplicationQuit
Called from the settings manager when quitting playmode or quiting the build.
Start by adding the namespace Akila.FPSFramework in your custom class.
Then change the class’s inheritance to SettingsPreset like this:
Remember, the SettingsPreset class inherits from ScriptableObject, so you need to add the attribute [CreateAssetMenu]
before the start of your custom class.
To use the class you've just written, go to the Project window in the Unity Editor and create a new asset file for the new settings preset you just created.
The system can only interact with functions that have either one parameter or no parameters at all. These parameters must be one of the following types: int, float, or bool.
Here’s a quick example use:
By creating a new asset file for the class you wrote you can find your custom functions in any option’s Function field and if the option is triggered your custom function will be called.
The custom functions that are acceptable in the SettingsPreset can only have a single parameter.
The SettingApplier component will save and load the last saved options and apply them when the game starts automaticlly.
The Setting Applier component determines what to save and load based on the assigned Option Path, so make sure to avoid using duplicate paths in different Setting Applier components.