private static UISlider AddSlider <T>(this UIHelperBase group, string text, string propertyName, SliderAttribute attr) { var property = typeof(T).GetProperty(propertyName); UILabel valueLabel = null; var helper = group as UIHelper; if (helper != null) { var type = typeof(UIHelper).GetField("m_Root", BindingFlags.NonPublic | BindingFlags.Instance); if (type != null) { var panel = type.GetValue(helper) as UIComponent; valueLabel = panel?.AddUIComponent <UILabel>(); } } var slider = (UISlider)group.AddSlider(text, attr.Min, attr.Max, attr.Step, (float)property.GetValue(OptionsWrapper <T> .Options, null), f => { property.SetValue(OptionsWrapper <T> .Options, f, null); OptionsWrapper <T> .SaveOptions(); attr.Action <float>().Invoke(f); if (valueLabel != null) { valueLabel.text = f.ToString(CultureInfo.InvariantCulture); } }); var nameLabel = slider.parent.Find <UILabel>("Label"); if (nameLabel != null) { nameLabel.width = nameLabel.textScale * nameLabel.font.size * nameLabel.text.Length; } if (valueLabel == null) { return(slider); } valueLabel.AlignTo(slider, UIAlignAnchor.TopLeft); valueLabel.relativePosition = new Vector3(240, 0, 0); valueLabel.text = property.GetValue(OptionsWrapper <T> .Options, null).ToString(); return(slider); }
private static UISlider AddSlider <T>(this UIHelperBase group, IOptionsWrapper <T> options, string text, string propertyName, SliderAttribute attr) { var property = typeof(T).GetProperty(propertyName); UILabel valueLabel = null; var helper = group as UIHelper; if (helper != null) { var type = typeof(UIHelper).GetField("m_Root", BindingFlags.NonPublic | BindingFlags.Instance); if (type != null) { var panel = type.GetValue(helper) as UIComponent; valueLabel = panel?.AddUIComponent <UILabel>(); } } float finalValue; var value = property.GetValue(options.GetOptions(), null); if (value is float) { finalValue = (float)value; } else if (value is byte) { finalValue = (byte)value; } else if (value is int) { finalValue = (int)value; } else { throw new Exception("Unsupported numeric type for slider!"); } var slider = (UISlider)group.AddSlider(text, attr.Min, attr.Max, attr.Step, Mathf.Clamp(finalValue, attr.Min, attr.Max), f => { if (value is float) { property.SetValue(options.GetOptions(), f, null); } else if (value is byte) { property.SetValue(options.GetOptions(), (byte)Math.Round(f, MidpointRounding.AwayFromZero), null); } else if (value is int) { property.SetValue(options.GetOptions(), (int)Math.Round(f, MidpointRounding.AwayFromZero), null); } options.SaveOptions(); attr.Action <float>().Invoke(f); if (valueLabel != null) { valueLabel.text = f.ToString(CultureInfo.InvariantCulture); } }); var nameLabel = slider.parent.Find <UILabel>("Label"); if (nameLabel != null) { nameLabel.width = nameLabel.textScale * nameLabel.font.size * nameLabel.text.Length; } if (valueLabel == null) { return(slider); } valueLabel.AlignTo(slider, UIAlignAnchor.TopLeft); valueLabel.relativePosition = new Vector3(240, 0, 0); valueLabel.text = value.ToString(); return(slider); }