AudioManager

AudioManager#

NAMESPACE:

UnityUtils.ScriptUtils.Audio

The AudioManager is used for managing audio types for things like sfx, and music. It provides functions for calculating volume based on a volume type so you can easily play sounds at a consistent volume!

Example Usage#

using UnityEngine;
using UnityUtils.ScriptUtils.Audio;

public class ExampleScript : MonoBehaviour
{
  void Start()
  {
     // Sets the sfx volume type to 0.4 volume.
     AudioManager.SetVolume(AudioManager.VolumeType.sfx, 0.4f);

     // Modifies the music volume type by -0.1f. If music volume starts at 1f, the new volume would be 0.9f.
     AudioManager.ModifyVolume(AudioManager.VolumeType.music, -0.1f);

     // Grabs a volume types set volume.
     float exampleVolumeValue = AudioManager.GetVolume(AudioManager.VolumeType.global);

     // Multiply a volume by a volume types volume (For proper adjustments)
     float exampleVolueValue2 = AudioManager.CalculateVolumeBasedOnType(0.3f, AudioManager.VolumeType.sfx);
}

Functions#

UnityUtils.ScriptUtils.Audio.AudioManager

Holds variables/functions for other audio scripts (Such as Global volume).

Public Types

enum VolumeType#

Holds different audio types for volume calculations.

Values:

enumerator Global#
enumerator Sfx#
enumerator Music#
enumerator UI#
enumerator Custom#

Public Static Functions

float GetVolume(VolumeType volumeType)#

Gets the current volume level for the specified audio classType.

Returns:

The volume level for the specified audio classType.

void ModifyVolume(VolumeType volumeType, float volume)#

Adjusts the volume for the specified audio classType by adding the given value to its current volume.

void SetVolume(VolumeType volumeType, float volume)#

Sets the volume level for the specified audio classType to the new volume. Auto clamps to the min/max audio volume (MIN_AUDIO_VOLUME, MAX_AUDIO_VOLUME).

float CalculateVolumeBasedOnType(float volume, VolumeType volumeType)#

Returns the final volume after applying both the classType volume and the global volume (unless volumeType is VolumeType.Global).

Returns:

Proper volume level based on audio classType.

float MultiplyByGlobalVolume(float volume)#

Multiplies the specified volume by the current global volume level.

float CalculateClipLength(float clipLength, float pitch)#

Calculates the effective playback duration of an audio clip after adjusting for pitch.

Returns:

Adjusted clip length based on pitch (clipLength / pitch).

float CalculateClipPitchWithLength(float clipLength, float time)#

Calculates the pitch adjustment factor needed to play an audio clip at a specified duration.

Returns:

Pitch factor to achieve the desired playback time (clipLength * time).

float CalculatePitchVariance(float pitchVariance)#

Calculates the pitch variance to add randomness to playback.

Returns:

Random number between 1 - pitchVariance and 1 + pitchVariance.

Public Static Attributes

const float MAX_AUDIO_VOLUME = 1f#

Maximum audio volume.

const float MIN_AUDIO_VOLUME = 0f#

Minimum audio volume.

const float DEFAULT_PITCH_VARIANCE = 0.1f#

Default pitch variance used around the package.