查看: 1517|回复: 3
打印 上一主题 下一主题

Simple AudioManager code for your Unity3d project,声音管理器(转)

[复制链接]

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-8-3 17:42:43 |只看该作者 |倒序浏览
老外写的一篇关于在unity3d中进行音频管理的文章
You know, sometimes very simple code has to be written again and again by lots of different people to solve the same basic problem. I think playing audio is one of such cases in Unity3d.
You have the audio components: AudioSource (attached to your sound emitter) and AudioListener (attached to the main camera). Now, the problem is that audio clips are represented by the AudioClip class, and each AudioSource can have a single AudioClip playing at the moment. That is sometimes ok, but other times, you want your game object to be able to play a number of different sounds, maybe at the same time (Think of a character playing footsteps sounds and maybe saying something at the same time).
Since it is very natural to have one manager object in your scene acting as a singleton, I’ve written a component that can be attached to such manager. The work of this component is to play almost any sound you want to play in your game. The code is very simple and self explationary.
01    // /////////////////////////////////////////////////////////////

02    //

03    // Audio Manager.

04    //

05    // This code is release under the MIT licence. It is provided as-is and without any warranty.

06    //

07    // Developed by Daniel Rodríguez (Seth Illgard) in April 2010

08    // http://www.silentkraken.com

09    //

10    // ///////////////////////////////////////////////////////////////

11     

12    using UnityEngine;

13    using System.Collections;

14     

15    public class AudioManager : MonoBehaviour

16    {

17        public AudioSource Play(AudioClip clip, Transform emitter)

18        {

19            return Play(clip, emitter, 1f, 1f);

20        }

21     

22        public AudioSource Play(AudioClip clip, Transform emitter, float volume)

23        {

24            return Play(clip, emitter, volume, 1f);

25        }

26     

27        /// <summary>

28        /// Plays a sound by creating an empty game object with an AudioSource

29        /// and attaching it to the given transform (so it moves with the transform). Destroys it after it finished playing.

30        /// </summary>

31        /// <param name="clip"></param>

32        /// <param name="emitter"></param>

33        /// <param name="volume"></param>

34        /// <param name="pitch"></param>

35        /// <returns></returns>

36        public AudioSource Play(AudioClip clip, Transform emitter, float volume, float pitch)

37        {

38            //Create an empty game object

39            GameObject go = new GameObject ("Audio: " +  clip.name);

40            go.transform.position = emitter.position;

41            go.transform.parent = emitter;

42     

43            //Create the source

44            AudioSource source = go.AddComponent<AudioSource>();

45            source.clip = clip;

46            source.volume = volume;

47            source.pitch = pitch;

48            source.Play ();

49            Destroy (go, clip.length);

50            return source;

51        }

52     

53        public AudioSource Play(AudioClip clip, Vector3 point)

54        {

55            return Play(clip, point, 1f, 1f);

56        }

57     

58        public AudioSource Play(AudioClip clip, Vector3 point, float volume)

59        {

60            return Play(clip, point, volume, 1f);

61        }

62     

63        /// <summary>

64        /// Plays a sound at the given point in space by creating an empty game object with an AudioSource

65        /// in that place and destroys it after it finished playing.

66        /// </summary>

67        /// <param name="clip"></param>

68        /// <param name="point"></param>

69        /// <param name="volume"></param>

70        /// <param name="pitch"></param>

71        /// <returns></returns>

72        public AudioSource Play(AudioClip clip, Vector3 point, float volume, float pitch)

73        {

74            //Create an empty game object

75            GameObject go = new GameObject("Audio: " + clip.name);

76            go.transform.position = point;

77     

78            //Create the source

79            AudioSource source = go.AddComponent<AudioSource>();

80            source.clip = clip;

81            source.volume = volume;

82            source.pitch = pitch;

83            source.Play();

84            Destroy(go, clip.length);

85            return source;

86        }

87    }


As you can see, this class is basically 6 overloads for the Play function. Basically we have two types: the type that takes a transform (emitter) as parameter and the type that takes a Vector3. The only difference is that if you provide a point (Vector3), the sound will be played there, but if you provide an emitter, the sound will be played where the emitter is, but will also follow the emitter as it moves (e.g. the engine of a passing car).

Hope you find it useful!

By the way, I’m planning on creating on tutorial on how to create Manager systems. By that I don’t mean simple components like this but rather the best way I have found to create such a Singleton object with those components, and how to integrate that with your game when you have multiple game states and game scenes (with potentially different managers). More on that soon.
原文地址:http://www.blog.silentkraken.com/2010/04/06/audiomanager/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-8-3 17:54:50 |只看该作者
我爱纳金网~www.narkii.com
回复

使用道具 举报

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-12-30 15:48:42 |只看该作者
学习了,虽然还是有难度,谢谢楼主的用心  
回复

使用道具 举报

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38268
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-12-31 01:51:00 |只看该作者
谢谢楼主的帖子分享,学习了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-23 23:59 , Processed in 0.062016 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部