I m a beginner and I need help with a panel
Could someone describe me how to make a setting panel or an window for my main menu spawned by a button click and disabled by clicking on the sides of the panrl. pls step by step and in an easy english
Comment
Answer by nathanthesnooper · Mar 18, 2017 at 04:07 AM
public GameObject panel;
public GameObject[] panelParameters;
public void TogglePanel () {
//
// This will make the panel hidden if it was previously on, and vice versa.
//
// The function will be called from an X button in the panel, and on a button to show the panel, Ex: a "Settings" Button.
//
if (!panel.activeInHierarchy) {
LoadPanel ();
}
panel.SetActive (!panel.activeInHierarchy);
}
public void LoadPanel () {
//
// This function should be called in the start function, or it can be called when the settings menu is opened,
// meaning on the TogglePanel() function where panel.activeInHierarchy is false;
//
for (int i = 0; i < panelParameters.Length; i++) {
GameObject parameter = panelParameters [i];
if (parameter.GetComponent<InputField> () != null) {
parameter.GetComponent<InputField> ().text = PlayerPrefs.GetString ("panelParameter_" + i);
}
if (parameter.GetComponent<Toggle> () != null) {
parameter.GetComponent<Toggle> ().isOn = intToBool(PlayerPrefs.GetInt ("panelParameter_" + i));
}
if (parameter.GetComponent<Dropdown> () != null) {
parameter.GetComponent<Dropdown> ().value = PlayerPrefs.GetInt ("panelParameter_" + i);
}
}
}
public void SavePanel () {
//
// This function should be called each frame, or for better performance every time you change a value in
// the settings. Can be done using the OnValueChanged() on most Buttons, InputFields, Toggles, and Dropdowns.
//
for (int i = 0; i < panelParameters.Length; i++) {
GameObject parameter = panelParameters [i];
if (parameter.GetComponent<InputField> () != null) {
PlayerPrefs.SetString ("panelParameter_" + i, parameter.GetComponent<InputField> ().text);
}
if (parameter.GetComponent<Toggle> () != null) {
PlayerPrefs.SetInt ("panelParameter_" + i, boolToInt(parameter.GetComponent<Toggle> ().isOn));
}
if (parameter.GetComponent<Dropdown> () != null) {
PlayerPrefs.SetInt ("panelParameter_" + i, parameter.GetComponent<Dropdown> ().value);
}
}
}
public int boolToInt (bool input) {
//
// Returns an integer (1/0) based on a bool (true/false) input; Due to the fact that there is no
// PlayerPrefs.Set/GetBool();
//
if (input) {
return 1;
} else {
return 0;
}
}
public bool intToBool (int input) {
//
// Returns a bool (true/false) based on an integer (1/0) input; Due to the fact that there is no
// PlayerPrefs.Set/GetBool();
//
if (input == 1) {
return true;
} else {
return false;
}
}
Final thoughts:
-This took me WAY too long to do :)
-Please fix basic compiler errors if there is some... :)
-Use a LayoutGroup on the panel, so that all of the parameters are lined up nicely
-You can set panelParameters from panel's children, but it is more resource intensive and takes more time in the long run.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113404im_/https://answers.unity.com/themes/thub/images/avi.jpg)