Skip to Content
《Unity 开发食谱》第二版
book

《Unity 开发食谱》第二版

by Paris Buttfield-Addison, Jon Manning, Tim Nugent
May 2025
Beginner to intermediate
432 pages
5h 53m
Chinese
O'Reilly Media, Inc.
Content preview from 《Unity 开发食谱》第二版

第 11 章 声音与音乐 声音与音乐

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

音频是互动体验中最重要的部分之一,尤其是在制作游戏时。 Unity 拥有多种工具,可用于简单和复杂的音频设置。在本章中,我们将讨论如何在场景中使用音频的基础知识,以及控制项目音效的更高级方法。

警告

声音和音乐的重要性远远超出您的想象。如果您要制作一款游戏,请不要忽略音效、音乐和音频。

与其他章节一样,本章远非详尽无遗的秘诀集,它涵盖了在 Unity 中使用音频可能需要做的所有事情。相反,我们试图涵盖作为 Unity 开发人员,我们发现自己在一个又一个游戏、一个又一个项目中反复实施的内容。

11.1 播放声音

问题

您想在项目中播放声音和音乐。

解决方案

要播放音频片段,首先需要一个音频文件。如果您已经有了一个音频文件,可将其拖放到项目中。

提示

如果您的游戏需要音效,您可以在一些地方找到免费音效。 Kenney收集了大量公共领域的音效,而像Freesound这样的社区网站则拥有更多的音效素材和音效质量。你也可以自己制作。Bfxr是一款适用于 Windows 和 Mac 的免费应用程序,可以生成复古风格的音效。Humble Bundle也经常为大量游戏资源提供优惠。

将音频资产添加到项目后,选择音频资产并查看 "检查器"(Inspector(图 11-1),即可配置 Unity 如何导入音频资产。Unity 支持.aif.wav.mp3.ogg音频。它还支持.xm.mod.it.s3m 等跟踪器格式。Unity 会将音频转换为您指定的格式。

udc2 1101
图 11-1. 音频片段资产的导入设置

导入音频片段后,就可以开始播放了。在 Unity 中,音频通过连接到游戏对象的AudioSource 组件播放。 创建AudioSource 组件:

  1. 打开 GameObject 菜单,选择音频 → 音频源。将创建一个新的游戏对象,并附加AudioSource 组件。

    备注

    您也可以将AudioSource 组件添加到现有对象中,方法是选择游戏对象,单击检查器底部的 "添加组件 "按钮,然后选择音频 → 音源。

  2. 有了音频源后,将要播放的音频片段从 "项目 "选项卡拖入音频片段槽(图 11-2)。

  3. 在 Unity 中播放该项目。音频将开始播放。

udc2 1102
图 11-2. AudioSource 组件的检查器
提示

您可以通过启用 "唤醒时播放 "选项来配置音频源是否在对象首次激活时开始播放。

讨论

当正在播放音频的AudioSource 组件处于AudioListener 组件的范围内时,播放器的扬声器或耳机就会播放音频。创建新场景时,所有对象中的主摄像机对象都会附加一个AudioListener 组件。

备注

场景中一次只能有一个AudioListener 。如果超过一个,Unity 会发出警告,并且只有一个能正常工作。

11.2 设置混音器

问题

您希望在一个中央位置管理项目播放音频的电平和设置。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer
The Human Factor in AI-Based Decision-Making

The Human Factor in AI-Based Decision-Making

Philip Meissner, Christoph Keding
Coaching for High Performance

Coaching for High Performance

MIT Sloan Management Review

Publisher Resources

ISBN: 9798341659384