Settings System
An advanced settings system based on Reflection
Last updated
An advanced settings system based on Reflection
Last updated
The settings system in the FPS Framework is designed to give you most of what you need to create and manage your settings menu in one scriptable object. You can create a simpler system yourself and use the built-in save system to save and load if you want.
The settings preset is a scriptable object that you can create from the create asset menu. To access the create asset menu, right-click anywhere in the project window, hover the mouse cursor over the menu Create, and then click on the item Default Settings Preset:
By clicking on Default Settings Preset, the editor will create a settings preset asset file that will act as a base for all of your settings and allow you to add and manage sections and options.
The settings preset should look like this when created:
The message says, “There are no sections.” This is because we didn’t add any sections to the preset, but first, what is a section?
In all games, settings menu is divided into multiple sections, like Video, Audio, and Input. The sections exist to manage and organize the work, which is why you can add multiple sections. To add any section, click on the + button, and to remove any section, click on the - button.
This is how each section should look when created:
By clicking on the + icon below the message, you can add a new option, but what is an option? Options are the options you give to the user, like Display Mode and Mouse Sensitivity.
This is what each option should look like when created
Each option has 2 fields, Name and Function.
Name
The name of the option. This is used to find the option and to trigger it.
Function
The target function that the option should call when triggered.
The function could be anything from the built-in functions to your custom functions but for now just use the function “print” just to test it and understand the system in a wider manar.
This is what ‘’using a function” means:
The function is set to 'print' so it's using the function 'print'
Each option can be triggered using the component “Setting Applier”
The applier component needs to be called from each UI Element’s events. Just to test and follow along by creating a dropdown and adding the “Setting Applier” component on the dropdown you just created and this is how it should look like:
The warning in the setting applier component happened because there’s no Settings Manager component in the scene, it’s recommended to add that component on a dedicated game object in your scene or on your game manager.
This is how it should look like:
Now if you go back to your dropdown and take a look at the setting applier component you should see another warning, to fix this warning assign any settings preset in the field “Setting Preset” inside the settings manager.
Now this what you should see on the dropdown:
If you click on the path to modify it you will find the options you defined and the sections you defined sorted like this: Section/Option. Select the option you like in the section you like as this will be the option to trigger when the applier component is triggered from the UI Element.
Triggering the setting applier is very easy and straight forward to trigger the applier add a new callback in the dropdowns's event like this:
Inside of this event you can drag the setting applier component and call your functions from there. If the function has a parameter select the dynamic parameter functions only and this is how it should look like when opting.
In this case it’s just the print function so choose the highlighted item in the image (Dynamic Int)
Now if you play and select any option in the dropdown the console should print a message with the value of the dropdown.
The settings preset was designed like that to allow scripting, you got a lot of space to code here since everything is interacting indirectly using C# Reflections which makes it easy to code.
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.
Save
Called from the settings manager in OnDisable, OnDestroy & OnApplicationQuit.
Load
Called from the settings manager in Start.
Create a new C# class and add the namespace FPSFramework in your custom class like this:
Then change the class’s inheritance to SettingsPreset like this:
Remember that SettingsPreset class is inheriting from ScriptableObject which means you need to add the attribute [CreateAssetMenu] before the start of your custom class. To use the class you just wrote go to the project window in the unity editor and create a new asset file of the new settings preset you just wrote. Now this is results:
The system can only interact with functions with 1 parameter or no parameters at all, these parameters could be one of the 3: int, floats 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.
Read the save system part of this document to understand the save system better.
To save & load and apply settings according to it create a data class for your preset like this:
Now reference the data class you just created in your preset like this:
You can save & load in the available virtual functions.