音频源在 3D 中的表示。
AudioSource 附加到 GameObject,用于在 3D 环境中播放声音。
要播放 3D 声音,您还需要一个 AudioListener。
音频监听器通常附加到您要使用的摄像机。
声音以 3D 还是 2D 方式播放由 AudioImporter 设置决定。
您可以使用 Play、Pause 和 Stop 播放单个音频剪辑。
也可以使用 volume 属性在播放时调整其音量,或使用 time 进行搜寻。
使用 PlayOneShot 可以在一个 AudioSource 上播放多个声音。
使用 PlayClipAtPoint 可以在 3D 空间中的某个静态位置播放剪辑。
另请参阅:AudioListener、AudioClip、AudioSource 组件。
//This script allows you to toggle music to play and stop. //Assign an AudioSource to a GameObject and attach an Audio Clip in the Audio Source. Attach this script to the GameObject.
using UnityEngine;
public class Example : MonoBehaviour { AudioSource m_MyAudioSource;
//Play the music bool m_Play; //Detect when you use the toggle, ensures music isn’t played multiple times bool m_ToggleChange;
void Start() { //Fetch the AudioSource from the GameObject m_MyAudioSource = GetComponent<AudioSource>(); //Ensure the toggle is set to true for the music to play at start-up m_Play = true; }
void Update() { //Check to see if you just set the toggle to positive if (m_Play == true && m_ToggleChange == true) { //Play the audio you attach to the AudioSource component m_MyAudioSource.Play(); //Ensure audio doesn’t play more than once m_ToggleChange = false; } //Check if you just set the toggle to false if (m_Play == false && m_ToggleChange == true) { //Stop the audio m_MyAudioSource.Stop(); //Ensure audio doesn’t play more than once m_ToggleChange = false; } }
void OnGUI() { //Switch this toggle to activate and deactivate the parent GameObject m_Play = GUI.Toggle(new Rect(10, 10, 100, 30), m_Play, "Play Music");
//Detect if there is a change with the toggle if (GUI.changed) { //Change to true to show that there was just a change in the toggle state m_ToggleChange = true; } } }
bypassEffects | 直通效果(从滤波器组件或全局监听器滤波器应用)。 |
bypassListenerEffects | 如果设置,则不将 AudioListener 上的全局效果应用于 AudioSource 生成的音频信号。不适用于 AudioSource 正在混合器组中播放的情况。 |
bypassReverbZones | 如果设置,则不将来自 AudioSource 的信号路由到与混响区关联的全局混响。 |
clip | 要播放的默认 AudioClip。 |
dopplerLevel | 设置该 AudioSource 的多普勒缩放。 |
gamepadSpeakerOutputType | 获取或设置音频源的游戏手柄音频输出类型。 |
ignoreListenerPause | 即使 AudioListener.pause 设置为 true,也允许 AudioSource 播放。这对于暂停菜单中的菜单元素声音或背景音乐很有用。 |
ignoreListenerVolume | 这使得音频源不考虑音频监听器的音量。 |
isPlaying | 当前正在播放该 clip 吗?(只读) |
isVirtual | 如果 AudioSource 播放的所有声音(由 Play() 或 playOnAwake 以及单次播放启动的主声音)都被音频系统剔除,则为 true。 |
loop | 是否循环播放该音频剪辑? |
maxDistance | (对数衰减)MaxDistance 为声音停止衰减的距离。 |
minDistance | 在最小距离内,AudioSource 将停止增大音量。 |
mute | 使 AudioSource 静音/取消静音。静音时将音量设置为 0,取消静音则恢复原来的音量。 |
outputAudioMixerGroup | AudioSource 应将其信号路由到的目标组。 |
panStereo | 以立体声方式(左声道或右声道)平移正在播放的声音。仅适用于单声道或立体声。 |
pitch | 音频源的音高。 |
playOnAwake | 如果设置为 true,音频源将在唤醒时自动开始播放。 |
priority | 设置 AudioSource 的优先级。 |
reverbZoneMix | 将来自 AudioSource 的信号混合到与混响区关联的全局混响中的量。 |
rolloffMode | 设置/获取 AudioSource 随距离衰减的方式。 |
spatialBlend | 设置 3D 空间化计算(衰减、多普勒效应等)对该 AudioSource 的影响程度。0.0 使声音变成全 2D 效果,1.0 使其变成全 3D。 |
spatialize | 启用或禁用空间化。 |
spatializePostEffects | 确定空间音响效果是在效果滤波器前还是后插入的。 |
spread | 设置扬声器空间中 3D 立体声或多声道声音的扩散角度(以度为单位)。 |
time | 播放位置(以秒为单位)。 |
timeSamples | PCM 样本中的播放位置。 |
velocityUpdateMode | 应以固定还是动态更新方式更新音频源。 |
volume | 音频源的音量(0.0 到 1.0)。 |
DisableGamepadOutput | 禁用音频源的游戏手柄音频输出。 |
GetAmbisonicDecoderFloat | 读取附加到 AudioSource 的自定义立体混响声解码器效果的用户定义参数。 |
GetCustomCurve | 获取给定 AudioSourceCurveType 的当前自定义曲线。 |
GetOutputData | 提供当前播放源的输出数据块。 |
GetSpatializerFloat | 读取附加到 AudioSource 的自定义空间音响效果的用户定义参数。 |
GetSpectrumData | 提供当前播放音频源的频谱数据块。 |
Pause | 暂停播放 clip。 |
Play | 播放 clip。 |
PlayDelayed | 按照指定的延时(以秒为单位)播放 clip。建议用户使用该函数代替旧的 Play(delay) 函数,Play(delay) 函数接受以样本(参考采样率为 44.1 kHz)数为单位指定的延时作为参数。 |
PlayOneShot | 播放 AudioClip,并根据 volumeScale 调整 AudioSource 音量。 |
PlayOnGamepad | 允许通过特定游戏手柄播放音频源。 |
PlayScheduled | 在 AudioSettings.dspTime 读取的绝对时间轴上的特定时间播放 clip。 |
SetAmbisonicDecoderFloat | 设置附加到 AudioSource 的自定义立体混响声解码器效果的用户定义参数。 |
SetCustomCurve | 设置给定 AudioSourceCurveType 的自定义曲线。 |
SetScheduledEndTime | 更改某个已计划播放的声音将结束的时间。注意,根据时间安排,并非所有重新计划的请求都能得到满足。 |
SetScheduledStartTime | 更改某个已计划播放的声音将开始的时间。 |
SetSpatializerFloat | 设置附加到 AudioSource 的自定义空间音响效果的用户定义参数。 |
Stop | 停止播放 clip。 |
UnPause | 恢复播放该 AudioSource。 |
GamepadSpeakerSupportsOutputType | 检查平台是否在游戏手柄上支持音频输出类型。 |
PlayClipAtPoint | 在世界空间中的给定位置播放 AudioClip。 |
enabled | 启用的 Behaviour 可更新,禁用的 Behaviour 不可更新。 |
isActiveAndEnabled | 是否已激活并启用 Behaviour? |
gameObject | 此组件附加到的游戏对象。始终将组件附加到游戏对象。 |
tag | 此游戏对象的标签。 |
transform | 附加到此 GameObject 的 Transform。 |
hideFlags | 该对象应该隐藏、随场景一起保存还是由用户修改? |
name | 对象的名称。 |
BroadcastMessage | 调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为 methodName 的方法。 |
CompareTag | 此游戏对象是否使用 tag 进行了标记? |
GetComponent | 如果游戏对象附加了类型为 type 的组件,则将其返回,否则返回 null。 |
GetComponentInChildren | 使用深度首次搜索返回 GameObject 或其任何子项中类型为 type 的组件。 |
GetComponentInParent | 返回 GameObject 或其任何父项中类型为 type 的组件。 |
GetComponents | 返回 GameObject 中类型为 type 的所有组件。 |
GetComponentsInChildren | 返回 GameObject 或其任何子项中类型为 type 的所有组件。 |
GetComponentsInParent | 返回 GameObject 或其任何父项中类型为 type 的所有组件。 |
SendMessage | 调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。 |
SendMessageUpwards | 调用此游戏对象中的每个 MonoBehaviour 上或此行为的每个父级上名为 methodName 的方法。 |
TryGetComponent | 获取指定类型的组件(如果存在)。 |
GetInstanceID | 返回对象的实例 ID。 |
ToString | 返回对象的名称。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindObjectOfType | 返回第一个类型为 type 的已加载的激活对象。 |
FindObjectsOfType | 返回所有类型为 type 的已加载的激活对象的列表。 |
Instantiate | 克隆 original 对象并返回克隆对象。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.