Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Make a slider bar in Unity

Discussion in 'Scripting' started by rahul_, May 6, 2014.

  1. rahul_

    rahul_

    Joined:
    Jul 25, 2013
    Posts:
    24
    Hi,

    I want to build a short simple scene in which there'll be a slider bar (can be a 3D mesh of a semi-circular shape that is being looked by the camera from the middle of Top and Front position).

    The user has to be able to use the mouse to drag a handle on the bar and change the values that'll effect another element. That bar is actually just meant to rotate the 3D model when the user drags the handle form the beginning tip the end. I'm aware of handling the rotation and can achieve the effect using the inbuilt GUI element in Unity but I'm looking to build a better looking bar (and as mentioned above of a different shape than the default) and was looking for steps on how to proceed with the same. I've looked up Asset Store and there aren't any readily available such elements that I could find and other tutorials links point to using the inbuilt GUI slider bar.

    Thanks for help in advance!
     
  2. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    For a custom slide bar, you can use GUITextures : you create a GUITexture, make it as your bar, you create a second one, make it as the slide button. Then you just have to use OnMouseDrag in the script attached to the slide button. In the OnMouseDrag function you just have to adjust the pixel inset, with value you get from Input.mousePosition.y or .x, depending if your slide bar is vertical or horizontal.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    I'm going to amend that a bit. GUITextures are a very old, almost legacy class at this point. Using Sprites for this sort of thing is a better habit to get into.
     
  4. rahul_

    rahul_

    Joined:
    Jul 25, 2013
    Posts:
    24
    Thanks for the response. Will I be able to get a semi-circular/circular slider bar using the above technique? For e.g.: Consider the bar of the following shape (being seen by the camera from middle of top and front camera position)

    $MtroGv2.png

    I checked out some other threads and was of the view that such GUI elements will only be stuck to the screen and won't have this kind of flexibility as with 3D elements.


    Same question as above to SmokyZebra. Will GUITextures/Sprites allow this kind of flexibility? What I'd like to do is move a slider handle over a bar and if the Sprites/Textures won't be able to give the above sort of appearance, it'd interesting to know of some links where a similar implementation of a 3D slider bar can be done.
     
  5. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    It's possible to do a bar as you shown, but it will be much more complicated to script than a regular vertical/horizontal bar.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    It's not a LOT harder. You'd mostly have to adjust the way it calculates the position - add in some Mathf.Sin to figure the Y position and some Mathf.Cos for the X position.

    Well, if your math skills are not great, I suppose that would be a lot harder...
     
  7. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Or, you can add a member variable of type AnimationCurve, which has your desired curve of the slider , then evaluate the actual slider objects position based on that curve.

    No more manual interpolation!