Hi,
I want my game have a “Setting” button that when click it, it can control the volume of the music and there is also a button to quit the game.
Now I have a problem. When I click the “Setting” button, it doesn’t work.
Why?? Please help me
This is my code:
var mvalue : float = 0.8;
function OnGUI () {
if(GUI.Button(new Rect(120, 240, 50, 50), "Setting")){
var windowRect = GUI.Window(0, Rect((Screen.width/2),(Screen.height/2), 150, 100), WindowFunction, "Setting");
}
}
function WindowFunction (windowID : int) {
mvalue = GUI.HorizontalSlider(Rect((Screen.width/2),(Screen.height/2),100,30), mvalue, 0.0, 1.0);
audio.volume = mvalue;
if(GUI.Button(new Rect(180, 280, 100, 25), "Quit Game")){
Application.Quit();
}
}
You need a boolean show variable. In your code, the window opens up only when the button is pressed, and closed right in the next OnGUI call.
var mvalue : float = 0.8;
var windowRect : Rect;
var show : boolean = false;
function OnGUI () {
if(GUI.Button(new Rect(120, 240, 50, 50), "Setting")){
show = !show;
}
if(show)
var windowRect = GUI.Window(0, Rect((Screen.width/2),(Screen.height/2), 150, 100), WindowFunction, "Setting");
}
function WindowFunction (windowID : int) {
mvalue = GUI.HorizontalSlider(Rect(10, 20,100,30), mvalue, 0.0, 1.0);
audio.volume = mvalue;
if(GUI.Button(new Rect(10, 50, 100, 25), "Quit Game")){
Application.Quit();
}
}
Update: In the window function, you should use local positioning. For exampe, if you want to put the content in the middle of the window you should use windowRect.width/2 instead of Screen.width/2