pitch
AudioSource의 소리 재생 속도를 조절하는 속성이다.
이 값은 기본적으로 1로 설정되어 있으며, 이는 소리를 정상 속도로 재생한다는 의미이다.
pitch 값이 2이면 소리는 2배 빠르게 재생되며, 0.5이면 소리는 절반 속도로 느리게 재생된다.
이를 통해 소리의 톤을 변경하거나, 소리의 속도를 조절하여 효과를 주는 등 다양한 응용이 가능하다.
using UnityEngine;
public class PitchController : MonoBehaviour
{
public AudioSource audioSource;
public float minPitch = 0.5f;
public float maxPitch = 2f;
private void Start()
{
// AudioSource 컴포넌트 가져오기
audioSource = GetComponent<AudioSource>();
}
private void Update()
{
// 피치 조절하기
if (Input.GetKeyDown(KeyCode.UpArrow))
{
IncreasePitch();
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
DecreasePitch();
}
}
private void IncreasePitch()
{
// 현재 피치 값에 0.1씩 증가
audioSource.pitch += 0.1f;
// 최대 피치 값 이상이면 최대 피치로 설정
if (audioSource.pitch > maxPitch)
{
audioSource.pitch = maxPitch;
}
}
private void DecreasePitch()
{
// 현재 피치 값에 0.1씩 감소
time
클립의 재생 위치를 지정한다.
using UnityEngine;
public class AudioTimeController : MonoBehaviour
{
public AudioSource audioSource;
public float seekTime = 10f;
private void Start()
{
// AudioSource 컴포넌트 가져오기
audioSource = GetComponent<AudioSource>();
}
private void Update()
{
// 시간 이동하기
if (Input.GetKeyDown(KeyCode.Space))
{
SeekAudio(seekTime);
}
// 일시 정지하기
if (Input.GetKeyDown(KeyCode.P))
{
PauseAudio();
}
// 다시 재생하기
if (Input.GetKeyDown(KeyCode.R))
{
ResumeAudio();
}
}
private void SeekAudio(float time)
{
// 지정된 시간으로 이동
audioSource.time = time;
}
private void PauseAudio()
{
// 오디오 일시 정지
audioSource.Pause();
}
private void ResumeAudio()
{
// 오디오 다시 재생
audioSource.Play();
}
}
SpatialBlend
오디오 소스의 3D 음향 공간 블렌딩을 조절하는 데 사용된다.
오디오의 3D 공간 위치와 리스너의 위치에 따라 오디오가 어떻게 블렌딩되는지를 제어할 수 있다.
using UnityEngine;
public class SpatialAudioController : MonoBehaviour
{
public AudioSource audioSource;
public float spatialBlendAmount = 0.5f;
private void Start()
{
// AudioSource 컴포넌트 가져오기
audioSource = GetComponent<AudioSource>();
// spatialBlend 초기화
audioSource.spatialBlend = spatialBlendAmount;
}
private void Update()
{
// 3D 공간 블렌딩 변경하기
if (Input.GetKeyDown(KeyCode.UpArrow))
{
IncreaseSpatialBlend();
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
DecreaseSpatialBlend();
}
}
private void IncreaseSpatialBlend()
{
// spatialBlend 값 증가 (1.0에 가까워짐 - 3D 오디오)
spatialBlendAmount += 0.1f;
spatialBlendAmount = Mathf.Clamp01(spatialBlendAmount); // 0.0과 1.0 사이로 제한
audioSource.spatialBlend = spatialBlendAmount;
}
private void DecreaseSpatialBlend()
{
// spatialBlend 값 감소 (0.0에 가까워짐 - 2D 오디오)
spatialBlendAmount -= 0.1f;
spatialBlendAmount = Mathf.Clamp01(spatialBlendAmount); // 0.0과 1.0 사이로 제한
audioSource.spatialBlend = spatialBlendAmount;
}
}
priority
오디오 소스의 재생 우선순위를 설정하는 데 사용된다.
이 속성은 동시에 여러 개의 오디오 소스가 재생될 때
어떤 소스가 다른 소스보다 우선적으로 재생되는지 결정하는 데 도움을 준다.
priority 값은 정수로 지정되며, 값이 높을수록 우선순위가 높아진다.
동일한 우선순위를 가진 오디오 소스들이 재생 중인 경우,
Unity는 다양한 요소에 따라 재생 우선순위를 결정한다.
예를 들어, 오디오 소스가 카메라에 가까이 위치한 경우 재생 우선순위가 높아질 수 있다.
게임 중에 중요한 이야기 대사를 재생하는 오디오 소스의 priority 값을 높게 설정하면,
다른 사운드 이펙트나 배경 음악보다 더 우선적으로 재생될 수 있다.
이를 통해 게임 플레이어는 중요한 대사를 명확하게 들을 수 있다.
priority 속성은 0 이상의 정수 값을 가지며, 기본값은 128이다.
낮은 우선순위 값을 가지는 오디오 소스는 더 높은 우선순위 값을 가지는 소스에 의해 재생이 차단될 수 있다.
따라서 원하는 우선순위에 따라 값을 조절하여 음향 효과를 원하는 대로 제어할 수 있다.
ignoreListenerPause
AudioListener가 일시 정지되었을 때 해당 AudioSource의 재생 여부를 결정하는 데 사용된다.
이 속성을 true로 설정하면, AudioListener가 일시 정지되어도 AudioSource는 계속해서 재생된다.
반대로, false로 설정하면 AudioListener가 일시 정지되면 해당 AudioSource도 일시 정지된다.
AudioListener 클래스의 pause 속성은 AudioListener 자체를 일시 정지하는 데 사용된다.
이 속성을 true로 설정하면, 모든 AudioSource가 일시 정지되고 오디오 재생이 중지된다.
반대로, false로 설정하면 일시 정지된 AudioSource들이 다시 재생되며 오디오 재생이 계속된다.
이러한 속성들은 게임에서 음악이나 사운드 효과를 제어하고자 할 때 유용하다.
예를 들어, 게임의 일시 정지 기능을 구현하고자 할 때, AudioListener.pause 속성을 true로 설정하여
모든 오디오 재생을 중지할 수 있다.
또는 특정한 AudioSource가 일시 정지되지 않도록 하고 싶은 경우,
해당 AudioSource의 ignoreListenerPause 속성을 true로 설정하여
AudioListener의 일시 정지에 영향을 받지 않도록 할 수 있다.
using UnityEngine;
public class AudioControl : MonoBehaviour
{
public AudioSource backgroundMusic;
// 게임이 일시 정지되었는지 여부를 저장하는 변수
private bool isGamePaused = false;
private void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
// 게임을 일시 정지 또는 재개
isGamePaused = !isGamePaused;
// AudioListener의 일시 정지 설정
AudioListener.pause = isGamePaused;
// 배경 음악 AudioSource의 ignoreListenerPause 속성 설정, 배경 음악은 다른 오디오소스가 정지되어도 계속 흘러나옴
backgroundMusic.ignoreListenerPause = true;
}
}
}