Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by dot · Dec 22, 2010 at 04:21 PM · guislidermarkerhorizontal-slider

How can I add markers to a horizontal slider?

any idea how to make markers on a horizontal slider?

alt text

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:alt text

as it goes to the sides - it gets missplaced more and more. http://img63.imageshack.us/img63/7926/but2.png alt text

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Dec 22, 2010 at 05:11 PM 0
Share

You mean like bookmarks on a movie progress bar?

avatar image dot · Dec 22, 2010 at 05:36 PM 0
Share

exactly .

avatar image Statement · Dec 22, 2010 at 06:47 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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).


Screenshot of it in action

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dot · Dec 23, 2010 at 04:04 PM 1
Share

thumbs_up_my_dear_host();

avatar image dot · Feb 06, 2011 at 01:58 PM 1
Share

just telling you again, you're a bomb. this script is great.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dot · Dec 22, 2010 at 05:49 PM 0
Share

edited the question, as I encountered a problem, going with this method

avatar image Jesse Anders · Dec 22, 2010 at 05:54 PM 0
Share

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.)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

GUI slider and rotation not working 1 Answer

how to make a Horizontal slider & toggle in C# with GUI textures 1 Answer

GUI Sliders in the same script. Question (Js.) 1 Answer

How to make a Horizontal Slider slide between resolutions? 3 Answers

Horizontal Slider issue 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges