1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Events;
    5. public class MusicMgr : BaseManager<MusicMgr>
    6. {
    7. //唯一的背景音乐组件
    8. private AudioSource bkMusic = null;
    9. //音乐大小
    10. private float bkValue = 1;
    11. //音效依附对象
    12. private GameObject soundObj = null;
    13. //音效列表
    14. private List<AudioSource> soundList = new List<AudioSource>();
    15. //音效大小
    16. private float soundValue = 1;
    17. public MusicMgr()
    18. {
    19. MonoMgr.GetInstance().AddUpdateListener(Update);
    20. }
    21. private void Update()
    22. {
    23. for( int i = soundList.Count - 1; i >=0; --i )
    24. {
    25. if(!soundList[i].isPlaying)
    26. {
    27. GameObject.Destroy(soundList[i]);
    28. soundList.RemoveAt(i);
    29. }
    30. }
    31. }
    32. /// <summary>
    33. /// 播放背景音乐
    34. /// </summary>
    35. /// <param name="name"></param>
    36. public void PlayBkMusic(string name)
    37. {
    38. if(bkMusic == null)
    39. {
    40. GameObject obj = new GameObject();
    41. obj.name = "BkMusic";
    42. bkMusic = obj.AddComponent<AudioSource>();
    43. }
    44. //异步加载背景音乐 加载完成后 播放
    45. ResMgr.GetInstance().LoadAsync<AudioClip>("Music/BK/" + name, (clip) =>
    46. {
    47. bkMusic.clip = clip;
    48. bkMusic.loop = true;
    49. bkMusic.volume = bkValue;
    50. bkMusic.Play();
    51. });
    52. }
    53. /// <summary>
    54. /// 暂停背景音乐
    55. /// </summary>
    56. public void PauseBKMusic()
    57. {
    58. if (bkMusic == null)
    59. return;
    60. bkMusic.Pause();
    61. }
    62. /// <summary>
    63. /// 停止背景音乐
    64. /// </summary>
    65. public void StopBKMusic()
    66. {
    67. if (bkMusic == null)
    68. return;
    69. bkMusic.Stop();
    70. }
    71. /// <summary>
    72. /// 改变背景音乐 音量大小
    73. /// </summary>
    74. /// <param name="v"></param>
    75. public void ChangeBKValue(float v)
    76. {
    77. bkValue = v;
    78. if (bkMusic == null)
    79. return;
    80. bkMusic.volume = bkValue;
    81. }
    82. /// <summary>
    83. /// 播放音效
    84. /// </summary>
    85. public void PlaySound(string name, bool isLoop, UnityAction<AudioSource> callBack = null)
    86. {
    87. if(soundObj == null)
    88. {
    89. soundObj = new GameObject();
    90. soundObj.name = "Sound";
    91. }
    92. //当音效资源异步加载结束后 再添加一个音效
    93. ResMgr.GetInstance().LoadAsync<AudioClip>("Music/Sound/" + name, (clip) =>
    94. {
    95. AudioSource source = soundObj.AddComponent<AudioSource>();
    96. source.clip = clip;
    97. source.loop = isLoop;
    98. source.volume = soundValue;
    99. source.Play();
    100. soundList.Add(source);
    101. if(callBack != null)
    102. callBack(source);
    103. });
    104. }
    105. /// <summary>
    106. /// 改变音效声音大小
    107. /// </summary>
    108. /// <param name="value"></param>
    109. public void ChangeSoundValue( float value )
    110. {
    111. soundValue = value;
    112. for (int i = 0; i < soundList.Count; ++i)
    113. soundList[i].volume = value;
    114. }
    115. /// <summary>
    116. /// 停止音效
    117. /// </summary>
    118. public void StopSound(AudioSource source)
    119. {
    120. if( soundList.Contains(source) )
    121. {
    122. soundList.Remove(source);
    123. source.Stop();
    124. GameObject.Destroy(source);
    125. }
    126. }
    127. }