How to render mod options (UMM)

From Nexus Mods Wiki
Revision as of 14:43, 11 July 2019 by Newman55 (talk | contribs) (Created page with " == Functions  == Use class ''GUILayout ''and ''UnityModManager.UI''. Below is a list of additional functions for the GUI render. These functions are recommended to wrap...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Functions 

Use class GUILayout and UnityModManager.UI. Below is a list of additional functions for the GUI render. These functions are recommended to wrap in GUILayout.BeginHorizontal() and GUILayout.EndHorizontal().

GUILayout.Label(string);
GUILayout.TextField(string);
GUILayout.TextArea(string);
GUILayout.Toggle(string)
GUILayout.HorizontalSlider(float, float, float);
UnityModManager.UI.DrawVector(ref Vector);
UnityModManager.UI.DrawColor(ref Color);
UnityModManager.UI.DrawFloatField(ref float, string);
UnityModManager.UI.DrawIntField(ref int, string);
UnityModManager.UI.ToggleGroup(ref int, string[]);
UnityModManager.UI.PopupToggleGroup(ref int, string[]);
UnityModManager.UI.DrawKeybinding(ref Keybinding);
UnityModManager.UI.DrawFields<t>(ref T, ModEntry, DrawFieldMask, Action);</t>

Settings

I will show with examples.

The Draw attribute allows to set some properties for render field. IDrawable.Draw() method only sees fields with this attribute.

public class Settings : UnityModManager.ModSettings, IDrawable
{
    [Draw("HD Models")] public bool EnableHDModels = true;
    [Draw("Settings", Collapsible = true)] public ModelsSettings ModelsSettings = new ModelsSettings();

    [Draw("Subpixel Morphological Anti-Aliasing"), Space(10)] public bool SMAAEnable = true;
    [Draw("Quality")] public SMAA.QualityPreset SMAAQuality = SMAA.QualityPreset.High;

    public override void Save(UnityModManager.ModEntry modEntry)
    {
        Save(this, modEntry);
    }

    public void OnChange()
    {
    }
}
Add DrawFields attribute if you need to render the fields not only with the Draw attribute.
[DrawFields(DrawFieldMask.Public)]
public class ModelsSettings
{
    public WheelsRotate WheelsRotate = WheelsRotate.SchemeA;
    public float VisibleDistance = 300f;
    public bool WithDrivers;
}
If changed at least one value, the OnChange method will be called.
static class Main
{
    public static Settings settings;

    static bool Load(UnityModManager.ModEntry modEntry)
    {
        settings = Settings.Load<settings>(modEntry);
        modEntry.OnGUI = OnGUI;
        modEntry.OnSaveGUI = OnSaveGUI;

        return true;
    }

    static void OnGUI(UnityModManager.ModEntry modEntry)
    {
        settings.Draw(modEntry);
    }

    static void OnSaveGUI(UnityModManager.ModEntry modEntry)
    {
        settings.Save(modEntry);
    }
}

Result

options-1.png

 


Next example.

For float numbers you can set precision. The default is 2.

public class SpeedSet
{
    [Header("Frontend")]
    [Draw("1", Precision = 1, Min = 0), Space(5)] public float Frontend = 1f;
    [Header("Simulation"), Space(5)]
    [Draw("1", Precision = 0, Min = 0)] public float Simulation1 = 20f;
    [Draw("2", Precision = 0, Min = 0)] public float Simulation2 = 30f;
    [Draw("3", Precision = 0, Min = 0)] public float Simulation3 = 40f;
    [Header("OnTrack"), Space(5)]
    [Draw("1", Precision = 2, Min = 0)] public float Race1 = 1f;
    [Draw("2", Precision = 1, Min = 0)] public float Race2 = 2f;
    [Draw("3", Precision = 1, Min = 0)] public float Race3 = 4f;

    public static SpeedSet Fast()
    {
        return new SpeedSet { Frontend = 2f, Race1 = 2f, Race2 = 4f, Race3 = 8f, Simulation1 = 30f, Simulation2 = 45f, Simulation3 = 60f };
    }
}
Pointing DrawType argument in the Draw attribute you can choose how to render field. All DrawType values: Auto, Ignore, Field, Slider, Toggle, ToggleGroup, PopupList, KeyBinding.
public enum SpeedSets { Fast, Slow };

public class Settings : UnityModManager.ModSettings, IDrawable
{
    [Draw(DrawType.ToggleGroup)] public SpeedSets Preset = SpeedSets.Fast;
    [Draw("", VisibleOn = "Preset|Fast")] public SpeedSet Fast = SpeedSet.Fast();
    [Draw("", VisibleOn = "Preset|Slow")] public SpeedSet Slow = new SpeedSet();

    public void OnChange()
    {
        Main.ApplySettings(Preset);
    }

    public override void Save(UnityModManager.ModEntry modEntry)
    {
        Save(this, modEntry);
    }
}
UnityModManager doesn't interact with ModSettings, so you need to remember to save it.
static class Main
{
    public static Settings settings;

    static bool Load(UnityModManager.ModEntry modEntry)
    {
        settings = Settings.Load<settings>(modEntry);
        modEntry.OnGUI = OnGUI;
        modEntry.OnSaveGUI = OnSaveGUI;

        return true;
    }
    
    static void OnGUI(UnityModManager.ModEntry modEntry)
    {
        settings.Draw(modEntry);
    }
    
    static void OnSaveGUI(UnityModManager.ModEntry modEntry)
    {
        settings.Save(modEntry);
    }
}

Result

options-2.png

 


All supported field types: bool, int, long, float, double, int[], long[], float[], double[], string, Vector2, Vector3, Vector4, Color, KeyBinding, Enum (w/o flags). If some field type does not support DrawType, will be thrown an exception.

If you need several separate configuration files, implement the GetPath method for UnityModManager.ModSettings

public class Settings : UnityModManager.ModSettings
{
    public override string GetPath(UnityModManager.ModEntry modEntry)
    {
        return Path.Combine(modEntry.Path, "OtherSettings.xml");
    }
}

Some examples can be found here