- Home /
How can I add markers to a horizontal slider?
any idea how to make markers on a horizontal slider?
I imagine it as some triangles under some certain parts of the slider, if you click them, the hSlider will be taken to the place they are marking.
They will also be needed to be created during runtime.
so, example:
var hSlider = 0; hSlider = GUI.HorizontalSlider (Rect (20,20, 300, 30), hSlider, 0, 100);
-[you slide the slider by the slider handle] -[you're at position 37, let's say, or 0, or 63, or 89, whatever]
if(Input.GetKeyDown(KeyCode.Space)){ //place the marker -[marker is placed in the spot, directly under the slider handle, let's say 67 is right now the value of hSlider] -[you decide to keep sliding, but if you press the marker under 67, the sliderHandle (and the hSlider value, ofcourse) will be taken to 67], -you can create mayn such markers on the hSlider during runtime
any idea hwo to solve the problem? =====================================================================
=====================================================================
EDIT:
about placing buttons under the slider: var pos1 = 0; var hSlider = 0;
function OnGUI(){
hSlider = GUI.HorizontalSlider (Rect (0,0, 300, 30), hSlider, GUI_min, GUI_max);
GUI.Button(Rect(pos1,10,10,10), "H");
}
function Update(){
if(Input.GetKeyDown(KeyCode.Space)){
pos1 = hSlider-5; //half of the button's width
}
I get the button slightly missplaced.
if it's in the centre, it's allright:
as it goes to the sides - it gets missplaced more and more.
You need to make use of slider padding information found in the skin to position the buttons. See my answer, try it out even though it's C#. You should be able to port it to JS easily.
Answer by Statement · Dec 22, 2010 at 05:51 PM
Interesting question. I am not an artist so I didn't make any triangles. However, I place some buttons instead (you can override the GUI style yourself, and tweak the magic constants a bit that are related with the GUI style size for the button).
Below script in action.
using System.Collections.Generic; using UnityEngine;
public class Bookmarks : MonoBehaviour { List<float> markers = new List<float>(); float sliderValue = 0.0f; float sliderValueMax = 100.0f; Rect sliderRect = new Rect(20, 20, 500, 20);
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
markers.Add(sliderValue);
}
}
void OnGUI()
{
var sliderStyle = new GUIStyle("horizontalslider");
var extraPadding
= sliderStyle.border.horizontal
+ sliderStyle.margin.horizontal
+ sliderStyle.padding.horizontal;
var y = sliderRect.y + sliderRect.height;
foreach (var marker in markers)
{
var progress = marker / sliderValueMax;
var xMin = sliderRect.xMin;
var xMax = sliderRect.xMax - extraPadding;
var x = Mathf.Lerp(xMin, xMax, progress);
var rect = new Rect(x, y - 6, 12, 18);
if (GUI.Button(rect, string.Empty))
{
sliderValue = marker;
}
}
sliderValue = GUI.HorizontalSlider(sliderRect, sliderValue, 0,
sliderValueMax);
}
}
It should run out of the box if you place it on a game object. Make sure you match the class and file names.
Answer by Jesse Anders · Dec 22, 2010 at 05:31 PM
You could store the marker positions in a dynamic container, such as the generic List container.
When the space bar (or whatever) is pressed, add the current value of 'hSlider' to the list. (You might want to skip it if there's already an entry in the list with close to the same value.) Then, in OnGUI(), iterate over the list and create a Button() control for each entry in the list. (You'll have to map the values in the list to appropriate 'x' coordinates in GUI space.) If the corresponding button is clicked, set 'hSlider' to the value of the current entry in the list.
edited the question, as I encountered a problem, going with this method
Short answer: You'll just have to tweak things until the buttons line up visually. (I'm not sure exactly how the horizontal slider control is laid out visually with respect to the specified rect for the control, so I can't be much more specific than that. But, it shouldn't be a problem to solve this - you'll just have to adjust things a bit.)