public void PlaySound(string name, Vector3 _position) { AudioDefinition audio = GetAudioDefinition(name); if (audio == null) { return; } if (!CanPlaySound(name)) { return; } GameObject soundOject = new GameObject(); soundOject.transform.position = _position; AudioSource audioSource = soundOject.AddComponent <AudioSource>(); audioSource.clip = audio.clip; // TODO: set up AudioSource properties like is loop or not, volume,... audioSource.Play(); Object.Destroy(soundOject, audio.clip.length); }
public static AudioSource PlaySoundAt(Vector3 position, AudioDefinition audioDef) { GameObject aObj = new GameObject("DynamicSound"); aObj.transform.position = position; AudioSource aSrc = aObj.AddComponent<AudioSource>(); AudioClip clip = audioDef.GetAudioClip(); aSrc.clip = clip; aSrc.volume = audioDef.Volume * effectsLevel; aSrc.pitch = audioDef.GetPitch(); aSrc.minDistance = audioDef.SoundRangeMin; aSrc.maxDistance = audioDef.SoundRangeMax; aSrc.Play(); if (!audioDef.Loop) Destroy(aObj, clip.length); sources.Add(aSrc); return aSrc; }
public static AudioSource PlaySoundParented(Transform transform, AudioDefinition audioDef) { GameObject aObj = new GameObject("DynamicSound"); aObj.transform.parent = transform; aObj.transform.localPosition = Vector3.zero; aObj.transform.localRotation = Quaternion.identity; AudioSource aSrc = aObj.AddComponent<AudioSource>(); AudioClip clip = audioDef.GetAudioClip(); aSrc.clip = clip; aSrc.volume = audioDef.Volume * effectsLevel; aSrc.pitch = audioDef.GetPitch(); aSrc.minDistance = audioDef.SoundRangeMin; aSrc.maxDistance = audioDef.SoundRangeMax; aSrc.Play(); if(!audioDef.Loop) Destroy(aObj, clip.length); sources.Add(aSrc); return aSrc; }