////////////////////////////////////////// /// SetUpData() ////////////////////////////////////////// public static void SetUpData() { m_dictData = new Dictionary <string, ID_Audio>(); //Load all data xml files UnityEngine.Object[] files = Resources.LoadAll("Audio", typeof(TextAsset)); foreach (TextAsset file in files) { string xmlString = file.text; // error message string strErrorFile = "Error in file " + file.name; //Create XMLParser instance XMLParser xmlParser = new XMLParser(xmlString); //Call the parser to build the IXMLNode objects XMLElement xmlElement = xmlParser.Parse(); // this will load files that have replaced the file stored in data locally #if UNITY_STANDALONE_WIN string strFile = Application.dataPath + "/Resources/" + file.name + ".xml"; if (System.IO.File.Exists(strFile)) { Debug.Log("An override audio file was found: " + strFile); xmlString = System.IO.File.ReadAllText(strFile); } #endif //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 list of children elements List <IXMLNode> listChildren = XMLUtils.GetChildrenList(childNode); //Get id Hashtable hashAttr = XMLUtils.GetAttributes(childNode); string strID = (string)hashAttr["ID"]; string strError = strErrorFile + "(" + strID + "): "; ID_Audio data = new ID_Audio(strID, hashAttr, listChildren, strError); // store the data if (m_dictData.ContainsKey(strID)) { Debug.LogError(strError + "Duplicate audio data!"); } else { // add data to dictionary m_dictData.Add(strID, data); } } } }
/////////////////////////////////////////// /// Init() /// Configures the actual audio source and /// plays the sound. /////////////////////////////////////////// public void Init(string strResource, Transform tf, Hashtable i_hashOptional) { // load the clip AudioClip clip = Resources.Load(strResource) as AudioClip; if (clip == null) { Debug.LogError("No such sound clip for resource " + strResource); Destroy(gameObject); return; } // create the audio source audioSource = gameObject.AddComponent <AudioSource>(); audioSource.clip = clip; // set the default volume (may be overriden) float fDefaultVolume = Constants.GetConstant <float>("DefaultVolume"); audioSource.volume = fDefaultVolume; ID_Audio dataAudio = IDL_Audio.GetData(strResource); if (dataAudio != null) { audioSource.volume = dataAudio.GetVolume(); audioSource.loop = dataAudio.ShouldLoop(); } // change pitch if necessary if (i_hashOptional.ContainsKey("pitch")) { float fPitch = (float)i_hashOptional["pitch"]; audioSource.pitch = fPitch; } gameObject.transform.parent = tf; gameObject.transform.position = tf.position; audioSource.Play(); // add destroy script -- if the audio doesn't loop if (audioSource.loop == false) { m_fLifetime = clip.length + 0.1f; if (i_hashOptional.ContainsKey("Time")) { m_fLifetime = (float)i_hashOptional["Time"]; } DestroyThis scriptDestroy = gameObject.AddComponent <DestroyThis>(); scriptDestroy.SetLife(m_fLifetime); } }
////////////////////////////////////////// /// GetData() ////////////////////////////////////////// public static ID_Audio GetData(string i_strKey) { // if the hash isn't set up yet, we need to do that if (m_dictData == null) { SetUpData(); } // data for the variable to be returned ID_Audio data = null; // search for the proper rollout data... if (m_dictData.ContainsKey(i_strKey)) { data = m_dictData[i_strKey]; } // there is no else becase it's fine if there is no audio data return(data); }