SamsonGame/Assets/Scripts/Managers/Sounds/Sound.cs

157 lines
4.3 KiB
C#
Raw Permalink Normal View History

2021-12-29 20:50:11 +03:00
using System;
using UnityEngine;
using Object = UnityEngine.Object;
namespace RND
{
public static class Sound
{
private static SoundsManager _sounds;
private static AudioListener _listener;
private static bool _isMuted;
public static void Init()
{
DestroyExtraListeners();
LoadSounds();
}
private static void LoadSounds()
{
if (_sounds == null)
_sounds = Assets.CreateImmortal<SoundsManager>("Main/SoundsManager");
_listener = _sounds.gameObject.GetComponent<AudioListener>();
}
private static void DestroyExtraListeners()
{
AudioListener[] existListeners = Object.FindObjectsOfType<AudioListener>();
if (existListeners.Length > 0)
foreach (AudioListener existListener in existListeners)
Object.Destroy(existListener);
}
public static void SetPack(int index)
{
_sounds.ChangePackBy(index);
}
public static void Mute()
{
_isMuted = true;
_listener.enabled = !_isMuted;
}
public static void Unmute()
{
_isMuted = false;
_listener.enabled = !_isMuted;
}
public static bool IsPlaying(string soundName)
{
SoundClip s = Array.Find(_sounds.Clips, sound => sound.name == soundName);
if (s == null)
throw new Exception($"Sound {soundName} not found!");
return s.Source.isPlaying;
}
public static void SetPitch(string soundName, float pitch)
{
SoundClip s = Array.Find(_sounds.Clips, sound => sound.name == soundName);
if (s == null)
throw new Exception($"Sound {soundName} not found!");
s.Source.pitch = pitch;
}
public static float GetVolume(string soundName)
{
SoundClip s = Array.Find(_sounds.Clips, sound => sound.name == soundName);
if (s == null)
throw new Exception($"Sound {soundName} not found!");
return s.volume;
}
public static void SetVolume(string soundName, float value)
{
SoundClip clip = Array.Find(_sounds.Clips, sound => sound.name == soundName);
if (clip == null)
throw new Exception($"Sound {soundName} not found!");
clip.Source.volume = value;
}
public static void SetVolume(SoundClip soundClip, float value)
{
if (soundClip == null)
throw new Exception($"Sound not found!");
soundClip.Source.volume = value;
}
public static void Play(string soundName)
{
SoundClip clip = Array.Find(_sounds.Clips, sound => sound.name == soundName);
if (clip == null)
throw new Exception($"Sound {soundName} not found!");
if (clip.disabled)
return;
clip.Source.Play();
}
/// <summary>
/// Play random sound named soundNameN, where N random between 1 and variationsCount.
/// </summary>
/// <param name="soundName">Base name of sound</param>
/// <param name="variationsCount">Suffix index</param>
public static void PlayRandomBySuffix(string soundName, int variationsCount)
{
int suffix = UnityEngine.Random.Range(1, variationsCount);
var fullName = $"{soundName}{suffix}";
SoundClip s = Array.Find(_sounds.Clips, sound => sound.name == fullName);
if (s == null)
throw new Exception($"Sound {fullName} not found!");
if (s.disabled)
return;
s.Source.Play();
}
public static void Stop(string soundName)
{
SoundClip s = Array.Find(_sounds.Clips, sound => sound.name == soundName);
if (s == null)
{
Debug.LogWarning($"Sound {soundName} not found!");
return;
}
s.Source.Stop();
}
public static void StopAll()
{
foreach (SoundClip s in _sounds.Clips)
{
if (s.Source.isPlaying)
s.Source.Stop();
}
}
}
}