You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using YooAsset;
|
|
|
|
namespace Script.FrameWork.Sound
|
|
{
|
|
public class SoundManager : Singleton<SoundManager>
|
|
{
|
|
private List<AudioSource> _audio = new();
|
|
|
|
public void PlaySound(string clipPath, bool loop = false)
|
|
{
|
|
if (string.IsNullOrEmpty(clipPath)) return;
|
|
var audioSource = _audio.Find(audio => !audio.isPlaying && audio.time <= 0);
|
|
if (audioSource == null)
|
|
{
|
|
var sound = new GameObject("sound");
|
|
audioSource = sound.AddComponent<AudioSource>();
|
|
_audio.Add(audioSource);
|
|
}
|
|
var clip = YooAssets.LoadAssetSync(clipPath).AssetObject as AudioClip;
|
|
clip.name = clipPath;
|
|
audioSource.clip = clip;
|
|
audioSource.loop = false;
|
|
audioSource.Play();
|
|
}
|
|
|
|
public void StopSound(string clipPath)
|
|
{
|
|
if (string.IsNullOrEmpty(clipPath)) return;
|
|
var audioSource = _audio.Find(audio => audio.clip != null && audio.clip.name == clipPath);
|
|
if (audioSource != null)
|
|
{
|
|
audioSource.Stop();
|
|
audioSource.time = 0;
|
|
// audioSource.clip = null;
|
|
}
|
|
}
|
|
}
|
|
} |