campfire/Assets/Scripts/AudioMaster.cs

39 lines
1.4 KiB
C#
Raw Permalink Normal View History

2020-04-19 01:12:29 +02:00
using UnityEngine;
using UnityEngine.Audio;
public class AudioMaster : MonoBehaviour {
public GameState GameState;
2020-04-19 22:09:31 +02:00
public Options Options;
2020-04-19 01:12:29 +02:00
public AudioMixer Mixer;
public AudioMixerSnapshot PlayingSnapshot;
public AudioMixerSnapshot PausedSnapshot;
public AudioMixerSnapshot GameoverSnapshot;
public float TransitionDuration;
void Update() {
float[] Weights = {
GameState.Current == State.Playing ? 1 : 0,
GameState.Current == State.Paused ? 1 : 0,
GameState.Current == State.GameOver ? 1 : 0,
};
AudioMixerSnapshot[] Snapshots = {
PlayingSnapshot,
PausedSnapshot,
GameoverSnapshot,
};
Mixer.TransitionToSnapshots(Snapshots, Weights, TransitionDuration);
2020-04-19 22:09:31 +02:00
Mixer.SetFloat("Master Volume", ConvertRatioToDecibel(Options.MasterVolume));
Mixer.SetFloat("Campfire Volume", ConvertRatioToDecibel(Options.CampfireVolume));
Mixer.SetFloat("Ambient Volume", ConvertRatioToDecibel(Options.AmbientVolume));
Mixer.SetFloat("Footstep Volume", ConvertRatioToDecibel(Options.FootstepVolume));
2020-04-21 02:10:32 +02:00
Mixer.SetFloat("Diary Volume", ConvertRatioToDecibel(Options.DiaryVolume));
2020-05-01 04:37:20 +02:00
Mixer.SetFloat("Teleport Volume", ConvertRatioToDecibel(Options.TeleportVolume));
2020-04-19 22:09:31 +02:00
}
private float ConvertRatioToDecibel(float x) {
return (Mathf.Pow(x, 0.16f) - 1) * 60f;
2020-04-19 01:12:29 +02:00
}
}