- Home /
how to set the background image on slider
how can i set the backgroung image on slider..? or can i change attach material to slider and can change it on click..?? if the value of slider variable ==1 can set image 1, and if value of slider variable==2 can set image 2 on slider..is it possible,..?? i hace done this till now.,it is changing image but on click it again not cnaginh back to previous image.
public GUISkin myskin;
private float sliderValue = 1.0f;
private float maxSliderValue = 2.0f;
public GUIStyle mystyle;
public GUIStyle mystyle1;
void OnGUI(){
GUI.skin=myskin;
GUILayout.BeginVertical ();
GUILayout.Box ("sliderValue : " + Mathf.RoundToInt(sliderValue));
sliderValue = GUI.HorizontalSlider(new Rect(10,50,200,50),sliderValue ,1.0f, maxSliderValue,mystyle1,mystyle1);
if (sliderValue == 1) {
sliderValue = GUI.HorizontalSlider(new Rect(10,50,200,50),sliderValue ,1.0f, maxSliderValue,mystyle,mystyle);
}
Answer by zharik86 · May 30, 2014 at 07:59 AM
You have the first GUISkin. In it you can create styles for slider, similar to what already in it are by default. If you look at default at styles for horizontal slider, you will see there two styles (HorizontalSlider and HorizontalSlider_thumb). The first style, is slider, and the second style - a scrolling element. Each of them has normal and hover background. So, having looked at these two styles you can create the sliders.
The second if you want to change style background programmatically, for example:
//it's your skin with include styles for new slider. Include:myslider and myslider_thumb
public GUISkin mySkin = null;
public Texture2D tex1 = null; //first texture for slider
public Texture2D tex2 = null; //second texture for slider
private float sliderValue = 0.0f;
void OnGUI() {
GUI.skin = mySkin;
//Create another variable, how see changing value slider
float change = GUI.HorizontalSlider(new Rect(10, 80, 200, 50), sliderValue ,1.0f, maxSliderValue, "myslider", "myslider_thumb");
if (sliderValue != change) { //changing value
sliderValue = change;
if (sliderValue == 0.0) {
GUI.skin.GetStyle("myslider").normal.background = tex1;
} else if (SliderValue = 1.0) {
GUI.skin.GetStyle("myslider").normal.background = tex2;
}
}
}
Your answer
Follow this Question
Related Questions
Create reset button to main camera 1 Answer
How to create a UnityScript array and access the data in each cell. 1 Answer
How to handle 5 touches at a time? 2 Answers
Unity3d Update issue 2 Answers
Game/editor stops responding when going to Highscores 0 Answers