Hi there guys
Can I store horizontal sliders in an array, thus making it a dynamic “list” of sliders? I’m letting my user create them, so I don’t know how many there will be.
Thanks in advance!
Hi there guys
Can I store horizontal sliders in an array, thus making it a dynamic “list” of sliders? I’m letting my user create them, so I don’t know how many there will be.
Thanks in advance!
I would use a Dictionary<string, float> for this matter, storing the label of each slider in the key and the value of the slider in the value. Then in the OnGUI function you just iterate over each dictionary entry and draw the sliders. Of course, if you need different max/min values for each slider you might have to create a small class containing the slider properties and values, and use a list of that type.
Thanks Fox
I’m currently going for your second sugestion, but I’m just trying to think, how would I then, after placing the objects in my List, call the instantiated class - if you know what I mean? How would I get it to show the list of sliders?
Thanks again
edit: would you mind giving me a guideline if I test it first without the List, where would I instantiate my class?
sorry:face_with_spiral_eyes: is this basically what my class will look like without any properties etc of course
using UnityEngine;
using System.Collections;
public class sliderClass : MonoBehaviour {
//public static float sliderVal;
//public float position { get; set; }
void OnGUI()
{
GUI.HorizontalSlider(new Rect(450, 100, 100, 30), sliderVal, 0.0F, 10.0F);
}
}
I would do like this, just tested it. I suppose this is what you want.
public class GUITools : MonoBehaviour
{
List<Slider> Sliders = new List<Slider>();
void Start ()
{
Sliders.Add(new Slider("A", 1, 100, 10));
Sliders.Add(new Slider("B", 1, 1000, 100));
}
void OnGUI()
{
foreach (Slider slider in Sliders)
{
GUILayout.BeginHorizontal();
GUILayout.Label(slider.Label);
slider.Value = GUILayout.HorizontalSlider(
slider.Value,
slider.MinValue,
slider.MaxValue,
GUILayout.MinWidth(100));
GUILayout.Label(slider.Value.ToString());
GUILayout.EndHorizontal();
}
}
}
public class Slider
{
public float MaxValue { get; set; }
public float MinValue { get; set; }
public float Value { get; set; }
public string Label { get; set; }
public Slider(string label, float min, float max, float defaultVal)
{
Label = label;
MaxValue = max;
MinValue = min;
Value = defaultVal;
}
}
Fox
I am so, so so greatful for this!
Thank you so much!
No problem, glad to help you out