- Home /
Question by
Fictional · Oct 08, 2014 at 12:11 PM ·
menuquality settings
When Active Toggle X
Hi,
I try to make a quality settings menu with GUI Toggle. Here my script:
//FASTEST
if (fastestQuality) {
if (GUI.Toggle(new Rect(10, 10, 100, 30), fastestQuality, "Fastest")) {
fastestQuality= true;
goodQuality = false;
fantasticQuality = false;
UpdateQuality();
}
if (GUI.Toggle(new Rect(10, 50, 100, 30), goodQuality , "Good")) {
goodQuality = true;
fantasticQuality = false;
fastestQuality = false;
UpdateQuality();
}
if (GUI.Toggle(new Rect(10, 90, 100, 30), fantasticQuality , "Fantastic")) {
fantasticQuality = true;
goodQuality = false;
fastestQuality = false;
UpdateQuality();
}
}
//GOOD
if (goodQuality ) {
if (GUI.Toggle(new Rect(10, 10, 100, 30), fastestQuality, "Fastest")) {
fastestQuality = true;
fantasticQuality = false;
goodQuality = false;
UpdateQuality();
}
if (GUI.Toggle(new Rect(10, 50, 100, 30), goodQuality , "Good")) {
goodQuality = true;
fantasticQuality = false;
fastestQuality = false;
UpdateQuality();
}
if (GUI.Toggle(new Rect(10, 90, 100, 30), fantasticQuality , "Fantastic")) {
fantasticQuality = true;
fastestQuality = false;
goodQuality = false;
UpdateQuality();
}
}
//FANTASTIC
if (fantasticQuality ) {
if (GUI.Toggle(new Rect(10, 10, 100, 30), fastestQuality, "Fastest")) {
fastestQuality = true;
goodQuality = false;
fantasticQuality = false;
UpdateQuality();
}
if (GUI.Toggle(new Rect(10, 50, 100, 30), goodQuality , "Good")) {
goodQuality = true;
fastestQuality = false;
fantasticQuality = false;
UpdateQuality();
}
if (GUI.Toggle(new Rect(10, 90, 100, 30), fantasticQuality , "Fantastic")) {
fantasticQuality = true;
fastestQuality = false;
goodQuality = false;
UpdateQuality();
}
}
My script works perfectly but too long. Is there any simple(shorter) way to do this?
Thanks a lot.
Comment
Am i missing something? The code is the same other then the if statements, whats is the point of the if statements? The bools won't do anything, why not just remove them? The wording isn't changing, the location of the Rect's aren't changing. I mean shouldn't this work:
if (GUI.Toggle(new Rect(10, 10, 100, 30), fastestQuality, "Fastest")) {
fastestQuality = true;
goodQuality = false;
fantasticQuality = false;
UpdateQuality();
}
if (GUI.Toggle(new Rect(10, 50, 100, 30), goodQuality , "Good")) {
goodQuality = true;
fastestQuality = false;
fantasticQuality = false;
UpdateQuality();
}
if (GUI.Toggle(new Rect(10, 90, 100, 30), fantasticQuality , "Fantastic")) {
fantasticQuality = true;
fastestQuality = false;
goodQuality = false;
UpdateQuality();
}
Answer by VesuvianPrime · Oct 08, 2014 at 12:38 PM
I would very much reccomend using an enum for your quality settings:
public enum Quality
{
Fastest,
Good,
Fantastic
}
private Quality m_Quality = Quality.Good;
public Quality quality
{
get {return m_Quality;}
set
{
if (value == m_Quality)
return;
m_Quality = value;
UpdateQuality();
}
}
...
if (GUI.Toggle(new Rect(10, 10, 100, 30), m_Quality == Quality.Fastest, "Fastest"))
quality = Quality.Fastest;
if (GUI.Toggle(new Rect(10, 50, 100, 30), m_Quality == Quality.Good, "Good"))
quality = Quality.Good;
if (GUI.Toggle(new Rect(10, 90, 100, 30), m_Quality == Quality.Fantastic, "Fantastic"))
quality = Quality.Fantastic;
Also, looks like you could get fancy, skip the enum entirely, and automate your quality menu: http://docs.unity3d.com/ScriptReference/QualitySettings-names.html
Your answer