- Home /
How to shift GUI.Buttons while clicking on a slider
Hello there,
i've asked this before with no reply so i will rephrase my question with a bit of code as well.
So lets say i have a GUI.Box and inside this Box is 20 GUI.Buttons. On left and right side i have a GUI.RepeatButton and when i click on right button it will scroll my track selection to the right, obviously the same goes for the left button just to the left side. It's like having my tracks in a loop so i could circle around. I am not very good with words so i hope you understand what i am trying to do.
#pragma strict
var icon : Texture2D;
private var _selectionWindowRect : Rect = new Rect(150,150,560,445);
private var _selectionWindowID : int = 0;
var hSliderValue : float = 0.0;
function OnGUI () {
// GUI.Button(Rect(Screen.width/2 - x,Screen.height/2 - y, x * 2, y * 2),"button");
_selectionWindowRect = GUI.Window(_selectionWindowID, _selectionWindowRect, SelectionWindow, "Select your animal");
}
function SelectionWindow (_selectionWindowID) {
GUI.BeginGroup (Rect (0,0,560,250));
SelectGorilla();
SelectRhino();
SelectFrog();
SelectLion();
SelectOstrich();
SelectDonkey();
SelectZebra();
SelectKangaroo();
SelectVaran();
SelectCamel();
SeparationLine();
GUI.EndGroup ();
// Wrap everything in the designated GUI Area
GUILayout.BeginArea (Rect (20,250,520,200));
//Make a box so you can see where you are on the screen
GUI.Box (Rect(20,30,480,110), "");
for(var i : int = 0; i < 40; i++) {
GUI.Button (new Rect(25 + (80 * i), 45, 80, 80),"Track " + i.ToString());
}
hSliderValue = GUI.HorizontalSlider (Rect (25, 150, 470, 30), hSliderValue, 0.0, 10.0);
GUILayout.BeginVertical();
GUILayout.Box("Choose Track: " + Mathf.Round(hSliderValue));
GUILayout.EndVertical();
//Left Arrow
if (GUI.RepeatButton(Rect(0,30,15,110), "<")) {
if(hSliderValue == 0.0) {
return;
Debug.Log(" Slider is at position 0 ");
} else {
hSliderValue -= 3.0 * Time.deltaTime;
Debug.Log("Clicked the left arrow");
}
}
//Right Arrow
if (GUI.RepeatButton(Rect(505,30,15,110), ">")) {
hSliderValue += 3.0 * Time.deltaTime;
Debug.Log("Clicked the right arrow");
}
GUI.Button (Rect (235, 165, 50, 25)," Play ");
GUILayout.EndArea();
}
Answer by mohitramani · May 30, 2013 at 12:20 PM
you have mentioned what you are trying to do. could you please mention where exactly are you getting stuck? or rather elaborate your problem a little bit more?
First thing is that when Choose track is at 0 the left RepeatButton is doing nothing, it remains at value 0, only when i use the right RepeatButton and increase the value from 0 to 1 then the left button is decreasing the value in Choose track and also it is going to negative values, like -1, -2 etc. I want to have it the way that if its on 0 and i use left button then it will go to 20, 19 etc. Something like the Tracks are in the loop that is circling around. If you know what i mean.
Second thing is that i would like to have the GUI.Buttons - Track 0, Track 1 etc inside the GUI.Box (BeginArea?) so they wont be overlapping this GUI.Box like now and while i will be using my RepeatButtons on sides these Track buttons would be smoothly moving as well. Lets say i have only 6 Tracks displayed inside the GUI.Box 0 to 5 and when i press RepeatButton on left the first Track will be 20 second 0, third 1 etc.
English is not my mother tongue, so sorry if you will get a headache from reading this.
First Point : Check the comment ::
if (GUI.RepeatButton(Rect(0,30,15,110), "<")) {
if(hSliderValue == 0.0) { // it seems like hSliderValue is not exactly zero and hence it is not returning
return; // try using the condition (hSliderValue <= 0.0f);
// if you want it to go to 20 the put that code here rather than return
Debug.Log(" Slider is at position 0 ");
} else {
hSliderValue -= 3.0 * Time.deltaTime;
Debug.Log("Clicked the left arrow");
}
}
From your second point what I have understood is that you want a loopable playlist where on pressing the left button you want your track no to decrease and if it becomes less than zero it should go to track 20.
To accomplish this I would say to keep hSliderValue as reference and use that to decide what tracks are to be displayed. you could do something like :
have max value of hSliderValue = totalTracks - noOfTrackDisplayed then based on hSliderValue draw the next 6 buttons which will be displayed in the playlist. for eg if the value of hSliderValue = 5, then loop from 5 till 10 and draw the buttons.
I hope this is what you were looking for!
This was exactly what i was looking for, thank you very much. You solved my problem