Files
2026-02-21 16:45:37 +08:00

127 lines
3.6 KiB
C#

using System;
using UnityEngine;
public class SoundSettings : MonoBehaviour
{
public enum SoundCategory
{
GLOBAL = 0,
MUSIC = 1,
SFX = 2,
AMBIENT = 3
}
public float[] startVolumes = new float[4];
private void Start()
{
string prefix = GlobalSettings.Instance.saveManager.GetProfile(GlobalSettings.Instance.saveManager.lastUsedProfile).prefix;
if (ES2.Exists(prefix))
{
using (ES2Reader reader = ES2Reader.Create(prefix))
{
SetVolume(SoundCategory.MUSIC, SaveManager.Read(reader, "musicVolume", startVolumes[1]));
SetVolume(SoundCategory.SFX, SaveManager.Read(reader, "sfxVolume", startVolumes[2]));
SetVolume(SoundCategory.AMBIENT, SaveManager.Read(reader, "ambientVolume", startVolumes[3]));
}
}
}
public void SetVolume(SoundCategory soundCategory, float vol)
{
switch (soundCategory)
{
case SoundCategory.GLOBAL:
AudioController.SetGlobalVolume(vol);
break;
case SoundCategory.MUSIC:
AudioController.SetCategoryVolume("Music", vol);
break;
case SoundCategory.SFX:
AudioController.SetCategoryVolume("SFX", vol);
break;
case SoundCategory.AMBIENT:
AudioController.SetCategoryVolume("Ambient", vol);
AudioController.SetCategoryVolume("AmbientUnderwater", vol);
break;
}
}
public float GetVolume(SoundCategory soundCategory)
{
switch (soundCategory)
{
case SoundCategory.GLOBAL:
return AudioController.GetGlobalVolume();
case SoundCategory.MUSIC:
return AudioController.GetCategoryVolume("Music");
case SoundCategory.SFX:
return AudioController.GetCategoryVolume("SFX");
case SoundCategory.AMBIENT:
return AudioController.GetCategoryVolume("Ambient");
default:
return 0f;
}
}
public void Save()
{
string currentProfilePrefix = GlobalSettings.Instance.saveManager.GetCurrentProfilePrefix();
using (ES2Writer eS2Writer = ES2Writer.Create(currentProfilePrefix))
{
eS2Writer.Write(GetVolume(SoundCategory.MUSIC), "musicVolume");
eS2Writer.Write(GetVolume(SoundCategory.SFX), "sfxVolume");
eS2Writer.Write(GetVolume(SoundCategory.AMBIENT), "ambientVolume");
eS2Writer.Write(RadioManager.Instance.isSetToPlay, "isSetToPlay");
if (RadioManager.Instance.radioPlayer.Station != null && RadioManager.Instance.radioPlayer.Station.Name != null)
{
eS2Writer.Write(RadioManager.Instance.radioPlayer.Station.Name, "radioStationName");
}
try
{
eS2Writer.Save();
}
catch (Exception exception)
{
Debug.LogException(exception, this);
}
Debug.Log("Save SoundSettings");
}
}
public void Load()
{
string currentProfilePrefix = GlobalSettings.Instance.saveManager.GetCurrentProfilePrefix();
using (ES2Reader reader = ES2Reader.Create(currentProfilePrefix))
{
SetVolume(SoundCategory.MUSIC, SaveManager.Read(reader, "musicVolume", startVolumes[1]));
SetVolume(SoundCategory.SFX, SaveManager.Read(reader, "sfxVolume", startVolumes[2]));
SetVolume(SoundCategory.AMBIENT, SaveManager.Read(reader, "ambientVolume", startVolumes[3]));
RadioManager.Instance.isSetToPlay = SaveManager.Read(reader, "isSetToPlay", false);
RadioManager.Instance.SetStation(SaveManager.Read(reader, "radioStationName", string.Empty));
if (RadioManager.Instance.isSetToPlay)
{
RadioManager.Instance.Play();
}
else
{
RadioManager.Instance.Stop();
}
}
}
public void Reset()
{
SetVolume(SoundCategory.MUSIC, startVolumes[1]);
SetVolume(SoundCategory.SFX, startVolumes[2]);
SetVolume(SoundCategory.AMBIENT, startVolumes[3]);
RadioManager.Instance.isSetToPlay = false;
Save();
}
private void OnApplicationPause(bool pause)
{
AudioListener.pause = pause;
}
}