How do i create an options menu, where when i change the volume from there it changes the level through out all my scenes and levels and i want to create an option to change the resolution as well and other things.
Here is a code which all you need is to copy and paste it in c# file amd you will have a menu then attach this script to the Main Camera in the scene and you can pick if you want a background image to add to it:
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour
{
public GUISkin guiSkin;
public Texture2D background, LOGO;
public bool DragWindow = false;
public string levelToLoadWhenClickedPlay = "";
public string[] AboutTextLines = new string[0];
private string clicked = "", MessageDisplayOnAbout = "About
";
private Rect WindowRect = new Rect((Screen.width / 2) - 100, Screen.height / 2, 200, 200);
private float volume = 1.0f;
private void Start()
{
for (int x = 0; x < AboutTextLines.Length;x++ )
{
MessageDisplayOnAbout += AboutTextLines[x] + "
";
}
MessageDisplayOnAbout += “Press Esc To Go Back”;
}
private void OnGUI()
{
if (background != null)
GUI.DrawTexture(new Rect(0,0,Screen.width , Screen.height),background);
if (LOGO != null && clicked != "about")
GUI.DrawTexture(new Rect((Screen.width / 2) - 100, 30, 200, 200), LOGO);
GUI.skin = guiSkin;
if (clicked == "")
{
WindowRect = GUI.Window(0, WindowRect, menuFunc, "Main Menu");
}
else if (clicked == "options")
{
WindowRect = GUI.Window(1, WindowRect, optionsFunc, "Options");
}
else if (clicked == "about")
{
GUI.Box(new Rect (0,0,Screen.width,Screen.height), MessageDisplayOnAbout);
}else if (clicked == "resolution")
{
GUILayout.BeginVertical();
for (int x = 0; x < Screen.resolutions.Length;x++ )
{
if (GUILayout.Button(Screen.resolutions[x].width + "X" + Screen.resolutions[x].height))
{
Screen.SetResolution(Screen.resolutions[x].width,Screen.resolutions[x].height,true);
}
}
GUILayout.EndVertical();
GUILayout.BeginHorizontal();
if (GUILayout.Button("Back"))
{
clicked = "options";
}
GUILayout.EndHorizontal();
}
}
private void optionsFunc(int id)
{
if (GUILayout.Button("Resolution"))
{
clicked = "resolution";
}
GUILayout.Box("Volume");
volume = GUILayout.HorizontalSlider(volume ,0.0f,1.0f);
AudioListener.volume = volume;
if (GUILayout.Button("Back"))
{
clicked = "";
}
if (DragWindow)
GUI.DragWindow(new Rect (0,0,Screen.width,Screen.height));
}
private void menuFunc(int id)
{
//buttons
if (GUILayout.Button("Play Game"))
{
//play game is clicked
Application.LoadLevel(levelToLoadWhenClickedPlay);
}
if (GUILayout.Button("Options"))
{
clicked = "options";
}
if (GUILayout.Button("About"))
{
clicked = "about";
}
if (GUILayout.Button("Quit Game"))
{
Application.Quit();
}
if (DragWindow)
GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
}
private void Update()
{
if (clicked == "about" && Input.GetKey (KeyCode.Escape))
clicked = "";
}
}
thanks , but i think it is bad for beginner , maybe you agree me or not , beginner do not need difficult code like this, if you have easy and simple ,it will be perfect , thanks you anyway ,good luck