I am aware this topic has been covered, but unless I’m missing something, I have covered all the bases my predecessors recommend.
I have an audio manager that creates an array of sounds via the inspector. Originally I was using strings as identifiers, but it looked ugly so I decided to switch to an enum since it is a small project and I wiill only be using about twenty sounds. Problem is, when I go into the inspector now, there is only the string value, no drop-down selection as an enum would normally make. Here is my code –
[System.Serializable]
public enum Sounds
{
TractorBeamStarting,
TractorBeamRunning,
TractorBeamStopping,
Engine,
HurtMoo_1,
HurtMoo_2,
Moo_1,
Moo_2,
Moo_3,
Moo_4,
Ambience
}
using UnityEngine.Audio;
using UnityEngine;
[System.Serializable]
public class Sound
{
public string name;
[SerializeField]
public Sounds nameEnum;
[HideInInspector]
public GameObject soundBoat;
public AudioClip clip;
[Range(0f, 1f)]
public float volume;
[Range(.1f, 3f)]
public float pitch;
[Range(0, 1)]
public float spatialBlend;
[HideInInspector]
public AudioSource source;
public bool loop;
}
using UnityEngine.Audio;
using System;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public Sound[] sounds;
public static AudioManager instance;
// Start is called before the first frame update
void Awake()
{
if(instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
foreach (Sound s in sounds)
{
GameObject go = Instantiate<GameObject>(new GameObject(s.nameEnum + "_soundBoat", typeof(AudioSource)), gameObject.transform);
s.soundBoat = go;
s.source = go.GetComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.spatialBlend = s.spatialBlend;
s.source.loop = s.loop;
}
}
public void Play(Sounds name, Vector3 position)
{
Sound s = Array.Find(sounds, sound => sound.nameEnum == name);
if (s == null)
{
Debug.LogWarning("Sound: " + name.ToString() + "does not exist!");
return;
}
s.soundBoat.transform.position = position;
s.source.Play();
}
public void PlayOneShot(Sounds name, AudioClip singleUseClip, Vector3 position)
{
Sound s = Array.Find(sounds, sound => sound.nameEnum == name);
if (s == null)
{
Debug.LogWarning("Sound: " + name.ToString() + "does not exist!");
return;
}
s.soundBoat.transform.position = position;
s.source.PlayOneShot(singleUseClip);
}
public void Stop(Sounds name)
{
Sound s = Array.Find(sounds, sound => sound.nameEnum == name);
if (s == null)
{
Debug.LogWarning("Sound: " + name.ToString() + "does not exist!");
return;
}
s.source.Stop();
}
public bool isPlaying(Sounds name)
{
Sound s = Array.Find(sounds, sound => sound.nameEnum == name);
if (s == null)
{
Debug.LogWarning("Sound: " + name.ToString() + " does not exist!");
return false;
}
bool _isplaying = s.source.isPlaying;
return _isplaying;
}
public void ChangePitch(Sounds name, float newPitch)
{
Sound s = Array.Find(sounds, sound => sound.nameEnum == name);
if (s == null)
{
Debug.LogWarning("Sound: " + name.ToString() + " does not exist!");
return;
}
s.source.pitch = newPitch;
}
public Sound GetSound(Sounds name)
{
Sound s = Array.Find(sounds, sound => sound.nameEnum == name);
if (s == null)
{
Debug.LogWarning("Sound: " + name.ToString() + " does not exist!");
return null;
}
return s;
}
}
So I made the enum, then I declare a reference to it, I even made it serializable (both the class and the field), and I’ve got nothing…
Any suggestions?