88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.Audio;
|
|
|
|
namespace Hallucinate.Audio
|
|
{
|
|
[Serializable]
|
|
public class AudioSample
|
|
{
|
|
[ValidateInput(nameof(HasName), "Audio sample needs a lookup name.")]
|
|
public string Name;
|
|
|
|
[Required]
|
|
public AudioClip Clip;
|
|
|
|
[PropertyRange(0f, 1f)]
|
|
public float DefaultVolume = 1f;
|
|
|
|
[PropertyRange(0.1f, 3f)]
|
|
public float DefaultPitch = 1f;
|
|
|
|
public AudioMixerGroup MixerGroup;
|
|
|
|
private bool HasName(string value)
|
|
{
|
|
return !string.IsNullOrWhiteSpace(value);
|
|
}
|
|
}
|
|
|
|
[CreateAssetMenu(fileName = "AudioDatabase", menuName = "BABA_YAGA/Audio/Audio Database")]
|
|
public class AudioDatabase : ScriptableObject
|
|
{
|
|
[Searchable]
|
|
[TableList]
|
|
[SerializeField] private List<AudioSample> samples = new List<AudioSample>();
|
|
|
|
[ShowInInspector]
|
|
[ReadOnly]
|
|
[BoxGroup("Runtime")]
|
|
public int SampleCount => samples?.Count ?? 0;
|
|
|
|
private Dictionary<string, AudioSample> _sampleCache;
|
|
|
|
[Button("Rebuild Lookup Cache")]
|
|
public void Initialize()
|
|
{
|
|
_sampleCache = new Dictionary<string, AudioSample>();
|
|
foreach (var sample in samples)
|
|
{
|
|
if (!string.IsNullOrEmpty(sample.Name) && !_sampleCache.ContainsKey(sample.Name))
|
|
{
|
|
_sampleCache.Add(sample.Name, sample);
|
|
}
|
|
}
|
|
}
|
|
|
|
public AudioSample GetSample(string name)
|
|
{
|
|
if (_sampleCache == null) Initialize();
|
|
|
|
if (_sampleCache.TryGetValue(name, out var sample))
|
|
{
|
|
return sample;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public IEnumerable<string> GetSampleNames()
|
|
{
|
|
foreach (var sample in samples)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(sample.Name))
|
|
{
|
|
yield return sample.Name;
|
|
}
|
|
}
|
|
}
|
|
|
|
[Button("Log Sample Names")]
|
|
private void LogSampleNames()
|
|
{
|
|
Debug.Log($"{name}: {string.Join(", ", GetSampleNames())}", this);
|
|
}
|
|
}
|
|
}
|