69 lines
1.8 KiB
C#
69 lines
1.8 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
public class SFXPlayer
|
|||
|
|
{
|
|||
|
|
private AudioSourcePool audioSourcePool;
|
|||
|
|
private List<AudioSource> activeSources = new List<AudioSource>(); // <20><><EFBFBD>ڲ<EFBFBD><DAB2>ŵ<EFBFBD> AudioSource <20>б<EFBFBD>
|
|||
|
|
|
|||
|
|
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();
|
|||
|
|
|
|||
|
|
// <20><> AudioSource <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
|
|||
|
|
activeSources.Add(source);
|
|||
|
|
}
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
|
|||
|
|
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();
|
|||
|
|
|
|||
|
|
// <20><> AudioSource <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
|
|||
|
|
activeSources.Add(source);
|
|||
|
|
|
|||
|
|
// ʹ<><CAB9>Э<EFBFBD>̴<EFBFBD><CCB4><EFBFBD><EFBFBD>ӳٺͻص<CDBB>
|
|||
|
|
CoroutineHelper.StartCoroutine(PlayCoroutine(source, delay, onComplete));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
|
|||
|
|
public void Stop()
|
|||
|
|
{
|
|||
|
|
foreach (var source in activeSources)
|
|||
|
|
{
|
|||
|
|
if (source.isPlaying)
|
|||
|
|
{
|
|||
|
|
source.Stop();
|
|||
|
|
audioSourcePool.ReturnAudioSource("SFX", source.gameObject);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
activeSources.Clear();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7>Э<EFBFBD><D0AD>
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|