与网格或纹理一样,__音频文件__资源的工作流程非常流畅且可靠。Unity 可以导入几乎任何常见文件格式,但在处理音频文件时,有一些细节值得注意。
自 Unity 5.0 开始,音频数据与实际音频剪辑 (AudioClip) 分离。音频剪辑仅仅是引用包含音频数据的文件,音频剪辑(AudioClip)导入器中有各种选项组合用于确定在运行时如何加载剪辑。这意味着您可以非常灵活地确定哪些音频资源应始终保留在内存中(因为您可能无法预测音频资源的播放频率和速度,例如脚步、武器和撞击的声音),而哪些资源可以根据需要加载,或者随着玩家通关而逐渐加载(语音、背景音乐、氛围循环音乐等等)。
在 Unity 中对音频进行编码时,音频在磁盘上的主要存储选项包括 PCM、ADPCM 或 Compressed。此外,还有几种特定于平台的格式,但这些格式的工作方式都类似。Unity 在导入音频时支持大部分常见格式(请参阅以下列表),并在将某个音频文件添加到项目中后,Unity 将导入此音频文件。默认模式为 _Compressed_,此模式下的音频数据压缩格式为 Vorbis/MP3(对于独立平台和移动平台)或 HEVAG/XMA(对于 PS Vita 和 Xbox One)。
有关压缩格式以及音频数据编码时可以使用的其他选项的详细描述,请参阅音频剪辑文档。
导入到 Unity 中的任何音频文件都可以作为__音频剪辑__实例在脚本中获取,因此可让音频系统在游戏运行时能够访问经过编码的音频数据。游戏可以在实际音频数据加载之前通过音频剪辑来访问有关音频数据的元数据信息。之所以能实现这一点,是因为导入过程已从编码的音频数据中提取了各种信息,例如长度、声道数和采样率,并将其存储在音频剪辑中。这在创建自动对话或音乐排序系统时可能很有用,因为音乐引擎可以在实际加载数据之前使用关于长度的信息来安排音乐播放。此外,由于只需将某一时间需要的音频剪辑保留在内存中,因此还有助于减少内存使用量。
格式 | 扩展名 |
---|---|
MPEG layer 3 | .mp3 |
Ogg Vorbis | .ogg |
Microsoft Wave | .wav |
音频交换文件格式 | .aiff / .aif |
Ultimate Soundtracker 模块 | .mod |
Impulse Tracker 模块 | .it |
Scream Tracker 模块 | .s3m |
FastTracker 2 模块 | .xm |
有关在 Unity 中使用声音的更多信息,请参阅音频概述。
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.