Newer
Older
DungeonShooting / DungeonShooting_Godot / src / manager / SoundManager.cs
  1. using System;
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 音频总线 区分不同的声音 添加声音效果 目前只有背景音乐 和音效 两个bus
  6. /// </summary>
  7. public enum BUS
  8. {
  9. BGM = 0,
  10. SFX = 1
  11. }
  12.  
  13. /// <summary>
  14. /// 声音管理 背景音乐管理 音效
  15. /// </summary>
  16. public class SoundManager
  17. {
  18. public static SoundManager Instance { get => SingleTon.singleTon; }
  19.  
  20. private static class SingleTon
  21. {
  22. internal static SoundManager singleTon = new SoundManager();
  23. }
  24.  
  25. private static AudioStreamPlayer audioStreamPlayer = new AudioStreamPlayer();
  26.  
  27.  
  28. /// <summary>
  29. /// 背景音乐路径
  30. /// </summary>
  31. public static string BGMPath = "res://resource/sound/bgm/";
  32. /// <summary>
  33. /// 音效路径
  34. /// </summary>
  35. public static string SFXPath = "res://resource/sound/sfx/";
  36.  
  37. /// <summary>
  38. /// 播放声音 用于bgm
  39. /// </summary>
  40. /// <param name="soundPath">bgm名字 在resource/sound/bgm/目录下</param>
  41. /// <param name="node">需要播放声音得节点 将成为音频播放节点的父节点</param>
  42. /// <param name="volume">音量</param>
  43. public static void PlayeMusic(string soundName, Node node, float volume)
  44. {
  45. AudioStream sound = ResourceManager.Load<AudioStream>(BGMPath + soundName);
  46. if (sound != null)
  47. {
  48. AudioStreamPlayer soundNode = new AudioStreamPlayer();
  49. node.AddChild(soundNode);
  50. soundNode.Stream = sound;
  51. soundNode.Bus = Enum.GetName(typeof(BUS), 0);
  52. soundNode.VolumeDb = volume;
  53. soundNode.Play();
  54. }
  55. else
  56. {
  57. GD.Print("没有这个资源!!!");
  58. }
  59. }
  60. /// <summary>
  61. /// 添加并播放音效 用于音效
  62. /// </summary>
  63. /// <param name="soundName">音效文件名字 在resource/sound/sfx/目录下</param>
  64. /// <param name="node">需要播放声音得节点 将成为音频播放节点的父节点</param>
  65. /// <param name="volume">音量</param>
  66. public static void PlaySoundEffect(string soundName, Node node, float volume = 0f)
  67. {
  68. AudioStream sound = ResourceManager.Load<AudioStream>(SFXPath + soundName);
  69. if (sound != null)
  70. {
  71. AudioStreamPlayer soundNode = new AudioStreamPlayer();
  72. node.AddChild(soundNode);
  73. soundNode.Stream = sound;
  74. soundNode.Bus = Enum.GetName(typeof(BUS), 1);
  75. soundNode.VolumeDb = volume;
  76. soundNode.Play();
  77. GD.Print("bus:", soundNode.Bus);
  78. }
  79. else
  80. {
  81. GD.Print("没有这个资源!!!");
  82. }
  83. }
  84. }