16 changed files with 228 additions and 28 deletions
Binary file not shown.
Binary file not shown.
@ -0,0 +1,134 @@ |
|||
using Core.Localization; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEngine; |
|||
using Core.Settings; |
|||
using Core.Tools.Saves; |
|||
using System; |
|||
|
|||
namespace Core.Audio |
|||
{ |
|||
internal class AudioController3D |
|||
{ |
|||
internal void Init(Transform parent, Dictionary<string, AudioClip> musicAnsSounds, Dictionary<SystemLanguage, Dictionary<string, AudioClip>> voicesDictionary) |
|||
{ |
|||
_musicAndSounds = musicAnsSounds; |
|||
_voicesDictionary = voicesDictionary; |
|||
} |
|||
|
|||
private GameObject _musicParent = null; |
|||
private GameObject _soundsParent = null; |
|||
private GameObject _voicesParent = null; |
|||
|
|||
private List<AudioSource> _soundsSources = new List<AudioSource>(); |
|||
private List<AudioSource> _voicesSources = new List<AudioSource>(); |
|||
|
|||
private Dictionary<string, AudioClip> _musicAndSounds = new Dictionary<string, AudioClip>(); |
|||
private Dictionary<SystemLanguage, Dictionary<string, AudioClip>> _voicesDictionary = new Dictionary<SystemLanguage, Dictionary<string, AudioClip>>(); |
|||
|
|||
internal float SoundsVolume |
|||
{ |
|||
get => SaveManager.GetFloat("MusicVolume", 1f); |
|||
set |
|||
{ |
|||
for (int i = 0; i < _soundsSources.Count; i++) |
|||
{ |
|||
if (_soundsSources[i] != null) |
|||
_soundsSources[i].volume = CoreSettings.data.soundsVolume * value; |
|||
} |
|||
} |
|||
} |
|||
|
|||
internal float VoicesVolume |
|||
{ |
|||
get => PlayerPrefs.GetFloat("MusicVolume", 1f); |
|||
set |
|||
{ |
|||
for (int i = 0; i < _voicesSources.Count; i++) |
|||
{ |
|||
if (_voicesSources[i] != null) |
|||
_voicesSources[i].volume = CoreSettings.data.voicesVolume * value; |
|||
} |
|||
} |
|||
} |
|||
|
|||
internal float PlaySound(string name, Transform target, float minDistance, float maxDistance, bool isLoop = false) |
|||
{ |
|||
AudioSource playSource = _soundsSources.Where(s => s != null && !s.isPlaying).FirstOrDefault(); |
|||
|
|||
if (playSource == null) |
|||
{ |
|||
playSource = _soundsParent.AddComponent<AudioSource>(); |
|||
playSource.playOnAwake = false; |
|||
playSource.volume = CoreSettings.data.soundsVolume * SoundsVolume; |
|||
|
|||
_soundsSources.Add(playSource); |
|||
} |
|||
|
|||
if (_musicAndSounds.ContainsKey(name)) |
|||
{ |
|||
playSource.clip = _musicAndSounds[name]; |
|||
playSource.loop = isLoop; |
|||
playSource.minDistance = minDistance; |
|||
playSource.maxDistance = maxDistance; |
|||
playSource.transform.SetParent(target); |
|||
playSource.transform.localPosition = Vector3.zero; |
|||
playSource.Play(); |
|||
|
|||
return _musicAndSounds[name].length; |
|||
} |
|||
else |
|||
throw new ArgumentNullException($"sound \"{name}\" not found"); |
|||
} |
|||
|
|||
internal void StopSound(string name) |
|||
{ |
|||
AudioSource playSource = _soundsSources.Where(s => s != null && s.clip.name == name).FirstOrDefault(); |
|||
|
|||
if (playSource != null) |
|||
playSource.Stop(); |
|||
} |
|||
|
|||
internal float PlayVoice(string name, Transform target, float minDistance, float maxDistance) |
|||
{ |
|||
AudioSource playSource = _voicesSources.Where(s => s != null && !s.isPlaying).FirstOrDefault(); |
|||
|
|||
if (playSource == null) |
|||
{ |
|||
playSource = _voicesParent.AddComponent<AudioSource>(); |
|||
playSource.loop = false; |
|||
playSource.playOnAwake = false; |
|||
playSource.volume = CoreSettings.data.voicesVolume * VoicesVolume; |
|||
|
|||
_voicesSources.Add(playSource); |
|||
} |
|||
|
|||
if (_voicesDictionary.ContainsKey(LocalizationManager.CurrentLanguage)) |
|||
{ |
|||
if (_voicesDictionary[LocalizationManager.CurrentLanguage].ContainsKey(name)) |
|||
{ |
|||
playSource.clip = _voicesDictionary[LocalizationManager.CurrentLanguage][name]; |
|||
playSource.minDistance = minDistance; |
|||
playSource.maxDistance = maxDistance; |
|||
playSource.transform.SetParent(target); |
|||
playSource.transform.localPosition = Vector3.zero; |
|||
playSource.Play(); |
|||
|
|||
return _voicesDictionary[LocalizationManager.CurrentLanguage][name].length; |
|||
} |
|||
else |
|||
throw new ArgumentNullException($"voice \"{name}\" not found"); |
|||
} |
|||
else |
|||
throw new ArgumentNullException($"voice \"{name}\" not found"); |
|||
} |
|||
|
|||
internal void StopVoice(string name) |
|||
{ |
|||
AudioSource playSource = _voicesSources.Where(s => s != null && s.clip.name == name).FirstOrDefault(); |
|||
|
|||
if (playSource != null) |
|||
playSource.Stop(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Core.Tools |
|||
{ |
|||
public static class BufferManager |
|||
{ |
|||
private static Dictionary<string, object> _buffer = new Dictionary<string, object>(); |
|||
|
|||
public static void Add<T>(string key, T element) |
|||
{ |
|||
if (_buffer.ContainsKey(key)) |
|||
_buffer[key] = element; |
|||
else |
|||
_buffer.Add(key, element); |
|||
} |
|||
|
|||
public static T Get<T>(string key) |
|||
{ |
|||
if (_buffer.ContainsKey(key)) |
|||
return (T)_buffer[key]; |
|||
|
|||
return default(T); |
|||
} |
|||
|
|||
public static void Remove(string key) |
|||
{ |
|||
if (_buffer.ContainsKey(key)) |
|||
_buffer.Remove(key); |
|||
} |
|||
} |
|||
} |
|||
Binary file not shown.
@ -1 +1 @@ |
|||
049145f5314f464185eb4974b4434a906bbba395 |
|||
7fd1797d3498be18a922a7bf796a7fa6c67322c0 |
|||
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue