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!
Tip
Use the UIAudioSlider to have an audio slider
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.
Not all of these need to be used, but these are some common types of audio’s games use. If a type is not provided that you need, you can use the custom types or create an issue on the github for this package to add more types.
Values:
-
enumerator Global#
Special audio type, everything gets multiplied by this volume, except for other global volumes.
-
enumerator SFX#
A general audio type used for sound effects. Other audio types like Weather or Footsteps are not affected by this.
-
enumerator Music#
A general audio type used for music.
-
enumerator UI#
-
enumerator Weather#
-
enumerator Dialog#
-
enumerator Footsteps#
-
enumerator Ambient#
-
enumerator Player#
-
enumerator Custom1#
-
enumerator Custom2#
-
enumerator Custom3#
-
enumerator Global#
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, float pitch)#
Calculates the pitch variance to add randomness to playback.
- Returns:
Random number between 1 - pitchVariance and 1 + pitchVariance.
-
enum VolumeType#