如果游戏缺少某种音频,无论是背景音乐还是其他音效,那么游戏是不完整的。Unity 的音频系统既灵活而又强大。它可以导入大多数标准音频文件格式,精通于在3D 空间中播放声音,还可根据需要提供其他效果(比如回声和滤波)。Unity 还可以通过用户机器上任何可用麦克风来录制音频,以便在游戏过程中进行使用或存储和传输。
在现实生活中,人们听到的声音是由物体发出来的。声音被感知的方式取决于许多因素。倾听者可以粗略地分辨出声音来自哪个方向,根据音量和音色判断距离。由于多普勒效应,快速移动的声源(如坠落的炸弹或过往的警车)会随着移动而改变音高。此外,周围环境也会影响声音的反射方式,因此洞穴内的声音会产生回声,但相同的声音在露天环境中却不会产生回声。
为了模拟位置的影响,Unity 要求声音源来自附加到对象的__音频源__。然后,发出的声音由附加到另一个对象(通常是主摄像机)上的__音频监听器__拾取。Unity 然后可以模拟音频源与监听器物体之间的距离和位置的影响,并相应地播放给用户。此外还使用源对象和监听器对象的相对速度来模拟多普勒效应以增加真实感。
Unity 无法仅根据场景几何体来计算回声,但您可以通过向对象添加__音频滤波器__来模拟回声。例如,您可以针对应该来自洞穴内的声音应用“回声”滤波器。如果对象在具有猛烈回声的地方来回移动,可以在场景中添加__混响区 (Reverb Zone)__。例如,游戏中驾车穿过隧道。如果在隧道内放置一个混响,那么汽车的发动机声音会在汽车驶入时开始回响,当汽车从另一侧驶出时,回声会消失。
Unity 混音器 (Audio Mixer) 允许您混合各种音频源,对音频源应用效果,以及执行母带制作 (mastering)。
音频源、音频监听器、混音器、音频效果和混响区的手册页中提供了有关可用于获得正确效果的众多选项和参数的信息。
Unity 可使用与其他资源相同的方式导入 AIFF、WAV、MP3 和 Ogg 格式的音频文件,只需要将文件拖入 Project 面板即可。导入音频文件会创建音频剪辑,然后可以将音频剪辑拖动到音频源中,或通过脚本来使用音频剪辑。音频剪辑参考页面提供了有关音频文件的可用导入选项的更多详细信息。
对于音乐,Unity 还支持音轨模块,这些模块使用短音频样本作为“编曲”,然后进行安排以播放曲调。可以从 .xm、.mod、.it 和 .s3m 文件导入音轨模块,但与普通音频剪辑的其他使用方式大致相同。
Unity 可以通过脚本来访问计算机的麦克风,并可通过直接录音来创建音频剪辑。Microphone 类提供了一个简单的 API 来查找可用的麦克风,查询麦克风的功能,以及开始和结束录制会话。麦克风的脚本参考页面中提供了音频录制的更多信息和代码示例。
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.