Unity Engine/Unity 2D.

[유니티 2D] 사운드 매니저

Muru 2023. 11. 12. 22:52

 

public class SoundManager : MonoBehaviour
{
		// 싱글톤
    public static SoundManager instance;    //자신을 변수로 담는다. 

    public AudioClip soundExplosion;    //폭발소리를 저장하는 AudioClip 변수

    AudioSource myAudio;    //AudioSource 컴포넌트 변수를 담는다.

    public void Awake() //start보다 먼저 객체가 생성될 때 호출되는 Awake
    {
        if(SoundManager.instance == null)   //instace가 값이 없냐?
        {
            SoundManager.instance = this;   //자기자신을받아.
        }
    }

    void Start()
    {
        myAudio = GetComponent<AudioSource>();  //AudioSource컴포넌트 가져오기
    }

    public void PlaySound()
    {
        myAudio.PlayOneShot(soundExplosion);
    }

public class SoundManager : MonoBehaviour
{
    //싱글톤
    public static SoundManager instance; // 자기자신을 변수로 담고

    public AudioClip soundExplosion;    //재생할 소리를 변수로 담는다.
    public AudioClip soundDie;  //죽는 사운드

    AudioSource myAudio; //AudioSource 컴포넌트를 변수로 담는다.

    private void Awake()    // Awake는 Start보다 먼저, 객체가 생성될 때 호출된다.
    {
        if(SoundManager.instance == null) //incetance가 비어있는지 검사
        {
            SoundManager.instance = this;   //자기 자신을 받는다...
        }
    }

    void Start()
    {
        myAudio = GetComponent<AudioSource>();  //AudioSource 컴포넌트 가져오기
    }

    public void PlaySound()
    {
        myAudio.PlayOneShot(soundExplosion);
    }

    public void SoundDie()
    {
        myAudio.PlayOneShot(soundDie);
    }
}