GameSoundManager.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. //声音的控制
  6. public class GameSoundManager{
  7. private static GameSoundManager _instance;
  8. //环境音乐是多个的时候 就要写一个List了
  9. private AudioSource bgSound;
  10. //所有的非预制件的音效的
  11. private List<AudioSource> effectSoundList;
  12. //所有的非预制件的音效的
  13. private List<AudioSource> environmentSoundList;
  14. public static GameSoundManager Instance
  15. {
  16. get
  17. {
  18. if (_instance == null) {
  19. _instance = new GameSoundManager ();
  20. }
  21. return _instance;
  22. }
  23. }
  24. private GameSoundManager()
  25. {
  26. effectSoundList = new List<AudioSource>();
  27. environmentSoundList = new List<AudioSource> ();
  28. }
  29. public void Init()
  30. {
  31. }
  32. public void RefreshBgSound()
  33. {
  34. foreach(AudioSource source in environmentSoundList)
  35. {
  36. RefreshBGSource (source);
  37. }
  38. }
  39. public void RefreshEffectSound()
  40. {
  41. foreach(AudioSource source in effectSoundList)
  42. {
  43. RefreshSource (source);
  44. }
  45. }
  46. private void RefreshSource(AudioSource source)
  47. {
  48. if (GameDataSetting.Instance.EffectSoundSwitch) {
  49. source.volume = GameDataSetting.Instance.EffectSoundValue;//音量大小
  50. } else {
  51. source.volume = 0;
  52. }
  53. }
  54. private void RefreshBGSource(AudioSource source)
  55. {
  56. if (GameDataSetting.Instance.BGSoundSwitch) {
  57. source.volume = GameDataSetting.Instance.BgSoundValue;//音量大小
  58. } else {
  59. source.volume = 0;
  60. }
  61. }
  62. //缓解音乐注册
  63. public void RegisterBG(AudioSource source, GameObject obj)
  64. {
  65. if (!environmentSoundList.Contains(source)) {
  66. environmentSoundList.Add(source);
  67. }
  68. RefreshBGSource (source);
  69. }
  70. //环境音乐注销
  71. public void UnRegisterBG(AudioSource source)
  72. {
  73. if (environmentSoundList.Contains(source)) {
  74. environmentSoundList.Remove(source);
  75. }
  76. }
  77. //音效注册
  78. public void Register(AudioSource source, GameObject obj)
  79. {
  80. if (!effectSoundList.Contains(source)) {
  81. effectSoundList.Add(source);
  82. }
  83. RefreshSource (source);
  84. }
  85. //音效注销
  86. public void UnRegister(AudioSource source)
  87. {
  88. if (effectSoundList.Contains(source)) {
  89. effectSoundList.Remove(source);
  90. }
  91. }
  92. }