当前位置:   article > 正文

Unity 音乐播放管理器_unity音乐播放器

unity音乐播放器

MonoSingleton

  1. using UnityEngine;
  2. public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
  3. {
  4. private static T instance;
  5. public static T Instance
  6. {
  7. get
  8. {
  9. if (instance == null)
  10. {
  11. // 查找存在的实例
  12. instance = (T)FindObjectOfType(typeof(T));
  13. // 如果不存在实例,则创建
  14. if (instance == null)
  15. {
  16. // 需要创建一个游戏对象,再把这个单例组件挂载到游戏对象上
  17. var singletonObject = new GameObject();
  18. instance = singletonObject.AddComponent<T>();
  19. singletonObject.name = typeof(T).ToString() + " (Singleton)";
  20. // 让实例不在切换场景时销毁
  21. DontDestroyOnLoad(singletonObject);
  22. }
  23. }
  24. return instance;
  25. }
  26. }
  27. }

音乐播放管理器

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [System.Serializable]
  5. public class MusicList
  6. {
  7. public string listName;
  8. public List<AudioClip> clips;
  9. }
  10. [System.Serializable]
  11. public class MusicPlayer
  12. {
  13. public string playerName;
  14. public AudioSource source;
  15. public List<AudioClip> musicList;
  16. public int index;
  17. [Header("只读")]
  18. public bool isPlaying;
  19. public bool IsPlaying => source.isPlaying;
  20. public MusicPlayer(string playerName, AudioSource source, List<AudioClip> musicList, int index)
  21. {
  22. this.playerName = playerName;
  23. this.source = source;
  24. this.musicList = musicList;
  25. this.index = index;
  26. }
  27. public void UpdateStatus()
  28. {
  29. isPlaying = IsPlaying;
  30. }
  31. }
  32. public class MusicManager : MonoSingleton<MusicManager>
  33. {
  34. public List<MusicList> musicLists;
  35. AudioClip GetAudioClip(string listName,int index)
  36. {
  37. foreach (var i in musicLists)
  38. {
  39. if (i.listName == listName)
  40. {
  41. return i.clips[index];
  42. }
  43. }
  44. return null;
  45. }
  46. public List<MusicPlayer> players;
  47. public MusicPlayer GetMusicPlayer(string playerName)
  48. {
  49. foreach (var i in players)
  50. {
  51. if (i.playerName == playerName)
  52. {
  53. return i;
  54. }
  55. }
  56. return null;
  57. }
  58. void RegisterMusicPlayer(string listName, int index, int volume, bool playRightNow, bool loop)
  59. {
  60. GameObject obj = new GameObject(listName);
  61. obj.transform.SetParent(transform);
  62. AudioSource source = obj.AddComponent<AudioSource>();
  63. foreach (var i in musicLists)
  64. {
  65. if (i.listName == listName)
  66. {
  67. source.clip = i.clips[index];
  68. source.volume = volume;
  69. source.playOnAwake = false;
  70. source.loop = loop;
  71. MusicPlayer player = new MusicPlayer(listName, source, i.clips, index);
  72. players.Add(player);
  73. if (playRightNow)
  74. PlayMusic(listName);
  75. }
  76. }
  77. }
  78. public void PlayMusic(string playerName)
  79. {
  80. foreach (var i in players)
  81. {
  82. if (i.playerName == playerName)
  83. {
  84. i.source.Play();
  85. i.UpdateStatus();
  86. }
  87. }
  88. }
  89. public void PauseMusic(string playerName)
  90. {
  91. foreach (var i in players)
  92. {
  93. if (i.playerName == playerName)
  94. {
  95. i.source.Pause();
  96. i.UpdateStatus();
  97. }
  98. }
  99. }
  100. public void SetVolume(string playerName,float volume)
  101. {
  102. foreach (var i in players)
  103. {
  104. if (i.playerName == playerName)
  105. {
  106. i.source.volume = volume;
  107. }
  108. }
  109. }
  110. public void PlayNextMusic(string playerName)
  111. {
  112. foreach (var i in players)
  113. {
  114. if (i.playerName == playerName)
  115. {
  116. i.index++;
  117. if (i.index >= i.musicList.Count)
  118. i.index = 0;
  119. i.source.clip = GetAudioClip(playerName, i.index);
  120. i.source.Play();
  121. }
  122. }
  123. }
  124. public void PlayLastMusic(string playerName)
  125. {
  126. foreach (var i in players)
  127. {
  128. if (i.playerName == playerName)
  129. {
  130. i.index--;
  131. if (i.index < 0)
  132. i.index = i.musicList.Count - 1;
  133. i.source.clip = GetAudioClip(playerName, i.index);
  134. i.source.Play();
  135. }
  136. }
  137. }
  138. void Start()
  139. {
  140. RegisterMusicPlayer("BackgroundMusic", 0, 1, true, true);
  141. RegisterMusicPlayer("EnvironmentMusic", 0, 1, false, true);
  142. }
  143. void Update()
  144. {
  145. if (Input.GetKeyDown(KeyCode.RightArrow))
  146. {
  147. PlayNextMusic("BackgroundMusic");
  148. }
  149. if (Input.GetKeyDown(KeyCode.LeftArrow))
  150. {
  151. PlayLastMusic("BackgroundMusic");
  152. }
  153. if (Input.GetKeyDown(KeyCode.UpArrow))
  154. {
  155. PlayMusic("BackgroundMusic");
  156. }
  157. if (Input.GetKeyDown(KeyCode.DownArrow))
  158. {
  159. PauseMusic("BackgroundMusic");
  160. }
  161. }
  162. }

使用方法

在音乐列表添加音乐

然后使用脚本提供的API,注册播放器并使用

效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/118408?site
推荐阅读
相关标签
  

闽ICP备14008679号