I’ve been trying to think of ways I could optimize my GUI. At first I thought you could store the floats of the rects as local variables and simply called when needed. This doesn’t really work if your GUI elements use a lot of different values that aren’t similar. Maybe you could get OnGUI to behave similar to the start function somehow? Do you guys have any suggestions for ways I could optimize my GUI code?
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
public class OptionsMenu : MonoBehaviour {
public GUISkin mySkin;
public float[] vols = new float[]{0,0};
public string[] keyButtonText = new string[]{"w","s","a","d"};
public string[] keyText = new string[]{"Move Up","Move Down","Move Left","Move Right"};
public int curResInt;
public int curKeyInt;
public bool isFullscreen;
public bool isChangingKey;
void Start(){
}
void Update(){
for(int i= 0; i < vols.Length; ++i) {
vols[i] = Mathf.RoundToInt(vols[i]);
}
if(isChangingKey){
if(Input.inputString != null){
keyButtonText[curKeyInt] = Input.inputString;
}
if(keyButtonText[curKeyInt] != ""){
isChangingKey = false;
}
}
}
void OnGUI(){
GUI.skin = mySkin;
GUI.Window(2, new Rect(Screen.width/2 - 275, Screen.height/2 - 275, 300, 550), Options, "Options");
if(isChangingKey){
GUI.Window(3, new Rect(Screen.width/2 +50, Screen.height/2 + 150, 250, 125), InputWait, "");
}
}
void Options(int windowID){
GUI.Label(new Rect(25, 30, 70, 20), "Resolution");
string[] resString = new string[]{"640x480","800x600","1024x768","1152x864"};
if (GUI.Button(new Rect(25, 50, 35, 35), "<")){
if(curResInt < resString.Length-1){
curResInt++;
}
else if(curResInt >= resString.Length-1){
curResInt = 0;
}
}
GUI.Box(new Rect(65, 50, 100, 35), resString[curResInt]);
if (GUI.Button(new Rect(170, 50, 35, 35), ">")){
if(curResInt > 0){
curResInt--;
}
else if(curResInt <= 0){
curResInt = resString.Length-1;
}
}
isFullscreen = GUI.Toggle(new Rect(25, 85, 80, 20), isFullscreen, "FullScreen");
string[] volLabelText = new string[]{"SoundFX Volume","Music Volume"};
for(int i= 0; i < 2; ++i) {
GUI.Label(new Rect(25, i*40+115, 110, 20), volLabelText[i]);
vols[i] = GUI.HorizontalSlider(new Rect(25, i*40+135, 125, 30), vols[i], 0, 100);
GUI.Box(new Rect(155, i*40+125, 35, 35),vols[i].ToString());
}
GUI.Label(new Rect(25, 200, 55, 20), "Controls");
for(int i= 0; i < 4; ++i) {
if(GUI.Button(new Rect(25, i*40+220, 35, 35), keyButtonText[i])) {
curKeyInt = i;
isChangingKey = true;
}
GUI.Label(new Rect(65, i*40+220, 45, 35), keyText[i]);
}
if (GUI.Button(new Rect(110, 495, 50, 35), "Apply")){}
}
void InputWait(int windowID){
GUI.Label(new Rect(75, 50, 100, 35), "Awaiting input");
}
}