AudioCore0.1
This commit is contained in:
56
Assets/04.AudioCore/RunTime/AudioCoreManager.cs
Normal file
56
Assets/04.AudioCore/RunTime/AudioCoreManager.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class AudioCoreManager
|
||||
{
|
||||
private static AudioSourcePool audioSourcePool;
|
||||
private static VoicePlayer Voice;
|
||||
private static SFXPlayer SFX;
|
||||
private static MusicPlayer Music;
|
||||
|
||||
// 初始化 AudioCoreManager
|
||||
static AudioCoreManager()
|
||||
{
|
||||
audioSourcePool = new AudioSourcePool();
|
||||
// 初始化播放器
|
||||
Voice = new VoicePlayer(audioSourcePool);
|
||||
SFX = new SFXPlayer(audioSourcePool);
|
||||
Music = new MusicPlayer(audioSourcePool);
|
||||
}
|
||||
|
||||
// 播放语音
|
||||
public static void PlayVoice(AudioClip clip, float volume = 1f, System.Action onComplete = null, float delay = 0f)
|
||||
{
|
||||
Voice.Play(clip, volume, onComplete, delay);
|
||||
}
|
||||
|
||||
// 停止语音
|
||||
public static void StopVoice()
|
||||
{
|
||||
Voice.Stop();
|
||||
}
|
||||
|
||||
// 播放音效
|
||||
public static void PlaySFX(AudioClip clip, float volume = 1f, System.Action onComplete = null, float delay = 0f)
|
||||
{
|
||||
SFX.Play(clip, volume, onComplete, delay);
|
||||
}
|
||||
|
||||
// 停止所有音效
|
||||
public static void StopAllSFX()
|
||||
{
|
||||
SFX.Stop();
|
||||
}
|
||||
|
||||
// 播放背景音乐
|
||||
public static void PlayMusic(AudioClip clip, float fadeDuration = 0f)
|
||||
{
|
||||
Music.Play(clip, fadeDuration);
|
||||
}
|
||||
|
||||
// 停止背景音乐
|
||||
public static void StopMusic(float fadeDuration = 1f)
|
||||
{
|
||||
Music.Stop(fadeDuration);
|
||||
}
|
||||
}
|
||||
11
Assets/04.AudioCore/RunTime/AudioCoreManager.cs.meta
Normal file
11
Assets/04.AudioCore/RunTime/AudioCoreManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d2dc9112fdb158489cae641ffcec61e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
136
Assets/04.AudioCore/RunTime/AudioSourcePool.cs
Normal file
136
Assets/04.AudioCore/RunTime/AudioSourcePool.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class AudioSourcePool
|
||||
{
|
||||
// 对象池字典,存储类型和对应的 GameObject 队列
|
||||
private Dictionary<string, Queue<GameObject>> poolDict = new Dictionary<string, Queue<GameObject>>();
|
||||
private GameObject poolObject;
|
||||
|
||||
/// <summary>
|
||||
/// 对象池初始化
|
||||
/// </summary>
|
||||
private void PoolAwake()
|
||||
{
|
||||
// 检查是否已经存在一个名为"AudioSourcePool"的对象
|
||||
SceneManager.sceneUnloaded += OnSceneUnloaded;
|
||||
poolObject = GameObject.Find("AudioSourcePool");
|
||||
if (poolObject == null)
|
||||
{
|
||||
// 如果不存在,创建一个新对象
|
||||
poolObject = new GameObject("AudioSourcePool");
|
||||
}
|
||||
CoroutineHelper.SetRunner();
|
||||
|
||||
// 初始化 Voice 池(最多 1 个)
|
||||
poolDict["Voice"] = new Queue<GameObject>();
|
||||
CreateAudioSource("Voice");
|
||||
|
||||
// 初始化 Music 池(最多 2 个)
|
||||
poolDict["Music"] = new Queue<GameObject>();
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
CreateAudioSource("Music");
|
||||
}
|
||||
|
||||
// 初始化 SFX 池(初始 4 个,可动态扩展)
|
||||
poolDict["SFX"] = new Queue<GameObject>();
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
CreateAudioSource("SFX");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建对象
|
||||
/// </summary>
|
||||
/// <param name="type">定义对象类型</param>
|
||||
private void CreateAudioSource(string type)
|
||||
{
|
||||
GameObject newObject = new GameObject($"AudioSource_{type}");
|
||||
newObject.transform.SetParent(poolObject.transform); // 将新对象作为当前对象的子对象
|
||||
newObject.AddComponent<AudioSource>().playOnAwake = false; // 添加 AudioSource 组件并禁用自动播放
|
||||
poolDict[type].Enqueue(newObject);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取对象
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public AudioSource GetAudioSource(string type)
|
||||
{
|
||||
if(poolObject == null)
|
||||
{
|
||||
PoolAwake();
|
||||
}
|
||||
|
||||
if (!poolDict.ContainsKey(type))
|
||||
{
|
||||
Debug.LogError($"对象池中不存在类型: {type}");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (poolDict[type].Count == 0)
|
||||
{
|
||||
/*// 如果池为空,动态创建新的 GameObject(仅限 SFX)
|
||||
if (type == "SFX")
|
||||
{
|
||||
CreateAudioSource(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"对象池 {type} 已用完,无法分配新的 AudioSource");
|
||||
return null;
|
||||
}*/
|
||||
|
||||
CreateAudioSource(type);
|
||||
}
|
||||
|
||||
GameObject audioObject = poolDict[type].Dequeue();
|
||||
AudioSource audioSource = audioObject.GetComponent<AudioSource>();
|
||||
return audioSource;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 回收对象
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="audioObject"></param>
|
||||
public void ReturnAudioSource(string type, GameObject audioObject)
|
||||
{
|
||||
if (!poolDict.ContainsKey(type))
|
||||
{
|
||||
Debug.LogError($"对象池中不存在类型: {type}");
|
||||
return;
|
||||
}
|
||||
|
||||
AudioSource audioSource = audioObject.GetComponent<AudioSource>();
|
||||
audioSource.Stop(); // 停止播放
|
||||
audioSource.clip = null; // 清空音频剪辑
|
||||
audioSource.volume = 1f; // 音量大小恢复
|
||||
poolDict[type].Enqueue(audioObject); // 回收到对象池
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 场景销毁时清空对象池
|
||||
/// </summary>
|
||||
/// <param name="scene"></param>
|
||||
void OnSceneUnloaded(Scene scene)
|
||||
{
|
||||
foreach (var pair in poolDict)
|
||||
{
|
||||
Queue<GameObject> queue = pair.Value;
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
GameObject obj = queue.Dequeue();
|
||||
if(obj != null)
|
||||
{
|
||||
UnityEngine.Object.Destroy(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
poolDict.Clear();
|
||||
}
|
||||
}
|
||||
11
Assets/04.AudioCore/RunTime/AudioSourcePool.cs.meta
Normal file
11
Assets/04.AudioCore/RunTime/AudioSourcePool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0879cbfda12f434f97c3e393664b7ec
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
24
Assets/04.AudioCore/RunTime/CoroutineHelper.cs
Normal file
24
Assets/04.AudioCore/RunTime/CoroutineHelper.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Internal;
|
||||
public static class CoroutineHelper
|
||||
{
|
||||
private static CoroutineRunner coroutineRunner;
|
||||
|
||||
public static void SetRunner()
|
||||
{
|
||||
GameObject runnerObject = new GameObject("CoroutineRunner");
|
||||
coroutineRunner = runnerObject.AddComponent<CoroutineRunner>();
|
||||
}
|
||||
public static Coroutine StartCoroutine(IEnumerator coroutine)
|
||||
{
|
||||
Coroutine myCoroutine = coroutineRunner.StartCoroutine(coroutine);
|
||||
return myCoroutine;
|
||||
}
|
||||
public static void StopCoroutine(Coroutine myCoroutine)
|
||||
{
|
||||
coroutineRunner.StopCoroutine(myCoroutine);
|
||||
}
|
||||
|
||||
private class CoroutineRunner : MonoBehaviour { }
|
||||
}
|
||||
11
Assets/04.AudioCore/RunTime/CoroutineHelper.cs.meta
Normal file
11
Assets/04.AudioCore/RunTime/CoroutineHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f7491906db8d634a8aa1655c3b5621a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
45
Assets/04.AudioCore/RunTime/GenericPool.cs
Normal file
45
Assets/04.AudioCore/RunTime/GenericPool.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class GenericPool<T> where T : new()
|
||||
{
|
||||
private Queue<T> _pool = new Queue<T>();
|
||||
private int _maxSize;
|
||||
|
||||
public GenericPool(int initialSize, int maxSize)
|
||||
{
|
||||
_maxSize = maxSize;
|
||||
for (int i = 0; i < initialSize; i++)
|
||||
{
|
||||
_pool.Enqueue(new T());
|
||||
}
|
||||
}
|
||||
|
||||
// 从对象池中获取一个对象
|
||||
public T Get()
|
||||
{
|
||||
if (_pool.Count > 0)
|
||||
{
|
||||
return _pool.Dequeue();
|
||||
}
|
||||
|
||||
// 如果池为空且未达到最大大小,则创建新对象
|
||||
if (_pool.Count < _maxSize)
|
||||
{
|
||||
return new T();
|
||||
}
|
||||
|
||||
// 如果池已满,返回默认值(或抛出异常)
|
||||
return default(T);
|
||||
}
|
||||
|
||||
// 将对象回收到对象池中
|
||||
public void Return(T item)
|
||||
{
|
||||
if (item == null) return;
|
||||
|
||||
if (_pool.Count < _maxSize)
|
||||
{
|
||||
_pool.Enqueue(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/04.AudioCore/RunTime/GenericPool.cs.meta
Normal file
11
Assets/04.AudioCore/RunTime/GenericPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3c60c3d4c25c5c428dc3e5835b40ac5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
29
Assets/04.AudioCore/RunTime/IAudio.cs
Normal file
29
Assets/04.AudioCore/RunTime/IAudio.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public interface IAudio
|
||||
{
|
||||
void Play(AudioClip audioClip,float volume);
|
||||
|
||||
void Stop();
|
||||
|
||||
void Stop(float delay);
|
||||
|
||||
void EndOperation(System.Action onComplete, float delay);
|
||||
|
||||
void Set3DPosition(Vector3 vector3);
|
||||
|
||||
}
|
||||
public abstract class AbstractAudio : IAudio
|
||||
{
|
||||
public abstract void Play(AudioClip audioClip, float volume);
|
||||
|
||||
public abstract void Stop();
|
||||
|
||||
public abstract void Stop(float delay);
|
||||
|
||||
public abstract void EndOperation(System.Action onComplete, float delay);
|
||||
|
||||
public abstract void Set3DPosition(Vector3 vector3);
|
||||
}
|
||||
11
Assets/04.AudioCore/RunTime/IAudio.cs.meta
Normal file
11
Assets/04.AudioCore/RunTime/IAudio.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a45ad70b96df7ae428058f547876d158
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
64
Assets/04.AudioCore/RunTime/MusicPlayer.cs
Normal file
64
Assets/04.AudioCore/RunTime/MusicPlayer.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
public class MusicPlayer
|
||||
{
|
||||
private AudioSourcePool audioSourcePool;
|
||||
|
||||
public MusicPlayer(AudioSourcePool audioSourcePool)
|
||||
{
|
||||
this.audioSourcePool = audioSourcePool;
|
||||
}
|
||||
|
||||
public void Play(AudioClip clip, float fadeDuration)
|
||||
{
|
||||
AudioSource source = audioSourcePool.GetAudioSource("Music");
|
||||
if (source == null) return;
|
||||
|
||||
CoroutineHelper.StartCoroutine(FadeMusic(source, clip, fadeDuration));
|
||||
}
|
||||
|
||||
public void Stop(float fadeDuration)
|
||||
{
|
||||
AudioSource source = audioSourcePool.GetAudioSource("Music");
|
||||
if (source == null) return;
|
||||
|
||||
CoroutineHelper.StartCoroutine(FadeOutMusic(source, fadeDuration));
|
||||
}
|
||||
|
||||
private IEnumerator FadeMusic(AudioSource source, AudioClip clip, float fadeDuration)
|
||||
{
|
||||
yield return FadeOutMusic(source, fadeDuration);
|
||||
|
||||
source.clip = clip;
|
||||
source.Play();
|
||||
|
||||
yield return FadeInMusic(source, fadeDuration);
|
||||
}
|
||||
|
||||
private IEnumerator FadeOutMusic(AudioSource source, float fadeDuration)
|
||||
{
|
||||
float startVolume = source.volume;
|
||||
|
||||
while (source.volume > 0)
|
||||
{
|
||||
source.volume -= startVolume * Time.deltaTime / fadeDuration;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
source.Stop();
|
||||
source.volume = startVolume;
|
||||
}
|
||||
|
||||
private IEnumerator FadeInMusic(AudioSource source, float fadeDuration)
|
||||
{
|
||||
float targetVolume = source.volume;
|
||||
source.volume = 0;
|
||||
|
||||
while (source.volume < targetVolume)
|
||||
{
|
||||
source.volume += targetVolume * Time.deltaTime / fadeDuration;
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/04.AudioCore/RunTime/MusicPlayer.cs.meta
Normal file
11
Assets/04.AudioCore/RunTime/MusicPlayer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cbd824d200553654e958ab9e5ef3f040
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
69
Assets/04.AudioCore/RunTime/SFXPlayer.cs
Normal file
69
Assets/04.AudioCore/RunTime/SFXPlayer.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class SFXPlayer
|
||||
{
|
||||
private AudioSourcePool audioSourcePool;
|
||||
private List<AudioSource> activeSources = new List<AudioSource>(); // 正在播放的 AudioSource 列表
|
||||
|
||||
public SFXPlayer(AudioSourcePool audioSourcePool)
|
||||
{
|
||||
this.audioSourcePool = audioSourcePool;
|
||||
}
|
||||
|
||||
/*public override void Play(AudioClip clip, float volume = 1f)
|
||||
{
|
||||
AudioSource source = audioSourcePool.GetAudioSource("SFX");
|
||||
if (source == null) return;
|
||||
|
||||
source.clip = clip;
|
||||
source.volume = volume;
|
||||
source.Play();
|
||||
|
||||
// 将 AudioSource 加入活动列表
|
||||
activeSources.Add(source);
|
||||
}
|
||||
*/
|
||||
|
||||
// 播放音效
|
||||
public void Play(AudioClip clip, float volume = 1f, System.Action onComplete = null, float delay = 0f)
|
||||
{
|
||||
AudioSource source = audioSourcePool.GetAudioSource("SFX");
|
||||
if (source == null) return;
|
||||
|
||||
source.clip = clip;
|
||||
source.volume = volume;
|
||||
source.Play();
|
||||
|
||||
// 将 AudioSource 加入活动列表
|
||||
activeSources.Add(source);
|
||||
|
||||
// 使用协程处理延迟和回调
|
||||
CoroutineHelper.StartCoroutine(PlayCoroutine(source, delay, onComplete));
|
||||
}
|
||||
|
||||
// 停止所有音效
|
||||
public void Stop()
|
||||
{
|
||||
foreach (var source in activeSources)
|
||||
{
|
||||
if (source.isPlaying)
|
||||
{
|
||||
source.Stop();
|
||||
audioSourcePool.ReturnAudioSource("SFX", source.gameObject);
|
||||
}
|
||||
}
|
||||
activeSources.Clear();
|
||||
}
|
||||
|
||||
// 播放音效的协程
|
||||
private IEnumerator PlayCoroutine(AudioSource source, float delay, System.Action onComplete)
|
||||
{
|
||||
yield return new WaitForSeconds(source.clip.length + delay);
|
||||
|
||||
onComplete?.Invoke();
|
||||
audioSourcePool.ReturnAudioSource("SFX", source.gameObject);
|
||||
activeSources.Remove(source);
|
||||
}
|
||||
}
|
||||
11
Assets/04.AudioCore/RunTime/SFXPlayer.cs.meta
Normal file
11
Assets/04.AudioCore/RunTime/SFXPlayer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 19ed6bde81273554f89ece0a1147f33d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
65
Assets/04.AudioCore/RunTime/Test.cs
Normal file
65
Assets/04.AudioCore/RunTime/Test.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class Test : MonoBehaviour
|
||||
{
|
||||
public AudioClip clip1;
|
||||
public AudioClip clip2;
|
||||
public AudioClip clip21;
|
||||
public AudioClip clip22;
|
||||
public AudioClip clip31;
|
||||
public AudioClip clip32;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
//AudioCore.PlayerPrefs();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.A))
|
||||
{
|
||||
AudioCoreManager.PlayVoice(clip1);
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.S))
|
||||
{
|
||||
AudioCoreManager.PlayVoice(clip2);
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.D))
|
||||
{
|
||||
AudioCoreManager.PlaySFX(clip21);
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.F))
|
||||
{
|
||||
AudioCoreManager.PlaySFX(clip22);
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.G))
|
||||
{
|
||||
AudioCoreManager.PlayMusic(clip31);
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.H))
|
||||
{
|
||||
AudioCoreManager.PlayMusic(clip32);
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.Z))
|
||||
{
|
||||
AudioCoreManager.StopVoice();
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.X))
|
||||
{
|
||||
AudioCoreManager.StopAllSFX();
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.C))
|
||||
{
|
||||
AudioCoreManager.StopMusic();
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.Space))
|
||||
{
|
||||
SceneManager.LoadScene(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/04.AudioCore/RunTime/Test.cs.meta
Normal file
11
Assets/04.AudioCore/RunTime/Test.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2cca50490fb28574697c5fd3d2d37b52
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
71
Assets/04.AudioCore/RunTime/VoicePlayer.cs
Normal file
71
Assets/04.AudioCore/RunTime/VoicePlayer.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
public class VoicePlayer
|
||||
{
|
||||
private AudioSourcePool audioSourcePool;
|
||||
private AudioSource currentSource; // 当前正在播放的 AudioSource
|
||||
Coroutine myCoroutine;
|
||||
|
||||
public VoicePlayer(AudioSourcePool audioSourcePool)
|
||||
{
|
||||
this.audioSourcePool = audioSourcePool;
|
||||
}
|
||||
|
||||
/*public override void Play(AudioClip clip, float volume = 1)
|
||||
{
|
||||
// 停止当前正在播放的语音
|
||||
Stop();
|
||||
|
||||
currentSource = audioSourcePool.GetAudioSource("Voice");
|
||||
if (currentSource == null) return;
|
||||
|
||||
currentSource.clip = clip;
|
||||
currentSource.volume = volume;
|
||||
currentSource.Play();
|
||||
}
|
||||
*/
|
||||
|
||||
// 播放语音
|
||||
public void Play(AudioClip clip, float volume = 1f, System.Action onComplete = null, float delayOnComplete = 0f)
|
||||
{
|
||||
// 停止当前正在播放的语音与旧协程
|
||||
Stop();
|
||||
if(myCoroutine!= null)
|
||||
{
|
||||
CoroutineHelper.StopCoroutine(myCoroutine);
|
||||
myCoroutine = null;
|
||||
}
|
||||
currentSource = audioSourcePool.GetAudioSource("Voice");
|
||||
if (currentSource == null) return;
|
||||
|
||||
currentSource.clip = clip;
|
||||
currentSource.volume = volume;
|
||||
currentSource.Play();
|
||||
|
||||
// 使用协程处理延迟和回调
|
||||
myCoroutine = CoroutineHelper.StartCoroutine(PlayVoiceCoroutine(currentSource, delayOnComplete, onComplete));
|
||||
}
|
||||
|
||||
// 停止语音
|
||||
public void Stop()
|
||||
{
|
||||
if (currentSource != null && currentSource.isPlaying)
|
||||
{
|
||||
currentSource.Stop();
|
||||
audioSourcePool.ReturnAudioSource("Voice", currentSource.gameObject);
|
||||
currentSource = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 播放语音的协程
|
||||
private IEnumerator PlayVoiceCoroutine(AudioSource source, float delayOnComplete, System.Action onComplete)
|
||||
{
|
||||
yield return new WaitForSeconds(source.clip.length+delayOnComplete);
|
||||
|
||||
onComplete?.Invoke();
|
||||
audioSourcePool.ReturnAudioSource("Voice", source.gameObject);
|
||||
currentSource = null;
|
||||
myCoroutine = null;
|
||||
}
|
||||
}
|
||||
11
Assets/04.AudioCore/RunTime/VoicePlayer.cs.meta
Normal file
11
Assets/04.AudioCore/RunTime/VoicePlayer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff93caf4019714a4797e6986c6e5c234
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "com.staryevo.codechecker",
|
||||
"version": "1.0.0",
|
||||
"displayName": "01.CodeChecker",
|
||||
"description": "代码检查工具",
|
||||
"description": "音频播放工具",
|
||||
"unity": "2021.3",
|
||||
"unityRelease": "30f1",
|
||||
"keywords": [
|
||||
|
||||
Reference in New Issue
Block a user