- Home /
GUI.Window not displaying Buttons
UPDATE : I've updated the script once again. Also, I tested a few things, and I put the hSlider right under where I define what the selectedToolbar is. It appeared when it was there... Any ideas?
using UnityEngine; using System.Collections;
public class Options : MonoBehaviour {
private Rect windowRect = new Rect(200, 200, 1200, 500); private int selectedToolbar = 0; private string[] toolbarStrings = { "Quality", "Sound", "Game Size", "Menu" }; public float hSliderValue = 0.0f; private bool IsOptionsAvailable = false; private int MyCurrentRes; void Start() { } void Update() { if (Input.GetKeyDown(KeyCode.O) && IsOptionsAvailable == false) { IsOptionsAvailable = true; Debug.Log ("Show Options"); } else if (Input.GetKeyDown(KeyCode.O) && IsOptionsAvailable == true) { IsOptionsAvailable = false; Debug.Log("Hide Options"); } } void OnGUI() { if (IsOptionsAvailable) { windowRect = GUI.Window(0, windowRect, WindowFunction, "Options"); } } void WindowFunction(int windowID) { GUILayout.BeginHorizontal(); selectedToolbar = GUILayout.Toolbar(selectedToolbar, toolbarStrings); if (GUI.changed) { Debug.Log("Toolbar was clicked"); if (selectedToolbar == 0) { Debug.Log("Graphics selected"); GUILayout.BeginVertical(); string[] names = QualitySettings.names; int i = 0; while (i < names.Length) { if (GUILayout.Button(names[i])) QualitySettings.SetQualityLevel(i, true); i++; } GUILayout.EndVertical(); } if (selectedToolbar == 2) { GUILayout.BeginVertical(); Debug.Log("Game size selected"); if (GUILayout.Button("Fullscreen Mode")) { MyCurrentRes = QualitySettings.GetQualityLevel(); Screen.SetResolution(1680, 1050, true); QualitySettings.currentLevel = QualityLevel.Fantastic; QualitySettings.SetQualityLevel(MyCurrentRes, true); Debug.Log("Fullscreen"); } else if (GUILayout.Button("Window Mode")) { MyCurrentRes = QualitySettings.GetQualityLevel(); Screen.fullScreen = false; QualitySettings.currentLevel = QualityLevel.Fantastic; QualitySettings.SetQualityLevel(MyCurrentRes, true); Debug.Log("Window"); } Resolution[] resolutions = Screen.resolutions; foreach (Resolution res in Screen.resolutions) { if (GUILayout.Button(res.width.ToString() + " x " + res.height.ToString())) { MyCurrentRes = QualitySettings.GetQualityLevel(); Screen.SetResolution(res.width, res.height, false); QualitySettings.currentLevel = QualityLevel.Fantastic; QualitySettings.SetQualityLevel(MyCurrentRes, true); Debug.Log(res.width.ToString() + " x " + res.height.ToString()); } } GUILayout.EndVertical(); } if (selectedToolbar == 3) { Debug.Log("Go to Main Menu"); Application.LoadLevel("MainMenu"); } if (selectedToolbar == 1) { Debug.Log("Change Volume"); GUI.Box(new Rect(140, 140, 200, 50), "Volume"); hSliderValue = GUI.HorizontalSlider(new Rect(40, 40, 600, 200), hSliderValue, 0.0f, 10.0f); AudioListener.volume = hSliderValue; } } GUILayout.EndHorizontal(); GUI.DragWindow(); } }
//OLD Question :I have a script that permits me to change the runtime qualitysettings and screen resolution. However, whenever I build the game, sometimes when I change the resolution the screen becomes black, or the camera gets uncalibrated (and mouse) and i have to select another quality setting to calibrate it. Any suggestions what this might be?
Answer by spiralfire11 · Jan 19, 2016 at 07:15 PM
I fixed the problem, just completely re-arranged my script without the usage of a toolbar :
using UnityEngine; using System.Collections;
public class Options : MonoBehaviour { //Les options de GUI utilisent tous les commandes de UnirtEngine.GUI;
private Rect windowRect = new Rect(Screen.width*0.2f, Screen.height*0.2f, 1200, 600);
private int selectedToolbar = 0;
public float hSliderValue = 0.0f;
private bool IsOptionsAvailable = false;
private int MyCurrentRes;
private float MyCurrentSound;
private Color guiColor;
void Start()//On vas chercher les components comme le son afin de pouvoir le changer
{
hSliderValue = AudioListener.volume;
guiColor = GUI.color;
}
void Update()// Si on tappe la touche O opur options, on vas pouvoir ouvrir le menu d'options
{
if (Input.GetKeyDown(KeyCode.O) && IsOptionsAvailable == false)
{
IsOptionsAvailable = true;
Debug.Log ("Show Options");
}
else if (Input.GetKeyDown(KeyCode.O) && IsOptionsAvailable == true)
{
IsOptionsAvailable = false;
Debug.Log("Hide Options");
}
}
void OnGUI()
{
if (IsOptionsAvailable)
{
windowRect = GUI.Window(0, windowRect, WindowFunction, "Options");
}
}
void WindowFunction(int windowID)
{
GUILayout.BeginArea(new Rect(10, 10, 250, windowRect.height));// On commence une section qui nous permet de changer la qualité du jeux
GUILayout.BeginVertical();
GUILayout.Button("Graphics");
GUI.backgroundColor = Color.green;
string[] names = QualitySettings.names;
int i = 0;
while (i < names.Length)
{
if (GUILayout.Button(names[i]))
QualitySettings.SetQualityLevel(i, true);
i++;
}
GUILayout.EndVertical();
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(265, 10, 250, windowRect.height));// On commence une section qui nous permet de changer la résolution du jeux
GUILayout.BeginVertical();
GUI.backgroundColor = guiColor;
GUILayout.Button("Game Size");
GUI.backgroundColor = Color.red;
if (GUILayout.Button("Fullscreen Mode"))
{
MyCurrentRes = QualitySettings.GetQualityLevel();
QualitySettings.SetQualityLevel(6, true);
Screen.SetResolution(1680, 1050, true);
QualitySettings.SetQualityLevel(MyCurrentRes, true);
Debug.Log("Fullscreen");
}
else if (GUILayout.Button("Window Mode"))
{
MyCurrentRes = QualitySettings.GetQualityLevel();
QualitySettings.SetQualityLevel(6, true);
Screen.fullScreen = false;
QualitySettings.SetQualityLevel(MyCurrentRes, true);
Debug.Log("Window");
}
Resolution[] resolutions = Screen.resolutions;
foreach (Resolution res in Screen.resolutions)
{
if (GUILayout.Button(res.width.ToString()
+ " x " + res.height.ToString()))
{
MyCurrentRes = QualitySettings.GetQualityLevel();
QualitySettings.SetQualityLevel(6, true);
Screen.SetResolution(res.width, res.height, false);
QualitySettings.SetQualityLevel(MyCurrentRes, true);
Debug.Log(res.width.ToString() + " x "
+ res.height.ToString());
}
}
GUILayout.EndVertical();
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(520, 10, 250, windowRect.height));// On commence une section qui nous permet de retourner au menu/recommencer le jeux *PAS RECOMMENDER*
GUILayout.BeginVertical();
GUI.backgroundColor = guiColor;
if (GUILayout.Button("Go to Menu"))
{
Application.LoadLevel("OpeningMenu");
}
if (GUILayout.Button("Restart Game"))
{
Application.LoadLevel(0);
}
GUILayout.EndVertical();
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(775, 10, 250, windowRect.height));
GUILayout.BeginVertical();
GUILayout.Button("Volume");
GUI.backgroundColor = Color.blue;
hSliderValue = GUILayout.HorizontalSlider(hSliderValue,
0.0f, 10.0f); // On commence une section qui nous permet de changer le volume
GUILayout.EndVertical();
AudioListener.volume = hSliderValue;
GUILayout.EndArea();
GUI.DragWindow();
} }
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
If health = 0, show a GUI.button C# 2 Answers