Making audio configurations

For audio configurations, we'll set the volumes for background music, sound effects, and the atmospheric sounds. We will also be setting the speaker mode for the audio output. Let's start off by creating a new C# script and naming it Audio_Config.

Setting the values

The first function that we'll be creating will be used to set the default values for our configurations. Add this function to the script:

public void SetDefaults()
{
  SetBG(1.00f);
  SetSFX(0.80f);
  SetAtm(0.60f);
  SetAudioType("Stereo");
}

In this function, we call the functions that we'll be creating next to set the default values. For the first three functions, we set the volumes for various values. The last function sets the speaker mode to a stereo default.

Configuring the volumes

Now, we'll be adding the functionality to change the volumes. Add these functions to your script:

public void SetBG(float bgVolume)
{
  AudioSource[] audios = GameObject.FindObjectsOfType<AudioSource>();
    
  foreach(AudioSource source in audios)
  {
    source.volume = bgVolume;
  }
}
  
public void SetSFX(float sfxVolume)
{
  AudioSource[] audios = GameObject.FindObjectsOfType<AudioSource>();
    
  foreach(AudioSource source in audios)
  {
    source.volume = sfxVolume;
  }
}
  
public void SetAtm(float atmVolume)
{
  AudioSource[] audios = GameObject.FindObjectsOfType<AudioSource>();
    
  foreach(AudioSource source in audios)
  {
    source.volume = atmVolume;
  }
}

In each of the preceding functions, we pass a float variable, which will be the new volume. Next, we create an array of audio sources, which we will grab from the scene. Finally, for each of the audio sources, we assign its volume to the new volume value of our passed variable.

Setting the speaker mode

Next, we'll set the speaker mode for the audio output. This will affect how the player will hear your audio. A player who uses headphones might want to use surround sound. A player who uses speakers to hear your game may want to use the stereo sound instead. Add this function to the script:

public void SetAudioType(string SpeakerMode)
{
  switch(SpeakerMode)
  {
  case "Mono":
    AudioSettings.speakerMode = AudioSpeakerMode.Mono;
    break;
  case "Stereo":
    AudioSettings.speakerMode = AudioSpeakerMode.Stereo;
    break;
  case "Surround":
    AudioSettings.speakerMode = AudioSpeakerMode.Surround;
    break;
  case "Surround 5.1":
    AudioSettings.speakerMode = AudioSpeakerMode.Mode5point1;
    break;
  case "Surround 7.1":
    AudioSettings.speakerMode = AudioSpeakerMode.Mode7point1;
    break;
  }
}

For this function, we pass a string, which will be used in a switch statement to change the speaker mode. In this switch statement, we check for each of the speaker modes that we want to support for our game. To change the speaker mode, we assign the speakerMode variable from AudioSettings and assign it to the associating speaker mode.

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

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