유니티에서는 AudioSource 컴포넌트를 스크립트로 제어할 수 있다.

 

프로젝트를 만들던 중,

코루틴으로 지연시킨 후 플레이하고 싶은 일이 생겼다.

이런 경우 컴포넌트에 delay 기능이 없으므로

스크립트로 재생 시점을 조정.

 

위처럼 스크립트를 작성해주면 내가 설정한 시점에 새로운 컴포넌트가 추가된다.

AddComponent로 하면 기존에 AudioSource컴포넌트가 붙어 있었어도

새로운 AudioSource컴포넌트가 만들어진다.

 

새로 만들어지는 것이기 때문에

컴포넌트를 추가한 뒤 클립을 이용하여

재생하고 싶은 오디오 파일을 오디오 클립에 넣는 스크립트도 같이

짜주어야 한다.

안 그럼 클립에 아무것도 없어서 소리가 안 난다.

(재생할게 없는 상태이니..)

 

 

 

그렇다면 기존에 세팅해둔 AudioSource 컴포넌트를 원하는 때에

작동하게 하려면 어떻게 해야할까?

컴포넌트에 미리 세팅을 해둔 후 컴포넌트를 불러와서 클립을 재생하면 된다.

 

 

 

스크립트로 오디오 소스를 건드리는 경우에는

내 의지와는 상관없이 재생되는것을 방지하기 위해 체크를 해제해야 한다.

 

 

 

 

 

Public Functions

GetCustomCurve AudioSource의 아웃풋 AudioMixer를 설정합니다.
GetSpatializerFloat Reads a user-defined parameter of a custom spatializer effect that is attached to an AudioSource.
Pause 재생중인 clip을 일시정지 합니다.
Play 일정한 딜레이를 갖게 하여 clip을 재생합니다.
PlayDelayed 초 단위로 지정된 지연에 clip을 재생합니다. 사용자는 44.1kHz의 참조 레이트와 관계하고 있는 샘플의 지정된 딜레이를 인수로 한 오래된 Play(delay) 함수 대신 이 함수를 사용하는 것을 추천합니다.
PlayOnDualShock4 Enable playing of audio source though a specfic DualShock4 controllerEnable playing of audio source though a specfic DualShock4 controller.
PlayOneShot AudioClip을 재생하며, volumeScale에 의해 AudioSource 볼륨을 스케일합니다.
PlayScheduled Plays the clip at a specific time on the absolute time-line that AudioSettings.dspTime reads from.
SetCustomCurve 현재의 오디오 소스(Audio source)에 대한 도플러효과 범위(Doppler scale)를 설정합니다.
SetScheduledEndTime Changes the time at which a sound that has already been scheduled to play will end. Notice that depending on the timing not all rescheduling requests can be fulfilled.
SetScheduledStartTime Changes the time at which a sound that has already been scheduled to play will start.
SetSpatializerFloat Sets a user-defined parameter of a custom spatializer effect that is attached to an AudioSource.
Stop clip의 재생을 정지합니다.
UnPause 이 AudioSource에 대해 일시정지된 재생상태를 해제합니다.

https://docs.unity3d.com/kr/530/ScriptReference/AudioSource.html

+ Recent posts