internal virtual void AddSettings(ModSettingsBase modSettings) { foreach (FieldInfo field in modSettings.GetFields()) { Attributes.GetAttributes(field, out SectionAttribute section, out NameAttribute name, out DescriptionAttribute description, out SliderAttribute slider, out ChoiceAttribute choice); if (section != null) { AddHeader(section); } else if (lastHeader == null) { AddPaddingHeader(); } if (slider != null) { AddSliderSetting(modSettings, field, name, description, slider); } else if (choice != null) { AddChoiceSetting(modSettings, field, name, description, choice); } else { // No Slider or Choice annotation, determine GUI object from field type Type fieldType = field.FieldType; if (fieldType == typeof(UnityEngine.KeyCode)) { AddKeySetting(modSettings, field, name, description); } else if (fieldType.IsEnum) { AddChoiceSetting(modSettings, field, name, description, ChoiceAttribute.ForEnumType(fieldType)); } else if (fieldType == typeof(bool)) { AddChoiceSetting(modSettings, field, name, description, ChoiceAttribute.YesNoAttribute); } else if (IsFloatType(fieldType)) { AddSliderSetting(modSettings, field, name, description, SliderAttribute.DefaultFloatRange); } else if (IsIntegerType(fieldType)) { AddSliderSetting(modSettings, field, name, description, SliderAttribute.DefaultIntRange); } else { throw new ArgumentException("Unsupported field type: " + fieldType.Name); } } } }
private void AddChoiceSetting(ModSettingsBase modSettings, FieldInfo field, NameAttribute name, DescriptionAttribute description, ChoiceAttribute choice) { // Create menu item GameObject setting = CreateSetting(name, description, comboBoxPrefab, "Label"); ConsoleComboBox comboBox = setting.GetComponent <ConsoleComboBox>(); // Add selectable values comboBox.items.Clear(); foreach (string choiceName in choice.Names) { comboBox.items.Add(choiceName); } comboBox.m_Localize = choice.Localize; // Add listener and set default value EventDelegate.Set(comboBox.onChange, new Action(() => UpdateChoiceValue(modSettings, field, comboBox.GetCurrentIndex()))); modSettings.AddRefreshAction(() => UpdateChoiceComboBox(modSettings, field, comboBox)); // Control visibility SetVisibilityListener(modSettings, field, setting, lastHeader); }
internal static void GetAttributes(FieldInfo field, out SectionAttribute section, out NameAttribute name, out DescriptionAttribute description, out SliderAttribute slider, out ChoiceAttribute choice) { // Must be assigned at least once, so assign to null first section = null; name = null; description = null; slider = null; choice = null; object[] attributes = field.GetCustomAttributes(true); foreach (object attribute in attributes) { if (attribute is SectionAttribute sectionAttribute) { section = sectionAttribute; } else if (attribute is NameAttribute nameAttribute) { name = nameAttribute; } else if (attribute is DescriptionAttribute descriptionAttribute) { description = descriptionAttribute; } else if (attribute is SliderAttribute sliderAttribute) { slider = sliderAttribute; } else if (attribute is ChoiceAttribute choiceAttribute) { choice = choiceAttribute; } } }