Created
January 29, 2019 13:50
-
-
Save HassakuTb/5e4cc02c0a5be0bbfd2f25766b78b9b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Audio; | |
using UnityEngine.UI; | |
namespace Citrus.Sound { | |
[RequireComponent(typeof(Slider))] | |
public class MixerVolumeSlider : MonoBehaviour{ | |
[SerializeField] private AudioMixer audioMixer; | |
[SerializeField] private string volumeExposeParameter; | |
private Slider slider; | |
private void Awake() | |
{ | |
slider = GetComponent<Slider>(); | |
} | |
private void Start() | |
{ | |
slider.minValue = 0f; | |
slider.maxValue = 1f; | |
slider.onValueChanged.AddListener(SetVolume); | |
} | |
/// <summary> | |
/// ボリュームを変更する | |
/// </summary> | |
/// <param name="value">[0, 1]</param> | |
private void SetVolume(float value) | |
{ | |
float db; | |
if (value < 0.0001f) | |
{ | |
db = -80f; | |
} | |
else | |
{ | |
// db変換 | |
db = 20 * Mathf.Log10(Mathf.Clamp(value, 0.0001f, 1.0f)); | |
db = Mathf.Clamp(db, -80f, 0f); | |
} | |
audioMixer.SetFloat(volumeExposeParameter, db); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ミキサーのボリュームのパラメータをExposeしないと使えない