Beispiel #1
0
    private void GenerateSettingsUIControls(SettingList settings, string parentName)
    {
        PropertyInfo[] properties = settings.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        for (int i = 0; i < properties.Length; i++)
        {
            // creating and positioning label text
            GameObject labelTextObject = Instantiate(this.textPrefab);
            labelTextObject.transform.SetParent(this.transform.Find(parentName));
            RectTransform labelTextRectTransform = labelTextObject.GetComponent <RectTransform>();
            labelTextRectTransform.anchoredPosition = new Vector2(0.0f, (i * -labelTextRectTransform.sizeDelta.y) - 20f);
            TextMeshProUGUI labelText = labelTextObject.GetComponent <TextMeshProUGUI>();
            labelText.text      = properties[i].Name;
            labelText.alignment = TextAlignmentOptions.MidlineRight;

            // choosing which UI control to create
            GameObject newUIControl = null;
            if (properties[i].PropertyType == typeof(int))
            {
                newUIControl = Instantiate(this.intFieldPrefab);
                TMP_InputField inputField = newUIControl.GetComponent <TMP_InputField>();
                inputField.text = properties[i].GetValue(settings).ToString();
            }
            else if (properties[i].PropertyType == typeof(float) || properties[i].PropertyType == typeof(double))
            {
                newUIControl = Instantiate(this.floatFieldPrefab);
                TMP_InputField inputField = newUIControl.GetComponent <TMP_InputField>();
                inputField.text = properties[i].GetValue(settings).ToString();
            }
            else if (properties[i].PropertyType == typeof(bool))
            {
                newUIControl = Instantiate(this.settingsTogglePrefab);
                Toggle toggle = newUIControl.GetComponent <Toggle>();
                toggle.isOn = (bool)properties[i].GetValue(settings);
            }
            else if (properties[i].PropertyType.IsEnum)
            {
                newUIControl = Instantiate(this.dropdownPrefab);
                TMP_Dropdown dropdown = newUIControl.GetComponent <TMP_Dropdown>();
                dropdown.AddOptions(System.Enum.GetNames(properties[i].PropertyType).ToList());
                dropdown.value = (int)properties[i].GetValue(settings);
            }
            else
            {
                Debug.Assert(false, string.Format("Unknown property type: {0}", properties[i].PropertyType.Name));
            }

            // remembering which setting this control is supposed to change, will be needed when loading settings from config
            TextProperty settingName = newUIControl.GetComponent <TextProperty>();
            settingName.Text = properties[i].Name;

            newUIControl.transform.SetParent(labelTextObject.transform, false);
            this.uiControls.Add(newUIControl);

            // setting position on canvas
            RectTransform rectTransformComponent = newUIControl.GetComponent <RectTransform>();
            rectTransformComponent.anchoredPosition += new Vector2(170.0f, 0.0f);
        }
    }
Beispiel #2
0
    /// <summary>
    /// Fills controls with values loaded from config file.
    /// </summary>
    /// <param name="settings">Settings object.</param>
    private void FillControls(SettingList settings)
    {
        PropertyInfo[] properties = settings.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        foreach (PropertyInfo property in properties)
        {
            GameObject control = (from uiControl
                                  in this.uiControls
                                  where uiControl.GetComponent <TextProperty>().Text == property.Name
                                  select uiControl).First();
            if (control == null)
            {
                continue;
            }
            switch (property.PropertyType.Name)
            {
            case nameof(Int32):
            case nameof(Single):
                control.GetComponent <TMP_InputField>().text = property.GetValue(settings).ToString();
                break;

            case nameof(Boolean):
                control.GetComponent <Toggle>().isOn = (bool)property.GetValue(settings);
                break;

            default:
                if (property.PropertyType.IsEnum)
                {
                    control.GetComponent <TMP_Dropdown>().value = (int)property.GetValue(settings);
                }
                else
                {
                    Debug.Log(string.Format("Setting type {0} is unknown", property.PropertyType.Name));
                }
                break;
            }
        }
    }
Beispiel #3
0
    /// <summary>
    /// Fills settings with values from the UI controls.
    /// </summary>
    /// <param name="settings">Settings to be filled.</param>
    private void FillSettings(SettingList settings)
    {
        PropertyInfo[] properties = settings.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        foreach (PropertyInfo property in properties)
        {
            GameObject control = (from uiControl
                                  in this.uiControls
                                  where uiControl.GetComponent <TextProperty>().Text == property.Name
                                  select uiControl).First();
            switch (property.PropertyType.Name)
            {
            case nameof(Int32):
                property.SetValue(settings, int.Parse(control.GetComponent <TMP_InputField>().text));
                break;

            case nameof(Single):
                property.SetValue(settings, float.Parse(control.GetComponent <TMP_InputField>().text));
                break;

            case nameof(Boolean):
                property.SetValue(settings, control.GetComponent <Toggle>().isOn);
                break;

            default:
                if (property.PropertyType.IsEnum)
                {
                    property.SetValue(settings, control.GetComponent <TMP_Dropdown>().value);
                }
                else
                {
                    Debug.Assert(false, string.Format("Setting type {0} is not supproted", property.PropertyType.Name));
                }
                break;
            }
        }
    }