Example 2: Writing the Shadow Setting

For the second example, we will write a setting allowing the user to control the shadow settings. When opening the Quality tab in the Project Settings, there are three options that can be set for the shadow setting: Disable Shadows, Hard Shadows Only, Hard and Soft Shadows.

To reflect these three options for our setting, we cannot use the ToggleGraphicSetting from the first example. Instead we use the MultiOptionGraphicSetting.

MultiOptionGraphicSetting is a very flexible base class. It allows you to define the value type of the setting and can be used for any setting which does not has a binary state, e.g. resolution, AA level, etc.

To get strated create a new script, name it ShadowSetting.cs and add the following content:

using Ilumisoft.GraphicsControl;
using UnityEngine;

public class ShadowSetting : MultiOptionGraphicSetting<int>
{
    public override string GetSettingName()
    {
        return "Shadows";
    }

    public override void Initialize()
    {
        // The names of the available options
        var names = new string[]
        {
            "Off",
            "Hard Shadows Only",
            "Hard and Soft Shadows"
        };

        // Add each option
        for (int i = 0; i < names.Length; i++)
        {
            AddOption(names[i], i);
        }

        // Set the index according to the current option
        SetIndex((int)QualitySettings.shadows);
    }

    public override void LoadSetting()
    {
        // Restore the index from the Graphic Settings Storage
        int index = GraphicSettingsStorage.GetInt(key: GetSettingName(), defaultValue: 0);

        // Set the index
        SetIndex(index);
    }

    public override void SaveSetting()
    {
        // Store the current index in the Graphic Settings Storage
        GraphicSettingsStorage.SetInt(key: GetSettingName(), value: GetIndex());
    }
}

As you can see the script is a lot larger than the one from the previous example. MultiOptionGraphicSetting is much more powerful than ToggleGraphicSetting, but therefore we need to write a bit more code as a tradeoff. In the Initialize method, we create the options we have, where the first option is represented by a value of 0, the second by 1 and the last by 2.Then we set the initial index by the current shadow quality setting.

With LoadSetting and SaveSetting we can restore the stored value or save it respectively.

Again we also need to write an applier, therefore create a new script called ShadowSettingApplier.cs with the following content:

using Ilumisoft.GraphicsControl;
using UnityEngine;

public class ShadowSettingApplier : GraphicSettingsApplier
{
    public override void ApplySettings()
    {
        if (GraphicSettingsManager.Instance.TryGet<ShadowSetting>(out var setting))
        {
            var shadowQuality = setting.GetSelectedOption();

            QualitySettings.shadows = (ShadowQuality)shadowQuality;
        }
    }
}

Here we get the selected option ( which is an integer) and convert it to the ShadowQuality to apply it.

In the last step, open the Graphic Settings Manager Prefab again, click AddComponent and add the ShadowSetting and the ShadowSettingApplier to the prefab. Now when running the sample scene you can change the shadow settings!

Last updated