The Settings system provides complete functionality. The demo samples include a working example with Graphics, Audio, and Localization settings, which can be used as a starting point for your implementation.
ScriptableObject that contains dictionary of SettingsDataSection.
ScriptableObject, abstract class you have to implement to add custom sections to SettingsDataSO.
Copy [ CreateAssetMenu (fileName = "SectionGameplay" , menuName = "CupkekGames/Settings/SectionGameplay" )]
public class SettingsDataSectionGameplay : SettingsDataSection
{
[ SerializeField ] private float _cursorSensitivity;
public float CursorSensitivity
{
get => _cursorSensitivity;
set
{
_cursorSensitivity = value;
// Apply cursor sensitivity here
}
}
public override bool Equals ( object obj)
{
if (obj == null || GetType() != obj .GetType())
return false ;
SettingsDataSectionGameplay b = ( SettingsDataSectionGameplay )obj;
return _cursorSensitivity == b . _cursorSensitivity ;
}
public override int GetHashCode ()
{
return _cursorSensitivity .GetHashCode();
}
public override void SaveToPlayerPrefs ( string key)
{
PlayerPrefs .SetFloat( $"{key}_CursorSensitivity" , _cursorSensitivity);
PlayerPrefs .Save();
}
public override void LoadFromPlayerPrefs ( string key)
{
if ( PlayerPrefs .HasKey( $"{key}_CursorSensitivity" ))
{
_cursorSensitivity = PlayerPrefs .GetFloat( $"{key}_CursorSensitivity" );
}
}
public override void CopyValuesFrom ( SettingsDataSection section)
{
if (section is SettingsDataSectionGameplay copy)
{
_cursorSensitivity = copy . _cursorSensitivity ;
}
}
/// < summary >
/// Applies settings from another < see cref = "SettingsDataSectionGameplay" /> instance.
/// </ summary >
/// < param name = "settingsData" >The source < see cref = "SettingsDataSectionGameplay" /> instance containing the settings to apply.</ param >
public override void ApplySettings ( SettingsDataSection settingsData)
{
if (settingsData is SettingsDataSectionGameplay copy)
{
CursorSensitivity = copy . _cursorSensitivity ;
}
}
}
Main view for Settings UI. Add view sections as a child to this object.
MonoBehaviour, abstract class you have to implement to add custom sections to SettingsMenuView.
Copy using UnityEngine . UIElements ;
using CupkekGames . Core ;
public class SettingsMenuViewAudio : SettingsMenuViewSection
{
private Slider _sliderCursorSensitivity;
protected override void Awake ()
{
base.Awake();
_sliderCursorSensitivity = UIDocument . rootVisualElement .Q < Slider > ( "CursorSensitivitySlider" );
}
public void OnEnable ()
{
_sliderCursorSensitivity .RegisterValueChangedCallback(OnSliderChanged);
}
public void OnDisable ()
{
_sliderCursorSensitivity .UnregisterValueChangedCallback(OnSliderChanged);
}
public override void ApplySettingsToUI ()
{
SettingsDataSectionGameplay gameplay = ( SettingsDataSectionGameplay ) _changedSettings . Dictionary [ "gameplay" ];
_sliderCursorSensitivity . value = gameplay . CursorSensitivity ;
}
private void OnSliderChanged ( ChangeEvent < int > evt)
{
SettingsDataSectionGameplay gameplay = ( SettingsDataSectionGameplay ) _changedSettings . Dictionary [ "gameplay" ];
gameplay . CursorSensitivity = evt . newValue ;
}
}