117 lines
3.0 KiB
C#
117 lines
3.0 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Audio;
|
|
|
|
public enum SoundType
|
|
{
|
|
Jump,
|
|
SafeZoneEnter,
|
|
Dice,
|
|
ButtonClick
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class SoundData
|
|
{
|
|
public SoundType soundType;
|
|
public int priority;
|
|
public AudioClip soundClip;
|
|
}
|
|
|
|
public class SoundManager : MonoBehaviour, IBootLoader, IBase, IDataLoader
|
|
{
|
|
[SerializeField] AudioSource audioSource;
|
|
[SerializeField] private AudioMixer audioMixer;
|
|
[SerializeField] private SoundData[] soundDatas;
|
|
|
|
private float volTimer = 0, cachedSecVolTimer = 0;
|
|
private bool startVolTimer = false;
|
|
public bool IsGameSoundOn
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
private Dictionary<SoundType, SoundData> soundDataDict = new Dictionary<SoundType, SoundData>();
|
|
|
|
public void Initialize()
|
|
{
|
|
InterfaceManager.Instance?.RegisterInterface<SoundManager>(this);
|
|
}
|
|
|
|
public void InitializeData()
|
|
{
|
|
audioSource.priority = 0;
|
|
for (int idx = 0; idx < soundDatas.Length; idx++)
|
|
{
|
|
if (soundDataDict.ContainsKey(soundDatas[idx].soundType))
|
|
soundDataDict[soundDatas[idx].soundType] = soundDatas[idx];
|
|
else
|
|
soundDataDict.Add(soundDatas[idx].soundType, soundDatas[idx]);
|
|
}
|
|
|
|
SetMainVolume(PlayerPrefs.GetFloat("MainVol", 1f));
|
|
SetBGMVolume(PlayerPrefs.GetFloat("BGMVol", 1f));
|
|
SetSFXVolume(PlayerPrefs.GetFloat("SFXVol", 1f));
|
|
}
|
|
|
|
public void SetGameSound(bool state)
|
|
{
|
|
IsGameSoundOn = state;
|
|
}
|
|
|
|
public void PlayGameSoundClip(SoundType soundType)
|
|
{
|
|
if (!enabled) return;
|
|
|
|
SoundData soundData = soundDataDict[soundType];
|
|
|
|
audioSource.priority = soundData.priority;
|
|
audioSource.PlayOneShot(soundData.soundClip);
|
|
}
|
|
|
|
public void PlayButtonSoundClip(SoundType soundType)
|
|
{
|
|
SoundData soundData = soundDataDict[soundType];
|
|
|
|
audioSource.priority = soundData.priority;
|
|
audioSource.PlayOneShot(soundData.soundClip);
|
|
}
|
|
|
|
public void SetSFXVolume(float value)
|
|
{
|
|
audioMixer.SetFloat("SFXVol", Mathf.Log10(value) * 20);
|
|
PlayerPrefs.SetFloat("SFXVol", value);
|
|
Debug.Log($"SFX Volume set to : {audioMixer.GetFloat("SFXVol", out float vol)}");
|
|
}
|
|
|
|
public void SetBGMVolume(float value)
|
|
{
|
|
audioMixer.SetFloat("BGMVol", Mathf.Log10(value) * 20);
|
|
PlayerPrefs.SetFloat("BGMVol", value);
|
|
Debug.Log($"BGM Volume set to : {audioMixer.GetFloat("BGMVol", out float vol)}");
|
|
}
|
|
|
|
public void SetMainVolume(float value)
|
|
{
|
|
audioMixer.SetFloat("MainVol", Mathf.Log10(value) * 20);
|
|
PlayerPrefs.SetFloat("MainVol", value);
|
|
Debug.Log($"Main Volume set to : {audioMixer.GetFloat("MainVol", out float vol)}");
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!startVolTimer) return;
|
|
|
|
if (volTimer > 0)
|
|
{
|
|
volTimer -= Time.deltaTime / 4;
|
|
}
|
|
else
|
|
{
|
|
volTimer = 0;
|
|
startVolTimer = false;
|
|
}
|
|
}
|
|
}
|