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

 

새게임 시작 시

나머지를 초기화 후 저장

재시작 시

기존에 가지고 가야 할 정보들만 따로 저장하고 시작됨

 

이어하기는 기존의 데이터를 가져옴

 

나머지 도감 클릭시 준비카드 or 행동카드 도감 선택

환경설정 시나와야 할 UI 구성

 

 

 

 

 

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class SelectPreparedCard : MonoBehaviour
{
    
    public TMP_Text preparedCardName;
    public TMP_Text preparedCardDesc;
    public int id;


    public void UpdateCard(int id)
    {
        this.id = id;
        preparedCardName.text = PlayerManager.Instance.dicPreparedCardDatas[id].card_name;
        preparedCardDesc.text = PlayerManager.Instance.dicPreparedCardDatas[id].desc;

    }
}

 

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class SelectStartCard : MonoBehaviour
{
    public Transform content;
    public GameObject cardPrefab;


    void Start()
    {
        PlayerManager.Instance.LoadPreparedCardData();
        PlayerManager.Instance.LoadPlayerSelectCardInfo();


        PreparedCardData[] prepareCards = new PreparedCardData[2];
        prepareCards[0] = PlayerManager.Instance.dicPreparedCardDatas[2000];
        prepareCards[1] = PlayerManager.Instance.dicPreparedCardDatas[2001];

        for (int i = 0; i < 2; i++){

            GameObject card = Instantiate(cardPrefab, content);
            SelectPreparedCard c = card.GetComponent<SelectPreparedCard>();
            c.id = prepareCards[i].id;
            c.UpdateCard(c.id);
        }
    }
}

 

inspector에서 string으로 원하는 scene의 이름을 저장한 후 클릭을 하면 해당  씬으로 이동

 

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ChangeScene : MonoBehaviour
{
    public string sceneName;
    private Button btn;

    void Start()
    {
        btn = GetComponent<Button>();

        btn.onClick.AddListener(() =>
        {
            SceneManager.LoadScene(sceneName);
        });
    }
}

 

 

 

카드 클릭시 체크 표시 및 알파 값 조절

 

클릭시 desc 나오는건 수정 요망

+ Recent posts