using System.Collections; using System.Collections.Generic; using UnityEngine; public static class AudioHelper { static Dictionary ClipByID; static Dictionary IDByClip; public enum AudioCategory : byte { Music = 1, Fx = 2, Voice = 3 } public static float VolumeLevel(AudioCategory category) { return PlayerPrefs.GetFloat(category.ToString(), 0.375f); // Did you know default volume of 1 is a bad idea } public static void SetVolume(AudioCategory category, float level) { PlayerPrefs.SetFloat(category.ToString(), level); PlayerPrefs.Save(); } public static int ByClip(AudioClip clip) { if (IDByClip == null) Init(); if (IDByClip.TryGetValue(clip, out int idIfPresent)) { return idIfPresent; } Debug.LogWarningFormat("{0} is not resources loaded clip!!", clip.name); return 0; } public static AudioClip ByID(int id) { if (ClipByID == null) Init(); try { return ClipByID[id]; } catch { throw new System.Exception(string.Format("Failure to find SFX with ID {0}", id)); } } // Build the audio lookup static void Init() { ClipByID = new Dictionary(); IDByClip = new Dictionary(); var allAudio = Resources.LoadAll(""); for (int i = 0; i < allAudio.Length; ++i) { var clip = allAudio[i]; ClipByID.Add(i, clip); IDByClip.Add(clip, i); } var oString = "Loaded Sounds: "; foreach (var key in ClipByID) { oString += key; } Debug.Log(oString); } }