赞
踩
MonoSingleton
- using UnityEngine;
- public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
- {
- private static T instance;
-
- public static T Instance
- {
- get
- {
- if (instance == null)
- {
- // 查找存在的实例
- instance = (T)FindObjectOfType(typeof(T));
-
- // 如果不存在实例,则创建
- if (instance == null)
- {
- // 需要创建一个游戏对象,再把这个单例组件挂载到游戏对象上
- var singletonObject = new GameObject();
- instance = singletonObject.AddComponent<T>();
- singletonObject.name = typeof(T).ToString() + " (Singleton)";
-
- // 让实例不在切换场景时销毁
- DontDestroyOnLoad(singletonObject);
- }
- }
-
- return instance;
- }
- }
- }
音乐播放管理器
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- [System.Serializable]
- public class MusicList
- {
- public string listName;
- public List<AudioClip> clips;
- }
-
- [System.Serializable]
- public class MusicPlayer
- {
- public string playerName;
- public AudioSource source;
- public List<AudioClip> musicList;
- public int index;
-
- [Header("只读")]
- public bool isPlaying;
- public bool IsPlaying => source.isPlaying;
-
- public MusicPlayer(string playerName, AudioSource source, List<AudioClip> musicList, int index)
- {
- this.playerName = playerName;
- this.source = source;
- this.musicList = musicList;
- this.index = index;
- }
-
- public void UpdateStatus()
- {
- isPlaying = IsPlaying;
- }
- }
-
- public class MusicManager : MonoSingleton<MusicManager>
- {
- public List<MusicList> musicLists;
-
- AudioClip GetAudioClip(string listName,int index)
- {
- foreach (var i in musicLists)
- {
- if (i.listName == listName)
- {
- return i.clips[index];
- }
- }
- return null;
- }
-
- public List<MusicPlayer> players;
-
- public MusicPlayer GetMusicPlayer(string playerName)
- {
- foreach (var i in players)
- {
- if (i.playerName == playerName)
- {
- return i;
- }
- }
- return null;
- }
-
- void RegisterMusicPlayer(string listName, int index, int volume, bool playRightNow, bool loop)
- {
- GameObject obj = new GameObject(listName);
- obj.transform.SetParent(transform);
-
- AudioSource source = obj.AddComponent<AudioSource>();
-
- foreach (var i in musicLists)
- {
- if (i.listName == listName)
- {
- source.clip = i.clips[index];
- source.volume = volume;
- source.playOnAwake = false;
- source.loop = loop;
- MusicPlayer player = new MusicPlayer(listName, source, i.clips, index);
- players.Add(player);
-
- if (playRightNow)
- PlayMusic(listName);
- }
- }
- }
-
- public void PlayMusic(string playerName)
- {
- foreach (var i in players)
- {
- if (i.playerName == playerName)
- {
- i.source.Play();
- i.UpdateStatus();
- }
- }
- }
-
- public void PauseMusic(string playerName)
- {
- foreach (var i in players)
- {
- if (i.playerName == playerName)
- {
- i.source.Pause();
- i.UpdateStatus();
- }
- }
- }
-
- public void SetVolume(string playerName,float volume)
- {
- foreach (var i in players)
- {
- if (i.playerName == playerName)
- {
- i.source.volume = volume;
- }
- }
- }
-
- public void PlayNextMusic(string playerName)
- {
- foreach (var i in players)
- {
- if (i.playerName == playerName)
- {
- i.index++;
- if (i.index >= i.musicList.Count)
- i.index = 0;
- i.source.clip = GetAudioClip(playerName, i.index);
- i.source.Play();
- }
- }
- }
-
- public void PlayLastMusic(string playerName)
- {
- foreach (var i in players)
- {
- if (i.playerName == playerName)
- {
- i.index--;
- if (i.index < 0)
- i.index = i.musicList.Count - 1;
- i.source.clip = GetAudioClip(playerName, i.index);
- i.source.Play();
- }
- }
- }
-
- void Start()
- {
- RegisterMusicPlayer("BackgroundMusic", 0, 1, true, true);
- RegisterMusicPlayer("EnvironmentMusic", 0, 1, false, true);
- }
-
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.RightArrow))
- {
- PlayNextMusic("BackgroundMusic");
- }
-
- if (Input.GetKeyDown(KeyCode.LeftArrow))
- {
- PlayLastMusic("BackgroundMusic");
- }
-
- if (Input.GetKeyDown(KeyCode.UpArrow))
- {
- PlayMusic("BackgroundMusic");
- }
-
- if (Input.GetKeyDown(KeyCode.DownArrow))
- {
- PauseMusic("BackgroundMusic");
- }
- }
- }
使用方法
在音乐列表添加音乐
然后使用脚本提供的API,注册播放器并使用
效果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。