- Home /
How to extend HorizontalSlider?
Hi!
I have a horizontal Slider with fixed width 300. The Problem is, that my SliderThumb cannot be moved to the end. It seems like only the slider texture is 300.
I attach screenshot because of my english its hard to explain the case.
As you can see on the screenshot, the volume slider cannot be moved forward to the right. It looks like its border is set up somwhere in the middle.
Im using custom GUISkin. Where can i find the option to extend the slider? Can this be made in GUISkin, or in my script?
using UnityEngine; using System.Collections; public class Options : MonoBehaviour { //these variables are only here because it is cheaper to access a value from memory instead of through a static class private float screenHeight; private float screenWidth; private float buttonHeight; private float buttonWidth; public GUISkin guiSkin; private float mySlider = 1.0f; // Use this for initialization void Start () { screenHeight = Screen.height; screenWidth = Screen.width; buttonHeight = screenHeight * 0.15f; buttonWidth = screenWidth * 0.3f; } void OnGUI() { //in order to use a delegate we just call it like a function. Simple! GUI.skin = guiSkin; options(); } float LabelSlider (Rect screenRect, float sliderValue, float sliderMaxValue, string labelText) { GUI.Label (screenRect, labelText); // <- Push the Slider to the end of the Label screenRect.x += screenRect.width; sliderValue = GUI.HorizontalSlider (screenRect, sliderValue, 0.0f, sliderMaxValue); return sliderValue; } //mainMenu only has two buttons in the version, one to play the game, and one to quit the game void options() { mySlider = LabelSlider (new Rect (480, 200, 100, 20), mySlider, 300.0f, "VOLUME"); if(GUI.Button(new Rect((screenWidth - buttonWidth) * 0.5f, screenHeight * 0.4f, buttonWidth, buttonHeight), "BACK")) { Application.LoadLevel("menu"); } } }
Im very new to Unity, so maybe its stupid question, but I will apreciate every answer.
I tried your code, it seems I don't have any issues, the sliderThumb can go till the end. Can you maybe show us where you declare mySlider and more important the GUISkin part concerning the horizontal slider ?
Thank you for you answer! Here the screenshot where i set the fixed height and width.
Here is the Slider GUISkin settings:
And here I attach Thumb GUISkin settings as well, maybe here something must be changed:
mySlider is declared at the beginning of the code, it looks pretty simple:
private float mySlider = 1.0f;
The only thing I actually changed is Fixed Width of Slider and the Fixed Width and Fixed Height of the Thumb.
Answer by Nerevar · Jun 24, 2014 at 02:44 PM
Okay, I think I got it :p
when you make your slider line 32 you set a wrong size, you have to decide whether you set the width parameter in script or in your custom skin.
two solutions:
1) Set the Slider fixed width to zero in the Custom skin
2) Retreive the fixed parameter from it and use it when you create your slider:
mySlider = LabelSlider (new Rect (480, 200, guiSkin.horizontalSlider.fixedWidth , 20), mySlider, 300.0f, "VOLUME");