/////////////////////////////////////////// /// 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); } }
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); } }
public void PlayOneShot(AudioClip i_clip) { if (i_clip == null) { Destroy(gameObject); return; } audioSource = gameObject.AddComponent <AudioSource>(); audioSource.clip = i_clip; audioSource.loop = false; //float fDefaultVolume = Constants_OLD.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 = i_clip.length + 0.1f; //if ( i_hashOptional.ContainsKey( "Time" ) ) // m_fLifetime = (float) i_hashOptional["Time"]; DestroyThis scriptDestroy = gameObject.AddComponent <DestroyThis>(); scriptDestroy.SetLife(m_fLifetime); } }