Creating the settings menu

The final part of our optimizations will be to add a menu so that the player can access and change the settings we created. Create a new C# script and name it Config_GUI.

Preparing the code

Now, we'll set up our code by adding variables, a start function, and an OnGUI function. Add this code to your script:

float volBG, volSFX, volATM, fov;
bool aa, shadows, sync, optionsGUI, full;
int res;
string settings, audiotype;
public Rect optionsRect = new Rect(100, 100, 500, 500);

void Start()
{
  volBG = 0;
  volATM = 0.3f;
  volSFX = 0.8f;
  fov = 90.00f;
  aa = true;
  fullscreen = true;
  shadows = true;
  optionsGUI = true;
  LoadAll();
}

void OnGUI()
{
  if(optionsGUI)
  {
    optionsRect = GUI.Window(0, optionsRect, OptionsGUI, "Options");
  }
}

All of the variables that we created are placeholders so that we aren't directly modifying the saved values that are in PlayerPrefs. The last variable, Rect, will be used to place and size our Options menu. In the Start function, we set the placeholders to some default values and call a LoadAll function. The LoadAll function will be created later; its purpose is to load our saved data in the placeholders. Finally, the OnGUI function will run the GUI window that will hold our Options menu.

Creating the GUI

Now, we will create the function that runs the GUI. We will create labels, buttons, horizontal sliders, and toggle buttons. Add this function to your script:

void OptionsGUI(int gui)
{
  GUILayout.BeginArea(new Rect(0, 50, 800, 800));
    
  GUI.Label(new Rect(25, 0, 100, 30), "Quality Settings");
    
  if(GUI.Button(new Rect(25, 20, 75, 20), "High"))
    GetComponent<Video_Config>().SetResolution(0, 3);
  if(GUI.Button(new Rect(100, 20, 75, 20), "Medium"))
    GetComponent<Video_Config>().SetResolution(1, 3);
  if(GUI.Button(new Rect(175, 20, 75, 20), "Low"))
    GetComponent<Video_Config>().SetResolution(2, 3);
  if(GUI.Button(new Rect(250, 20, 75, 20), "Custom"))
    GetComponent<Video_Config>().SetResolution(3, 3);

  GUI.Label(new Rect(25, 40, 100, 30), "Field of View");
  fov = GUI.HorizontalSlider(new Rect(115, 45, 100, 30), fov, 60.00f, 120.00f);

  GUI.Label(new Rect(25, 60, 100, 30), "Antialiasing");
  aa = GUI.Toggle(new Rect(115, 60, 100, 30), aa, " On/Off");

  GUI.Label(new Rect(25, 75, 100, 30), "Resolution");

  if(GUI.Button(new Rect(25, 95, 75, 20), "1920x1080"))
    GetComponent<Video_Config>().SetResolution(0, 3);
  if(GUI.Button(new Rect(100, 95, 75, 20), "1600x900"))
    GetComponent<Video_Config>().SetResolution(1, 3);
  if(GUI.Button(new Rect(175, 95, 75, 20), "1280x1024"))
    GetComponent<Video_Config>().SetResolution(2, 3);
  if(GUI.Button(new Rect(250, 95, 75, 20), "1280x800"))
    GetComponent<Video_Config>().SetResolution(3, 3);
  if(GUI.Button(new Rect(325, 95, 75, 20), "640x400"))
    GetComponent<Video_Config>().SetResolution(4, 3);
    
  GUI.Label(new Rect(25, 125, 100, 30), "FullScreen");
  full = GUI.Toggle(new Rect(95, 125, 100, 30), fullscreen, " On/Off");

For each option that we have, we make a block of code creating its label and buttons for the player to see and adjust. The label is being used as a title for the option, so the player knows what they are editing. The buttons are made available to change the setting when they are clicked. Changing the fullscreen option is done with a toggle button, which is a single button that can be turned on and off like a Boolean.

To adjust the field of view, we use a slider so that the player can scroll through many options. Using the slider adds more customization as you don't need to create buttons. A slider isn't ideal for all options, but it fits perfectly to adjust the field of view. Now let's continue to create the other options:

  GUI.Label(new Rect(25, 140, 100, 30), "Shadows");
  shadows = GUI.Toggle(new Rect(95, 140, 100, 30), shadows, " On/Off");
    
  GUI.Label(new Rect(25, 160, 150, 30), "Music Volume");
  volBG = GUI.HorizontalSlider(new Rect(25, 180, 100, 30), volBG, 0.00f, 1.00f);
  GUI.Label(new Rect(25, 200, 150, 30), "SFX Volume");
  volSFX = GUI.HorizontalSlider(new Rect(25, 220, 100, 30), volSFX, 0.00f, 1.00f);
  GUI.Label(new Rect(25, 240, 150, 30), "Atmospheric Volume");
  volATM = GUI.HorizontalSlider(new Rect(25, 260, 100, 30), volATM, 0.00f, 1.00f);
    
  GUI.Label(new Rect(25, 270, 100, 30), "Speaker Type");
    
  if(GUI.Button(new Rect(25, 290, 75, 20), "Mono"))
    GetComponent<Audio_Config>().SetAudioType("Mono");
  if(GUI.Button(new Rect(100, 290, 75, 20), "Stereo"))
    GetComponent<Audio_Config>().SetAudioType("Stereo");
  if(GUI.Button(new Rect(175, 290, 75, 20), "Surround"))
    GetComponent<Audio_Config>().SetAudioType("Surround");
  if(GUI.Button(new Rect(250, 290, 100, 20), "Surround 5.1"))
    GetComponent<Audio_Config>().SetAudioType("Surround 5.1");
  if(GUI.Button(new Rect(350, 290, 100, 20), "Surround 7.1"))
    GetComponent<Audio_Config>().SetAudioType("Surround 7.1");
    
  if(GUI.Button(new Rect(25, 350, 100, 20), "Save Settings"))
    SaveAll();
  GUI.Button(new Rect(150, 350, 100, 20), "Back");

  GUILayout.EndArea();
}

There is a lot going on in this function, but it's all GUI code. To change the Quality Settings, Resolution, and Speaker Type parameters, we use buttons. When clicked, the buttons will call their associating functions for either the video or audio configuration scripts that we previously created.

To change the Field of View, Background Music volume, Sound Effects volume, and Atmospheric volume parameters, we use a horizontal slider to edit their values. For the Anti-aliasing, Fullscreen, and Shadows options, we use a toggle or radio button to switch them on or off.

Finally, we create a button that will save the settings, and another button to go back. The back button can be left open for now since it can be used to go back to the main menu or a pause menu.

Saving all the values

To save our settings, we need to create a function for it; let's do that now. Add this function to your script:

void SaveAll()
{
  PlayerPrefs.SetString("Custom_Settings", settings);

  if(shadows)
    PlayerPrefs.SetInt("Custom_Shadows", 1);
  else
    PlayerPrefs.SetInt("Custom_Shadows", 0);

  PlayerPrefs.SetFloat("Custom_FOV", fov);

  PlayerPrefs.SetInt("Custom_Resolution", res);

  PlayerPrefs.SetInt("Custom_Full", Convert.ToInt32(fullscreen));

  if(aa)
    PlayerPrefs.SetInt("Custom_AA", 1);
  else
    PlayerPrefs.SetInt("Custom_AA", 0);

  if(sync)
    PlayerPrefs.SetInt("Custom_Sync", 1);
  else
    PlayerPrefs.SetInt("Custom_Sync", 0);

  PlayerPrefs.SetFloat("atmVolume", volBG);
  PlayerPrefs.SetFloat("sfxVolume", volSFX);
  PlayerPrefs.SetFloat("bgVolume", volATM);
  PlayerPrefs.SetString("audioType", audiotype);
}

What this function will do is use the PlayerPrefs function to save all of our values. To save the settings, we use the placeholder variables that we created earlier.

Loading all the values

The last feature of the Config_GUI script will be to load all of our saved data. Add this last function to your script:

void LoadAll()
{
  volBG = PlayerPrefs.GetFloat("bgVolume");
  volSFX = PlayerPrefs.GetFloat("sfxVolume");
  volATM = PlayerPrefs.GetFloat("atmVolume");
  fov = PlayerPrefs.GetFloat("Custom_FOV");
  aa = Convert.ToBoolean(PlayerPrefs.GetInt("Custom_AA"));
  shadows = Convert.ToBoolean(PlayerPrefs.GetInt("Custom_Shadows"));
  sync = Convert.ToBoolean(PlayerPrefs.GetInt("Custom_Sync"));
  fullscreen = Convert.ToBoolean(PlayerPrefs.GetInt("Custom_Full"));
  res = PlayerPrefs.GetInt("Custom_Resolution");
  settings = PlayerPrefs.GetString("Custom_Settings");
  audiotype = PlayerPrefs.GetString("audioType");
}

For each of the placeholder variables that we created, we load the saved data for it. To get the saved data, we use the PlayerPrefs get functionality to load the values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.181.36