[SerializeField] private AudioMixer m_AudioMixer; [SerializeField] private Slider m_MusicMasterSlider; [SerializeField] private Slider m_MusicBGMSlider; [SerializeField] private Slider m_MusicSFXSlider; private const string MasterVolumeKey = "MasterVolume"; private const string BGMVolumeKey = "BGMVolume"; private const string SFXVolumeKey = "SFXVolume"; private void Awake() { m_MusicMasterSlider.onValueChanged.AddListener(SetMasterVolume); m_MusicBGMSlider.onValueChanged.AddListener(SetMusicVolume); m_MusicSFXSlider.onValueChanged.AddListener(SetSFXVolume); } private void Start() { LoadVolumeSettings(); } public void SetMasterVolume(float volume) { m_AudioMixer.SetFloat("Master", Mathf.Log10(volume) * 20); PlayerPrefs.SetFloat(MasterVolumeKey, volume); PlayerPrefs.Save(); } public void SetMusicVolume(float volume) { m_AudioMixer.SetFloat("BGM", Mathf.Log10(volume) * 20); PlayerPrefs.SetFloat(BGMVolumeKey, volume); PlayerPrefs.Save(); } public void SetSFXVolume(float volume) { m_AudioMixer.SetFloat("SFX", Mathf.Log10(volume) * 20); PlayerPrefs.SetFloat(SFXVolumeKey, volume); PlayerPrefs.Save(); } public void LoadVolumeSettings() { float masterVolume = PlayerPrefs.GetFloat(MasterVolumeKey, 1f); float bgmVolume = PlayerPrefs.GetFloat(BGMVolumeKey, 1f); float sfxVolume = PlayerPrefs.GetFloat(SFXVolumeKey, 1f); m_MusicMasterSlider.value = masterVolume; m_MusicBGMSlider.value = bgmVolume; m_MusicSFXSlider.value = sfxVolume; ApplyVolumeToMixer(masterVolume, bgmVolume, sfxVolume); } private void ApplyVolumeToMixer(float masterVolume, float bgmVolume, float sfxVolume) { m_AudioMixer.SetFloat("Master", Mathf.Log10(masterVolume) * 20); m_AudioMixer.SetFloat("BGM", Mathf.Log10(bgmVolume) * 20); m_AudioMixer.SetFloat("SFX", Mathf.Log10(sfxVolume) * 20); }
public class SaveLoad : MonoBehaviour { public static SaveLoad Instance { get; private set; } private string saveFilePath; private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); saveFilePath = Application.persistentDataPath + "/saveData.json"; } else { Destroy(gameObject); } } [System.Serializable] private class SaveData { public string savedStage; } public void SaveCurrentStage(string stageName) { try { SaveData data = new SaveData { savedStage = stageName }; string json = JsonUtility.ToJson(data, true); File.WriteAllText(saveFilePath, json); Debug.Log($"�������� '{stageName}'�� {saveFilePath}�� ����Ǿ����ϴ�."); } catch (IOException e) { Debug.LogError($"�������� ���� �� ���� ��: {e.Message}"); } } public string LoadSavedStage() { StorySceneManager.firstStoryCount = StorySceneManager.fullStoryCount; try { if (File.Exists(saveFilePath)) { string json = File.ReadAllText(saveFilePath); // ���� �б� SaveData data = JsonUtility.FromJson<SaveData>(json); Debug.Log($"����� �������� '{data.savedStage}'�� �ε�Ǿ����ϴ�."); return data.savedStage; } else { Debug.LogWarning("���� ������ �������� �ʾ� �⺻��('Stage1')�� ��ȯ�մϴ�."); return "Stage1"; } } catch (IOException e) { Debug.LogError($"�������� �ε� �� ���� ��: {e.Message}"); return "Stage1"; } } }
+ 스크립트로 AudioSource를 가져오고 AudioMixer를 활용
(코드의 일부)
[SerializeField] private AudioClip successBGM; [SerializeField] private AudioClip failureBGM; [SerializeField] private AudioSource audioSource; // bgm재생용 [SerializeField] private AudioMixerGroup bgmMixerGroup; private void Awake() { if (audioSource == null) { audioSource = gameObject.AddComponent<AudioSource>(); } audioSource.outputAudioMixerGroup = bgmMixerGroup; } private void StorySceneFirstImage() { if (firstStoryCount == 0 && !Finish.AllClearCheck) { } else if (firstStoryCount == fullStoryCount && !Finish.AllClearCheck) { PlayBGM(failureBGM); } else if (firstStoryCount == fullStoryCount &&Finish.AllClearCheck) { PlayBGM(successBGM); } } private void PlayBGM(AudioClip clip) { if (clip == null) { return; } audioSource.clip = clip; audioSource.Play(); }