유니티 음악 일시정지와 재개 (Pause & Resume)

농담고미고미·2024년 7월 3일

Unity 개발 일지

목록 보기
10/26

배경음을 musicSounds[]배열에서 찾아낸다. 이때 배경음은 "이름"으로 찾는다. 만약 Sound s가 null이면 못 찾았다는 로그를 반환하고, 찾으면 Pause()을 이용한다.

일시정지된 배경음을 다시 재개하고 싶다. 따라서 일시정지할 때 pausedTime = musicSource.time; 을 해준다.

그리고 Resume함수에서는 다시 Play();해준다. 이때 Play();만 하면 음악이 처음부터 재생된다. 여기에 musicSource.time = pausedTime; 을 해줘야지 오디오 파일의 일시정지 됐던 시간부터 재생된다.

public void PauseMusic(string name)
{
Sound s = Array.Find(musicSounds, x => x.name == name);

if (s == null)
{
    Debug.Log("Sound Not Found");
}

else
{
    musicSource.clip = s.clip;
    musicSource.Pause();
    pausedTime = musicSource.time;
}

}
public void ResumeMusic(string name)
{
Sound s = Array.Find(musicSounds, x => x.name == name);

if (s == null)
{
    Debug.Log("Sound Not Found");
}

else
{
    musicSource.clip = s.clip;
    musicSource.Play();
    musicSource.time = pausedTime;
}

}

profile
농담곰을 좋아해요 말랑곰탱이

0개의 댓글