using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class UISound : MonoBehaviour
{
public AudioSource backgroundAudioSource;
public AudioSource effectAudioSource;
public Slider backgroundSoundSlider;
[SerializeField] private TMP_Text backgroundSoundAmount;
public Slider effectSoundSlider;
[SerializeField] private TMP_Text effectSoundAmount;
private int backgroundSoundValue;
private int effectSoundValue;
private IEnumerator CoPlay;
private IEnumerator CoEffect;
void Start()
{
// 슬라이더 값 로드
LoadSoundSettings();
backgroundSoundSlider.onValueChanged.AddListener(delegate { UpdateBackgroundSoundAmount(); });
effectSoundSlider.onValueChanged.AddListener(delegate { UpdateEffectSoundAmount(); });
UpdateBackgroundSoundAmount();
UpdateEffectSoundAmount();
CoPlay = CoPlayBackgroundMusic();
StartCoroutine(CoPlay);
CoEffect = CoPlayEffectMusic();
StartCoroutine(CoEffect);
}
public IEnumerator CoPlayBackgroundMusic()
{
backgroundAudioSource.loop = true;
backgroundAudioSource.Play();
while (true)
{
float backVolume = backgroundSoundValue / 100f;
backgroundAudioSource.volume = backVolume;
yield return null;
}
}
public IEnumerator CoPlayEffectMusic()
{
effectAudioSource.loop = true;
effectAudioSource.Play();
while (true)
{
float volume = effectSoundValue / 100f;
effectAudioSource.volume = volume;
yield return null;
}
}
void UpdateBackgroundSoundAmount()
{
backgroundSoundValue = (int)backgroundSoundSlider.value;
backgroundSoundAmount.text = backgroundSoundValue.ToString();
SaveSoundSettings(); // 슬라이더 값 변경 시 저장
Debug.Log($"Background Sound Value: {backgroundSoundValue}");
}
void UpdateEffectSoundAmount()
{
effectSoundValue = (int)effectSoundSlider.value;
effectSoundAmount.text = effectSoundValue.ToString();
SaveSoundSettings(); // 슬라이더 값 변경 시 저장
Debug.Log($"Effect Sound Value: {effectSoundValue}");
}
void SaveSoundSettings()
{
PlayerPrefs.SetInt("BackgroundSoundValue", backgroundSoundValue);
PlayerPrefs.SetInt("EffectSoundValue", effectSoundValue);
PlayerPrefs.SetString("BackgroundSoundAmount", backgroundSoundAmount.text);
PlayerPrefs.SetString("EffectSoundAmount", effectSoundAmount.text);
PlayerPrefs.Save();
Debug.Log("Sound settings saved.");
}
void LoadSoundSettings()
{
if (PlayerPrefs.HasKey("BackgroundSoundValue"))
{
backgroundSoundValue = PlayerPrefs.GetInt("BackgroundSoundValue");
backgroundSoundSlider.value = backgroundSoundValue;
}
else
{
backgroundSoundValue = (int)backgroundSoundSlider.value;
}
if (PlayerPrefs.HasKey("EffectSoundValue"))
{
effectSoundValue = PlayerPrefs.GetInt("EffectSoundValue");
effectSoundSlider.value = effectSoundValue;
}
else
{
effectSoundValue = (int)effectSoundSlider.value;
}
if (PlayerPrefs.HasKey("BackgroundSoundAmount"))
{
backgroundSoundAmount.text = PlayerPrefs.GetString("BackgroundSoundAmount");
}
if (PlayerPrefs.HasKey("EffectSoundAmount"))
{
effectSoundAmount.text = PlayerPrefs.GetString("EffectSoundAmount");
}
Debug.Log($"Loaded Background Sound Value: {backgroundSoundValue}");
Debug.Log($"Loaded Effect Sound Value: {effectSoundValue}");
}
}
'Project > Skill Steal Girl' 카테고리의 다른 글
[SSG] Dialog Canavs Btn Test (0) | 2024.06.23 |
---|---|
[SSG] Dialog RnD (0) | 2024.06.15 |
[SSG] 행동카드 도감에 있는 정보인지 아닌지 구별하여 출력 (0) | 2024.06.09 |
[SSG] 행동카드 도감 instantiate (0) | 2024.06.09 |
[SSG] 최종프로젝트 - 환경설정(음량 조절) (0) | 2024.06.03 |