public static void SetupData() { dictSounds = new Dictionary <string, DataSound>(); //Load all item xml files UnityEngine.Object[] files = Resources.LoadAll("Sounds", typeof(TextAsset)); foreach (TextAsset file in files) { string xmlString = file.text; //Create XMLParser instance XMLParser xmlParser = new XMLParser(xmlString); //Call the parser to build the IXMLNode objects XMLElement xmlElement = xmlParser.Parse(); //Go through all child node of xmlElement (the parent of the file) for (int i = 0; i < xmlElement.Children.Count; i++) { IXMLNode childNode = xmlElement.Children[i]; //Get id Hashtable hashAttr = XMLUtils.GetAttributes(childNode); string id = (string)hashAttr["ID"]; DataSound sound = new DataSound(id, hashAttr); // store the sound dictSounds.Add(id, sound); } } }
//Look for sound with id in the dictionary public static DataSound GetSoundData(string clipName, int variations = 1) { if (dictSounds == null) { SetupData(); } DataSound sound = null; // If there is more than one variation, append a number to it randomly if (variations > 1) { int randomVariation = UnityEngine.Random.Range(1, variations + 1); clipName = clipName + randomVariation.ToString(); } if (dictSounds.ContainsKey(clipName)) { sound = dictSounds[clipName]; } else { Debug.LogError("No such sound with id " + clipName + " -- creating one with default values"); sound = new DataSound(clipName); } return(sound); }
/// <summary> /// The base level private method that plays /// a sound. It creates a custom audio /// source that gives us more control over /// the sound. /// </summary> /// <returns>The sound.</returns> /// <param name="sound">Sound.</param> /// <param name="hashOverrides">Hash overrides.</param> private LgAudioSource PlaySound(DataSound sound, Hashtable hashOverrides){ GameObject soundObject = new GameObject("Sound: " + sound.GetResourceName()); LgAudioSource soundSource = soundObject.AddComponent<LgAudioSource>(); soundSource.Init(sound, transform, hashOverrides); return soundSource; }
/// <summary> /// The base level private method that plays /// a sound. It creates a custom audio /// source that gives us more control over /// the sound. /// </summary> private LgAudioSource PlaySound(DataSound sound, Hashtable option) { GameObject soundObject = new GameObject("Sound: " + sound.GetResourceName()); LgAudioSource soundSource = soundObject.AddComponent <LgAudioSource>(); soundSource.Init(sound, transform, option); return(soundSource); }
public void Init(DataSound sound, Transform tf, Hashtable option) { string strResource = sound.GetResourceName(); AudioClip clip = Resources.Load(strResource) as AudioClip; if (clip == null) { Debug.LogError("No such sound clip for resource " + strResource); Destroy(gameObject); return; } audioClipName = strResource; // this is a little messy, but get variables that could be overriden float volume = sound.GetVolume(); if (option.Contains("Volume")) { volume = (float)option["Volume"]; } float pitch = sound.GetPitch(); if (option.Contains("Pitch")) { pitch = (float)option["Pitch"]; } bool loop = false; if (option.Contains("Loop")) { loop = (bool)option["Loop"]; } // create the audio source audioSource = gameObject.AddComponent <AudioSource>(); audioSource.clip = clip; audioSource.volume = volume; audioSource.pitch = pitch; audioSource.loop = loop; gameObject.transform.parent = tf; gameObject.transform.position = tf.position; audioSource.Play(); // add destroy script if (!loop) { DestroyThis scriptDestroy = gameObject.AddComponent <DestroyThis>(); scriptDestroy.SetLife(clip.length); } }
/// <summary> /// <para>Plays the clip. </para> /// <para>option: pass in properties (Volume, Loop, etc) for LgAudioSource. </para> /// <para>option: isSoundClipManaged (T: LgAudioManager will keep a reference to this sound clip /// and will not allow the same sound clip to be played again until the current clip is stopped or /// finished, F: same sound can be played more than once and once the sound is played there's no /// way to stop it until it finishes)</para> /// </summary> /// <param name="clipName">Sound clip name</param> /// <param name="variations">Number of sounds for the same clip</param> /// <param name="hashOverrides">Hash overrides.</param> public virtual void PlayClip(string clipName, int variations = 1, Hashtable option = null) { if (option == null) { option = new Hashtable(); } if (clipName == "") { Debug.LogError("Something trying to play a sound with an empty sound id..."); return; } DataSound sound = DataSounds.GetSoundData(clipName, variations); if (sound == null) { Debug.LogError("No such sound with id " + clipName); return; } bool isSoundClipManaged = false; // Set the default to false if (option.ContainsKey("IsSoundClipManaged")) { isSoundClipManaged = (bool)option["IsSoundClipManaged"]; } //if multip sound mode is on. just play the clip if (!isSoundClipManaged) { LgAudioSource audioSource = PlaySound(sound, option); audioSource.OnDestroyed += LgAudioSourceDestroyed; } //if multi sound mode is off. check the dicitonary for the same clip else { if (!spawnedAudioSources.ContainsKey(clipName)) { LgAudioSource audioSource = PlaySound(sound, option); spawnedAudioSources.Add(clipName, audioSource); audioSource.OnDestroyed += LgAudioSourceDestroyed; } } }
/// <summary> /// Plays the clip. /// </summary> /// <param name="soundClip">Sound clip.</param> /// <param name="hashOverrides">Hash overrides.</param> public void PlayClip(string soundClip, Hashtable hashOverrides = null){ if(hashOverrides == null) hashOverrides = new Hashtable(); if(soundClip == ""){ Debug.LogError("Something trying to play a sound with an empty sound id..."); return; } DataSound sound = DataSounds.GetSoundData(soundClip); if(sound == null){ Debug.LogError("No such sound with id " + soundClip); return; } PlaySound(sound, hashOverrides); }