프로젝트/건즈앤 레이첼스

[유니티 프로젝트] 옵션 UI

Bueong_E 2023. 4. 1. 00:50
반응형
SMALL

 

추가 구현해야 할 사항은 아래와 같다.

1. 체크버튼은 실제 바이브레이션을 끌수 있도록 구성

2. 공지사항 버튼 연동

3. 도움말 버튼 연동

 

 

코드

 

AudioManager 스크립트

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


public class AudioManager : MonoBehaviour
{
    public float backgroundMusicVolume = 1f; // 배경음악 볼륨
    public float soundEffectVolume = 1f; // 효과음 볼륨
    public bool isVibrationOn = true; // 진동 On/Off

    private AudioSource audioSource;
    public GameObject deathSound;

    void Start()
    {
        // 초기화
        this.audioSource = this.GetComponent<AudioSource>();
    }

    // 배경음악 볼륨 설정
    public void SetBackgroundMusicVolume(float value)
    {
        this.backgroundMusicVolume = value;
        this.audioSource.volume = value;   
    }

    // 효과음 볼륨 설정
    public void SetSoundEffectVolume(float value)
    {
        this.soundEffectVolume = value;
        this.PlaySfx();
    }


    // 진동 On/Off 설정
    public void SetVibrationOnOff(bool value)
    {
        this.isVibrationOn = value;
        string state = value ? "on" : "off";
        Debug.LogFormat("viberation : {0}", state);
    }

    public void PlaySfx()
    {
        var go = GameObject.Instantiate(this.deathSound);    
        go.GetComponent<AudioSource>().volume = this.soundEffectVolume;    
        this.StartCoroutine(this.DestoySFX(go));    
    }

    private IEnumerator DestoySFX(GameObject go)
    {
        yield return new WaitForSeconds(2f);
        GameObject.Destroy(go);
    }

}

UISettingPopup 스크립트

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

public class UISettingPopup : MonoBehaviour
{
    public Slider backgroundMusicSlider; // 배경음악 볼륨 조절 슬라이더
    public Slider soundEffectSlider; // 효과음 볼륨 조절 슬라이더
    public Toggle vibrationToggle; // 진동 On/Off 토글

    public AudioManager audioManager; // AudioManager 컴포넌트

    void Start()
    {
        // 초기값 설정
        this.backgroundMusicSlider.value = this.audioManager.backgroundMusicVolume;       
        this.soundEffectSlider.value = this.audioManager.soundEffectVolume;
        this.vibrationToggle.isOn = this.audioManager.isVibrationOn;

        this.backgroundMusicSlider.onValueChanged.AddListener((x) =>
        {
            this.OnBackgroundMusicSliderValueChanged(x);
        });

        this.soundEffectSlider.onValueChanged.AddListener((x) =>
        {
            this.OnSoundEffectSliderValueChanged(x);
        });

        this.vibrationToggle.onValueChanged.AddListener((x) => {

            this.OnVibrationToggleValueChanged(x);
        });
    }

    // 배경음악 볼륨 조절 슬라이더 값 변경시 호출되는 메소드
    public void OnBackgroundMusicSliderValueChanged(float value)
    {
        this.audioManager.SetBackgroundMusicVolume(value);
    }

    // 효과음 볼륨 조절 슬라이더 값 변경시 호출되는 메소드
    public void OnSoundEffectSliderValueChanged(float value)
    {
        this.audioManager.SetSoundEffectVolume(value);
    }

    // 진동 On/Off 토글 값 변경시 호출되는 메소드
    public void OnVibrationToggleValueChanged(bool value)
    {
        this.audioManager.SetVibrationOnOff(value);
    }

}

 

 

반응형
LIST