UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
AudioSource
# AudioSource class in UnityEngine / 继承自:[Behaviour](https://docs.unity3d.com/cn/2019.4/ScriptReference/Behaviour.html) ## 描述 音频源在 3D 中的表示。 AudioSource 附加到 [GameObject](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.html),用于在 3D 环境中播放声音。 要播放 3D 声音,您还需要一个 [AudioListener](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioListener.html)。 音频监听器通常附加到您要使用的摄像机。 声音以 3D 还是 2D 方式播放由 [AudioImporter](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioImporter.html) 设置决定。 您可以使用 [Play](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.Play.html)、[Pause](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.Pause.html) 和 [Stop](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.Stop.html) 播放单个音频剪辑。 也可以使用 [volume](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-volume.html) 属性在播放时调整其音量,或使用 [time](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-time.html) 进行搜寻。 使用 [PlayOneShot](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.PlayOneShot.html) 可以在一个 AudioSource 上播放多个声音。 使用 [PlayClipAtPoint](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.PlayClipAtPoint.html) 可以在 3D 空间中的某个静态位置播放剪辑。 另请参阅:[AudioListener](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioListener.html)、[AudioClip](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioClip.html)、[AudioSource 组件](https://docs.unity3d.com/cn/2019.4/Manual/class-AudioSource.html)。 ``` //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](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-bypassEffects.html) | 直通效果(从滤波器组件或全局监听器滤波器应用)。 | | ----------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | | [bypassListenerEffects](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-bypassListenerEffects.html) | 如果设置,则不将 AudioListener 上的全局效果应用于 AudioSource 生成的音频信号。不适用于 AudioSource 正在混合器组中播放的情况。 | | [bypassReverbZones](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-bypassReverbZones.html) | 如果设置,则不将来自 AudioSource 的信号路由到与混响区关联的全局混响。 | | [clip](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-clip.html) | 要播放的默认 AudioClip。 | | [dopplerLevel](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-dopplerLevel.html) | 设置该 AudioSource 的多普勒缩放。 | | [gamepadSpeakerOutputType](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-gamepadSpeakerOutputType.html) | 获取或设置音频源的游戏手柄音频输出类型。 | | [ignoreListenerPause](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-ignoreListenerPause.html) | 即使 AudioListener.pause 设置为 true,也允许 AudioSource 播放。这对于暂停菜单中的菜单元素声音或背景音乐很有用。 | | [ignoreListenerVolume](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-ignoreListenerVolume.html) | 这使得音频源不考虑音频监听器的音量。 | | [isPlaying](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-isPlaying.html) | 当前正在播放该 clip 吗?(只读) | | [isVirtual](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-isVirtual.html) | 如果 AudioSource 播放的所有声音(由 Play() 或 playOnAwake 以及单次播放启动的主声音)都被音频系统剔除,则为 true。 | | [loop](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-loop.html) | 是否循环播放该音频剪辑? | | [maxDistance](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-maxDistance.html) | (对数衰减)MaxDistance 为声音停止衰减的距离。 | | [minDistance](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-minDistance.html) | 在最小距离内,AudioSource 将停止增大音量。 | | [mute](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-mute.html) | 使 AudioSource 静音/取消静音。静音时将音量设置为 0,取消静音则恢复原来的音量。 | | [outputAudioMixerGroup](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-outputAudioMixerGroup.html) | AudioSource 应将其信号路由到的目标组。 | | [panStereo](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-panStereo.html) | 以立体声方式(左声道或右声道)平移正在播放的声音。仅适用于单声道或立体声。 | | [pitch](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-pitch.html) | 音频源的音高。 | | [playOnAwake](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-playOnAwake.html) | 如果设置为 true,音频源将在唤醒时自动开始播放。 | | [priority](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-priority.html) | 设置 AudioSource 的优先级。 | | [reverbZoneMix](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-reverbZoneMix.html) | 将来自 AudioSource 的信号混合到与混响区关联的全局混响中的量。 | | [rolloffMode](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-rolloffMode.html) | 设置/获取 AudioSource 随距离衰减的方式。 | | [spatialBlend](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-spatialBlend.html) | 设置 3D 空间化计算(衰减、多普勒效应等)对该 AudioSource 的影响程度。0.0 使声音变成全 2D 效果,1.0 使其变成全 3D。 | | [spatialize](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-spatialize.html) | 启用或禁用空间化。 | | [spatializePostEffects](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-spatializePostEffects.html) | 确定空间音响效果是在效果滤波器前还是后插入的。 | | [spread](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-spread.html) | 设置扬声器空间中 3D 立体声或多声道声音的扩散角度(以度为单位)。 | | [time](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-time.html) | 播放位置(以秒为单位)。 | | [timeSamples](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-timeSamples.html) | PCM 样本中的播放位置。 | | [velocityUpdateMode](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-velocityUpdateMode.html) | 应以固定还是动态更新方式更新音频源。 | | [volume](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-volume.html) | 音频源的音量(0.0 到 1.0)。 | ## 公共函数 | [DisableGamepadOutput](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.DisableGamepadOutput.html) | 禁用音频源的游戏手柄音频输出。 | | ----------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [GetAmbisonicDecoderFloat](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.GetAmbisonicDecoderFloat.html) | 读取附加到 AudioSource 的自定义立体混响声解码器效果的用户定义参数。 | | [GetCustomCurve](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.GetCustomCurve.html) | 获取给定 AudioSourceCurveType 的当前自定义曲线。 | | [GetOutputData](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.GetOutputData.html) | 提供当前播放源的输出数据块。 | | [GetSpatializerFloat](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.GetSpatializerFloat.html) | 读取附加到 AudioSource 的自定义空间音响效果的用户定义参数。 | | [GetSpectrumData](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.GetSpectrumData.html) | 提供当前播放音频源的频谱数据块。 | | [Pause](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.Pause.html) | 暂停播放 clip。 | | [Play](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.Play.html) | 播放 clip。 | | [PlayDelayed](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.PlayDelayed.html) | 按照指定的延时(以秒为单位)播放 clip。建议用户使用该函数代替旧的 Play(delay) 函数,Play(delay) 函数接受以样本(参考采样率为 44.1 kHz)数为单位指定的延时作为参数。 | | [PlayOneShot](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.PlayOneShot.html) | 播放 AudioClip,并根据 volumeScale 调整 AudioSource 音量。 | | [PlayOnGamepad](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.PlayOnGamepad.html) | 允许通过特定游戏手柄播放音频源。 | | [PlayScheduled](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.PlayScheduled.html) | 在 AudioSettings.dspTime 读取的绝对时间轴上的特定时间播放 clip。 | | [SetAmbisonicDecoderFloat](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.SetAmbisonicDecoderFloat.html) | 设置附加到 AudioSource 的自定义立体混响声解码器效果的用户定义参数。 | | [SetCustomCurve](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.SetCustomCurve.html) | 设置给定 AudioSourceCurveType 的自定义曲线。 | | [SetScheduledEndTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.SetScheduledEndTime.html) | 更改某个已计划播放的声音将结束的时间。注意,根据时间安排,并非所有重新计划的请求都能得到满足。 | | [SetScheduledStartTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.SetScheduledStartTime.html) | 更改某个已计划播放的声音将开始的时间。 | | [SetSpatializerFloat](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.SetSpatializerFloat.html) | 设置附加到 AudioSource 的自定义空间音响效果的用户定义参数。 | | [Stop](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.Stop.html) | 停止播放 clip。 | | [UnPause](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.UnPause.html) | 恢复播放该 AudioSource。 | ## 静态函数 | [GamepadSpeakerSupportsOutputType](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.GamepadSpeakerSupportsOutputType.html) | 检查平台是否在游戏手柄上支持音频输出类型。 | | --------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- | | [PlayClipAtPoint](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.PlayClipAtPoint.html) | 在世界空间中的给定位置播放 AudioClip。 | ## 继承的成员 ## 变量 | [enabled](https://docs.unity3d.com/cn/2019.4/ScriptReference/Behaviour-enabled.html) | 启用的 Behaviour 可更新,禁用的 Behaviour 不可更新。 | | --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | | [isActiveAndEnabled](https://docs.unity3d.com/cn/2019.4/ScriptReference/Behaviour-isActiveAndEnabled.html) | 是否已激活并启用 Behaviour? | | [gameObject](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component-gameObject.html) | 此组件附加到的游戏对象。始终将组件附加到游戏对象。 | | [tag](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component-tag.html) | 此游戏对象的标签。 | | [transform](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component-transform.html) | 附加到此 GameObject 的 Transform。 | | [hideFlags](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object-hideFlags.html) | 该对象应该隐藏、随场景一起保存还是由用户修改? | | [name](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object-name.html) | 对象的名称。 | ## 公共函数 | [BroadcastMessage](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.BroadcastMessage.html) | 调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为 methodName 的方法。 | | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | | [CompareTag](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.CompareTag.html) | 此游戏对象是否使用 tag 进行了标记? | | [GetComponent](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.GetComponent.html) | 如果游戏对象附加了类型为 type 的组件,则将其返回,否则返回 null。 | | [GetComponentInChildren](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.GetComponentInChildren.html) | 使用深度首次搜索返回 GameObject 或其任何子项中类型为 type 的组件。 | | [GetComponentInParent](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.GetComponentInParent.html) | 返回 GameObject 或其任何父项中类型为 type 的组件。 | | [GetComponents](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.GetComponents.html) | 返回 GameObject 中类型为 type 的所有组件。 | | [GetComponentsInChildren](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.GetComponentsInChildren.html) | 返回 GameObject 或其任何子项中类型为 type 的所有组件。 | | [GetComponentsInParent](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.GetComponentsInParent.html) | 返回 GameObject 或其任何父项中类型为 type 的所有组件。 | | [SendMessage](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.SendMessage.html) | 调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。 | | [SendMessageUpwards](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.SendMessageUpwards.html) | 调用此游戏对象中的每个 MonoBehaviour 上或此行为的每个父级上名为 methodName 的方法。 | | [TryGetComponent](https://docs.unity3d.com/cn/2019.4/ScriptReference/Component.TryGetComponent.html) | 获取指定类型的组件(如果存在)。 | | [GetInstanceID](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object.GetInstanceID.html) | 返回对象的实例 ID。 | | [ToString](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object.ToString.html) | 返回对象的名称。 | ## 静态函数 | [Destroy](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object.Destroy.html) | 移除 GameObject、组件或资源。 | | ---------------------------------------------------------------------------------------------------- | ------------------------------------------------ | | [DestroyImmediate](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object.DestroyImmediate.html) | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 | | [DontDestroyOnLoad](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object.DontDestroyOnLoad.html) | 在加载新的 Scene 时,请勿销毁 Object。 | | [FindObjectOfType](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object.FindObjectOfType.html) | 返回第一个类型为 type 的已加载的激活对象。 | | [FindObjectsOfType](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object.FindObjectsOfType.html) | 返回所有类型为 type 的已加载的激活对象的列表。 | | [Instantiate](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object.Instantiate.html) | 克隆 original 对象并返回克隆对象。 | ## 运算符 | [bool](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object-operator_Object.html) | 该对象是否存在? | | ---------------------------------------------------------------------------------------- | ------------------------------------------------ | | [operator !=](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object-operator_ne.html) | 比较两个对象是否引用不同的对象。 | | [operator ==](https://docs.unity3d.com/cn/2019.4/ScriptReference/Object-operator_eq.html) | 比较两个对象引用,判断它们是否引用同一个对象。 |
da
2022年5月24日 09:58
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码