Hello there,
I would like to share my solution to give different characters different sound sets (=voices) by using scriptable objects. Here is an example:
Here is the code for the SO:
using UnityEngine;
namespace RPG.Audio
{
[CreateAssetMenu(fileName = "SoundSet", menuName = "Sounds/New Sound Set", order = 0)]
public class SoundSet : ScriptableObject
{
[Range(0, 1)] public float volume = 1f;
public Subset[] subsets;
[System.Serializable]
public struct Subset
{
public SoundContext context;
public AudioClip[] audioClips;
}
}
}
The SoundContext is a simple enum (see stats section):
namespace RPG.Audio
{
public enum SoundContext
{
GetHit,
Footstep,
Landing,
Death
}
}
The scriptable object can then be dragged into a “Character Sounds” component, that I created:
In the CharacterSounds I convert the soundSet into a dictionary for performance and readability:
private void Awake() => BuildSoundDictionary();
private void BuildSoundDictionary()
{
foreach (var subset in soundSet.subsets)
{
if (subset.audioClips.Length == 0) continue;
soundDictionary.Add(subset.context, subset.audioClips);
}
}
… and do some randomizing magic afterwards using a modified version of this: The best way to randomize sounds in Unity 3D C# - Sonigon
If you have any questions or suggestions, please let me know.
Soro