-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetVolume.cs
More file actions
83 lines (70 loc) · 2.7 KB
/
SetVolume.cs
File metadata and controls
83 lines (70 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
/* SetVolume.cs
* Max Mitchell
* Last Updated: Oct. 2021
*
* Purpose: SetVolume class acts as an interface for client scripts to modify
* volume of various Sounds in an AudioManager instance. Has
* functionality for adjusting volumes, and a simple fade. Primarily
* designed for use with the Slider class, and adding volume sliders
* to a game's GUI
*/
public class SetVolume : MonoBehaviour
{
public AudioMixer mixer;
// AudioManager has three channels to start. Feel free
// to add or remove channels as needed.
public Slider Master;
public Slider Music;
public Slider SFX;
public Slider Voices;
private void Start()
{
// setup sliders based on current mixer levels
float temp;
mixer.GetFloat("MasterVolume", out temp);
Master.value = MixerToSliderValue(temp);
mixer.GetFloat("MusicVolume", out temp);
Music.value = MixerToSliderValue(temp);
mixer.GetFloat("SFXVolume", out temp);
SFX.value = MixerToSliderValue(temp);
mixer.GetFloat("VoicesVolume", out temp);
Voices.value = MixerToSliderValue(temp);
}
float MixerToSliderValue(float mixerValue)
{
// reverse process used to turn slider value into mixer value
float sliderValue = mixerValue / 20;
sliderValue = Mathf.Pow(10, sliderValue);
return sliderValue;
}
// Call these setter functions from whatever scripting component you want
// to change the audio levels. Recommended: Slider
public void SetLevel(float sliderValue) { mixer.SetFloat("MasterVolume", Mathf.Log10(sliderValue) * 20); }
public void SetMusicLevel(float sliderValue) { mixer.SetFloat("MusicVolume", Mathf.Log10(sliderValue) * 20); }
public void SetSFXLevel(float sliderValue) { mixer.SetFloat("SFXVolume", Mathf.Log10(sliderValue) * 20); }
public void SetVoicesLevel(float sliderValue) { mixer.SetFloat("VoicesVolume", Mathf.Log10(sliderValue) * 20); }
// Use to fade music to specific volume
public IEnumerator FadeMusic(float time, float goal)
{
float temp;
mixer.GetFloat("MusicVolume", out temp);
temp = MixerToSliderValue(temp);
float change = temp;
float velocity = 10.0f;
float i = time;
while (!Mathf.Approximately(change, goal))
{
change = Mathf.SmoothDamp(temp, goal, ref velocity, time / 10);
SetMusicLevel(change);
print(change);
i -= Time.deltaTime;
yield return null;
}
SetMusicLevel(goal);
}
}