Skip to content

Instantly share code, notes, and snippets.

@tk009999
Created March 12, 2021 06:06
Show Gist options
  • Save tk009999/dd52f1b56ed7c79a08dd129dddb22e86 to your computer and use it in GitHub Desktop.
Save tk009999/dd52f1b56ed7c79a08dd129dddb22e86 to your computer and use it in GitHub Desktop.
namespace UnityEngine.Audio
{
public static class AudioExtensions
{
/// <summary>
///
/// </summary>
/// <param name="mixer"></param>
/// <param name="exposedName">The name of 'The Exposed to Script' variable</param>
/// <param name="value">value must be between 0 and 1</param>
public static void SetVolume(this AudioMixer mixer, string exposedName, float value)
{
mixer.SetFloat(exposedName, Mathf.Lerp(-80.0f, 0.0f, Mathf.Clamp01(value)));
}
/// <summary>
///
/// </summary>
/// <param name="mixer"></param>
/// <param name="exposedName">The name of 'The Exposed to Script' variable</param>
/// <returns></returns>
public static float GetVolume(this AudioMixer mixer, string exposedName)
{
if (mixer.GetFloat(exposedName, out float volume))
{
return Mathf.InverseLerp(-80.0f, 0.0f, volume);
}
return 0f;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment