当前位置:   article > 正文

unity3d,声音播放_unity3d 播放声音

unity3d 播放声音

1、audioclip

把音频文件导入到unity以后,音频文件就会变成一个audioclip。


点击,可以看到属性


load type:小文件选择Decompress On Load,大的文件选Compressed In Memory

Compression Format:PCM不压缩,一般选ADPCM,背景音乐,对话啥的选Vorbis/MP3

Sample Rate Setting:一般选Optimize Sample Rate,优化采样率


上面的是根据官方文档得出的结论,不过好像实际运用中还得看具体情况。


2、AudioSource


这是unity播放声音的基本组件,关联了audioclip就能够播放了。声音播放基本是在对这个组件进行操作。


3、AudioListener

每个场景只能有一个的组件,声音播放必须的组件,默认绑在main camera上。


4、示例




点击鼠标左键,生成一个立方体,同时发出声音。(模拟子弹射击,玩家发招的声音情况)

这个声音经常使用,就绑定在控制方

  1. using UnityEngine;
  2. using System.Collections;
  3. public class ThrowCube : MonoBehaviour {
  4. public GameObject perfab;
  5. public AudioClip shoot_sound;
  6. private AudioSource audio_source;
  7. void Start () {
  8. audio_source = GetComponent<AudioSource> ();
  9. }
  10. // Update is called once per frame
  11. void Update () {
  12. if (Input.GetMouseButtonDown (0)) {
  13. audio_source.PlayOneShot (shoot_sound,GameInfo.effect_sound_volume);
  14. GameObject shoot_obj = Instantiate (perfab, transform.position, transform.rotation) as GameObject;
  15. shoot_obj.GetComponent<Rigidbody> ().AddRelativeForce (new Vector3 (Input.mousePosition.x/10, Input.mousePosition.y/5, 50f));
  16. }
  17. }
  18. }


立方体掉出屏幕,发出声音并销毁。(模拟怪物被击中的情况)

有种做法,因为这个声音使用频度也可能很高,就把声音绑着控制方,如玩家上。但是,这边为了编程省事,把声音绑着怪物上。

稍微麻烦的地方是要等声音播放完毕再销毁。

  1. using UnityEngine;
  2. using System.Collections;
  3. public class CubeControl : MonoBehaviour
  4. {
  5. public AudioClip destory_sound;
  6. private AudioSource audio_source;
  7. void Awake ()
  8. {
  9. audio_source = GetComponent<AudioSource> ();
  10. audio_source.mute = GameInfo.effect_sound_mute;
  11. }
  12. void OnBecameInvisible ()
  13. {
  14. audio_source.PlayOneShot (destory_sound,GameInfo.effect_sound_volume);
  15. InvokeRepeating ("DestorySelf", 0, .1f);
  16. }
  17. void DestorySelf(){
  18. if (!audio_source.isPlaying) {
  19. Destroy (gameObject);
  20. }
  21. }
  22. }


场景开始播放背景音乐

点击鼠标右键更换播放的背景音乐(模拟事件触发背景音改变)

  1. using UnityEngine;
  2. using System.Collections;
  3. public class BgSoundControl : MonoBehaviour
  4. {
  5. public AudioClip[] bg_sounds;
  6. private AudioSource audio_source;
  7. void Awake ()
  8. {
  9. audio_source = GetComponent<AudioSource> ();
  10. audio_source.volume = GameInfo.bg_music_volume;
  11. audio_source.clip = bg_sounds [0];
  12. }
  13. // Update is called once per frame
  14. void Update ()
  15. {
  16. if (Input.GetMouseButtonDown (1)) {
  17. audio_source.clip = bg_sounds [1];
  18. audio_source.Play ();
  19. }
  20. }
  21. }


添加音量控制

为了方便控制,把背景音乐的游戏对象方到指定的tag便于控制。

建立一个静态类存放音量信息,同时便于多方调用

单次播放的声音,在播放的时候,传入音量大小的参数控制音量,背景声音是控制audiosource的volume值控制音量

  1. public static class GameInfo {
  2. public static float effect_sound_volume;
  3. public static float bg_music_volume;
  4. public static bool effect_sound_mute;
  5. public static bool bg_music_mute;
  6. }


音量设置的类

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class SoundSetControl : MonoBehaviour
  5. {
  6. private AudioSource bg_music;
  7. public Slider sound_slider;
  8. public Slider music_slider;
  9. public Toggle sound_toggle;
  10. public Toggle music_toggle;
  11. public Button confirm_button;
  12. public Button exit_button;
  13. public AudioClip sound_clip;
  14. public AudioClip music_clip;
  15. private AudioSource audio_source;
  16. public delegate void SoundSetAction();
  17. public event SoundSetAction OnClose;
  18. // Use this for initialization
  19. void Awake ()
  20. {
  21. audio_source = GetComponent<AudioSource> ();
  22. AudioSource[] list = FindObjectsOfType<AudioSource> ();
  23. for (int i = 0; i < list.Length; i++) {
  24. if (list [i].gameObject.tag == "BackgroundMusic") {
  25. bg_music = list [i];
  26. break;
  27. }
  28. }
  29. LoadSoundSet ();
  30. SetUI ();
  31. Confirm ();
  32. gameObject.SetActive (false);
  33. }
  34. void OnEnable (){
  35. GetComponent<AudioSource> ().mute = false;
  36. }
  37. public void SetEffectSound ()
  38. {
  39. audio_source.PlayOneShot (sound_clip, sound_slider.value);
  40. sound_toggle.isOn = false;
  41. }
  42. public void SetBgMusic ()
  43. {
  44. audio_source.PlayOneShot (music_clip, music_slider.value);
  45. music_toggle.isOn = false;
  46. }
  47. //根据变量设置游戏音量
  48. public void Confirm(){
  49. GameInfo.bg_music_mute = music_toggle.isOn;
  50. GameInfo.effect_sound_mute = sound_toggle.isOn;
  51. GameInfo.bg_music_volume = music_slider.value;
  52. GameInfo.effect_sound_volume = sound_slider.value;
  53. Save ();
  54. AudioSource[] list = FindObjectsOfType<AudioSource> ();
  55. for (int i = 0; i < list.Length; i++) {
  56. list [i].mute = GameInfo.effect_sound_mute;
  57. }
  58. bg_music.mute = GameInfo.bg_music_mute;
  59. bg_music.volume = GameInfo.bg_music_volume;
  60. }
  61. public void Exit(){
  62. //SoundSetAction ();
  63. if (OnClose != null) {
  64. OnClose ();
  65. }
  66. gameObject.SetActive (false);
  67. }
  68. private void LoadSoundSet ()
  69. {
  70. if (PlayerPrefs.GetInt ("SoundMute", 1)==1) {
  71. GameInfo.effect_sound_mute = true;
  72. } else {
  73. GameInfo.effect_sound_mute = false;
  74. }
  75. if (PlayerPrefs.GetInt ("BgMusicMute", 1)==1) {
  76. GameInfo.bg_music_mute = true;
  77. } else {
  78. GameInfo.bg_music_mute = false;
  79. }
  80. GameInfo.effect_sound_volume = PlayerPrefs.GetFloat ("EffectSoundVolume", 1f);
  81. GameInfo.bg_music_volume = PlayerPrefs.GetFloat ("BgMusicVolume", 1f);
  82. }
  83. private void SetUI(){
  84. sound_slider.value = GameInfo.effect_sound_volume;
  85. sound_toggle.isOn = GameInfo.effect_sound_mute;
  86. music_slider.value = GameInfo.bg_music_volume;
  87. music_toggle.isOn = GameInfo.bg_music_mute;
  88. }
  89. private void Save(){
  90. if (GameInfo.effect_sound_mute) {
  91. PlayerPrefs.SetInt ("SoundMute", 1);
  92. } else {
  93. PlayerPrefs.SetInt ("SoundMute", 0);
  94. }
  95. if (GameInfo.bg_music_mute) {
  96. PlayerPrefs.SetInt ("BgMusicMute", 1);
  97. } else {
  98. PlayerPrefs.SetInt ("BgMusicMute",0);
  99. }
  100. PlayerPrefs.SetFloat ("EffectSoundVolume", GameInfo.effect_sound_volume);
  101. PlayerPrefs.SetFloat ("BgMusicVolume", GameInfo.bg_music_volume);
  102. }
  103. }


关闭的时候,用了事件,这样做的想法是,把这个类绑在canvas上,整个canvas可以做成预制件,方便在多个场景中使用。

事件交由统一的事件处理类,将事件交到其他类去执行。目的是解耦合。

  1. using UnityEngine;
  2. using System.Collections;
  3. public class EventManage : MonoBehaviour {
  4. private GameManage gameManage;
  5. private SoundSetControl soundSetControl;
  6. void Awake(){
  7. gameManage = FindObjectOfType<GameManage> ();
  8. soundSetControl = FindObjectOfType<SoundSetControl> ();
  9. }
  10. // Use this for initialization
  11. void Start () {
  12. soundSetControl.OnClose+=gameManage.GamePlay;
  13. }
  14. // Update is called once per frame
  15. void Update () {
  16. }
  17. }

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class GameManage : MonoBehaviour {
  5. //public delegate void GameAction();
  6. //public event GameAction OnGamePause,OnGamePlay;
  7. public GameObject sound_canvas;
  8. private ThrowCube throw_cube;
  9. private AudioSource bg_music;
  10. public Text txt;
  11. // Use this for initialization
  12. void Awake () {
  13. throw_cube = FindObjectOfType<ThrowCube> ();
  14. AudioSource[] list = FindObjectsOfType<AudioSource> ();
  15. for (int i = 0; i < list.Length; i++) {
  16. if (list [i].gameObject.tag == "BackgroundMusic") {
  17. bg_music = list [i];
  18. break;
  19. }
  20. }
  21. }
  22. // Update is called once per frame
  23. void Update () {
  24. if (Input.GetKey (KeyCode.Escape)) {
  25. sound_canvas.SetActive (true);
  26. GamePause ();
  27. }
  28. txt.text = GameInfo.bg_music_mute.ToString () + ";" + GameInfo.bg_music_volume.ToString () + ";" + GameInfo.effect_sound_mute.ToString () + ";" + GameInfo.effect_sound_volume.ToString ();
  29. }
  30. public void GamePause(){
  31. throw_cube.enabled = false;
  32. bg_music.Pause ();
  33. }
  34. public void GamePlay(){
  35. throw_cube.enabled = true;
  36. bg_music.Play ();
  37. }
  38. }


内容下载:http://download.csdn.net/detail/wuyt2008/9474143




声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号