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}");
    }
}

+ Recent posts