Skip to content

Create Mod Settings

piotrulos edited this page Jan 12, 2022 · 11 revisions

Mod Settings

Mod settings need to be created in main Mod class by overriding ModSettings()

public class MyMod1 : Mod
{
    public override string ID => "MyMod1"; 
    public override string Name => "Testing";
    public override string Author => "piotrulos";
    public override string Version => "1.0";
    public override string Description => "";

    public override void ModSettings()
    {
        // All settings should be created here
    } 
}

If you want to update some things controlled by settings when settings valued are loaded from save, you need to also override ModSettingsLoaded()

public class MyMod1 : Mod
{
    public override string ID => "MyMod1"; 
    public override string Name => "Testing";
    public override string Author => "piotrulos";
    public override string Version => "1.0";
    public override string Description => "";

    public override void ModSettingsLoaded()
    {
        // Call some functions here to update stuff when settings values are loaded
    } 
}

Mod Settings

In 1.2 there are new settings variables SettingsSliderInt, SettingsSlider,SettingsTextBox, SettingsCheckBox, SettingsCheckBoxGroup. (AddButton, AddHeader, AddText, doesn't require variable)

SettingsSlider and SettingsSliderInt

Used to add slider to mod settings

SettingsSliderInt - Accepts integer values, can also show values as text from string[] array.

AddSlider(Mod mod, string settingID, string name, int minValue, int maxValue, int value = 0, Action onValueChanged = null, string[] textValues = null)

mod - your mod instance (usually this)
settingID - unique setting ID
name - Name of the slider (visible in settings)
minValue - minimum value (slider range)
maxValue - maximum value (slider range)
value - default value of slider (default is 0)
(Optional) onValueChanged - Do something when value changed (set to null if not needed)
(Optional) textValues - Array of text values (set to null if not needed)

* SettingsSlider - Accepts float values.

AddSlider(Mod mod, string settingID, string name, float minValue, float maxValue, float value = 0f, Action onValueChanged = null, int decimalPoints = 2)

similar values to SettingsSliderInt except:
(Optional) decimalPoints - how many decimal points have float value (default 2)

Usage Examples

SettingsSlider my_slider;
            
public override void ModSettings()
{
    Settings.AddHeader(this, "This is slider test");
    my_slider = Settings.AddSlider(this, "mySlider", "This is my slider", 0f, 50f, 5f);
}

Result
Float slider with range from 0 to 50 with default value as 5f
obraz

SettingsSliderInt my_slider;
string[] sliderValues = new string[4] { "Something", "Something2", "Something3", "Something4" };

public override void ModSettings()
{
    Settings.AddHeader(this, "This is slider test");
    my_slider = Settings.AddSlider(this, "myIntSlider", "This is my slider", 0, 3, 0, null, sliderValues);
}

Result
Float slider with range from 0 to 3 with default value as 0, and text values from sliderValues array
0 = "Something"
1 = "Something2"
2 = "Something3"
3 = "Something4"
obraz '

SettingsTextBox

Use to add editable textbox to settings

AddTextBox(Mod mod, string settingID, string name, string value, string placeholderText)
AddTextBox(Mod mod, string settingID, string name, string value, string placeholderText, InputField.ContentType contentType)

Note: this overloads may require you to add UnityEngine.UI.dll reference
mod - your mod instance (usually this)
settingID - unique setting ID
name - Name of the textbox (visible in settings)
value - Default text value (type string.Empty if no default value needed)
placeholderText - placeholder text shown inside textbox when value is empty (like "enter text here...")
(Optional) contentType - Unity InputField.ContentType

Usage Examples

SettingsTextBox my_textBox;

public override void ModSettings()
{
    Settings.AddHeader(this, "This is TextBox test");
    my_textBox = Settings.AddTextBox(this, "my_textBox", "This is my TextBox", string.Empty, "Enter text here...");
}

Result
TextBox without default value with placeholder "Enter text here..."
obraz

SettingsCheckBox

Used to add checkBox to mod settings

AddCheckBox(Mod mod, string settingID, string name, bool value = false, Action onValueChanged = null)

mod - your mod instance (usually this)
settingID - unique setting ID
name - Name of the checkbox (visible in settings)
value - Default value (true or false)
(Optional) onValueChanged - Do something when value changed (set to null if not needed)

Usage Examples

SettingsCheckBox my_checkBox;

public override void ModSettings()
{
    Settings.AddHeader(this, "This is CheckBox test");
    my_checkBox = Settings.AddCheckBox(this, "my_checkBox", "This is my CheckBox", false);
}

Result
CheckBox named This is my CheckBox with default value false
obraz

SettingsDropDownList

Used to add DropDown List to mod settings

AddDropDownList(Mod mod, string settingID, string name, string[] arrayOfItems, int defaultSelected = 0, Action OnSelectionChanged = null)

mod - your mod instance (usually this)
settingID - unique setting ID
name - Name of the checkbox (visible in settings)
arrayOfItems - Array of names that will be displayed in DropDownList
(Optional) defaultSelected - Selected Index by default (0 if not set)
(Optional) OnSelectionChanged - Do something when value changed (set to null if not needed)

Usage Examples

SettingsDropDownList dropDownList;

public override void ModSettings()
{
    Settings.AddHeader(this, "This is DropDownList Test");
    string[] ItemNames = new string[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10" };
    dropDownList = Settings.AddDropDownList(this, "myList", "This is my DropDownList", ItemNames);
}

Result
DropDownList with default selected item as 0.
obraz
obraz

SettingsColorPicker

Used to add Color Picker (RGB or RGBA) to mod settings

AddColorPickerRGB(Mod mod, string settingID, string name, Action OnColorChanged = null)
AddColorPickerRGBA(Mod mod, string settingID, string name, Action OnColorChanged = null)
AddColorPickerRGB(Mod mod, string settingID, string name, Color32 defaultColor, Action OnColorChanged = null)
AddColorPickerRGBA(Mod mod, string settingID, string name, Color32 defaultColor, Action OnColorChanged = null)

mod - your mod instance (usually this)
settingID - unique setting ID
name - Name of the color picker (visible in settings)
(Optional) defaultColor - Default color (0,0,0,255 [black] if not set)
(Optional) **OnColorChanged ** - Do something when color changed (set to null if not needed)

Usage Examples

This example shows both Color pickers (with alpha and without)

SettingsColorPicker colorPicker, colorPickerA;

public override void ModSettings()
{
    Settings.AddHeader(this, "This is Color Picker Test RGB");
    colorPicker = Settings.AddColorPickerRGB(this, "colorPicker", "This is my color picker", () => ModConsole.Print($"Selected color: {colorPicker.GetValue()}"));
           
    Settings.AddHeader(this, "This is Color Picker Test RGBA");
    colorPickerA = Settings.AddColorPickerRGBA(this, "colorPicker2", "This is my color picker", () => ModConsole.Print($"Selected color: {colorPickerA.GetValue()}"));

}

Result
RGB/RGBA color picker
obraz

Other Settings types

AddButton

AddButton(Mod mod, string settingID, string name, Action onClick)
AddButton(Mod mod, string settingID, string name, Action onClick, UnityEngine.Color btnColor, UnityEngine.Color buttonTextColor)

mod - your mod instance (usually this)
settingID - unique setting ID
name - Name of the Button
onClick - Function to execute when button is clicked
(Optional) btnColor - Background button color (Default new Color32(85, 38, 0, 255))
(Optional) buttonTextColor - Text Color (Default Color.white)

Usage Examples

public override void ModSettings()
{
    Settings.AddHeader(this, "This is Button test");
    Settings.AddButton(this, "my_button", "This is My Button", DoSomethingCool);
}

private void DoSomethingCool()
{
    ModUI.ShowMessage("You clicked this button", "It works!");
}

AddHeader

Adds a collapsable Header

AddHeader(Mod mod, string HeaderTitle)
AddHeader(Mod mod, string HeaderTitle, UnityEngine.Color backgroundColor)
AddHeader(Mod mod, string HeaderTitle, UnityEngine.Color backgroundColor, UnityEngine.Color textColor)

mod - your mod instance (usually this)
HeaderTitle - Title of your header
(Optional) backgroundColor - Background color of header (Default new Color32(95, 34, 18, 255))
(Optional) textColor - Text Color of header (Default new Color32(236, 229, 2, 255))

AddText

Add just a text (It supports unity rich text tags)

AddText(Mod mod, string text)

mod - your mod instance (usually this)
text - text (supports unity rich text tags)

Usage Examples

public override void ModSettings()
{
    Settings.AddHeader(this, "This is Text test");
    Settings.AddText(this, "This is text, can be any lenght, and supports unity <color=aqua>Rich text tags</color>");
}
Clone this wiki locally