public void PlayLoopingSound(int clipIndex) { GameObject sfx = Instantiate(audioObj, transform.position, Quaternion.identity); if (audioParentObj != null) { sfx.transform.SetParent(audioParentObj.transform); } audioParentObj = null; AudioSource audioSource = sfx.GetComponent <AudioSource>(); AudioClipSettings audioClipSettings = sfx.GetComponent <AudioClipSettings>(); audioClipSettings.SetPlayLoopingSound(); if (audioClipSettings.FadeIn) { FadeInSound(sfx); } else if (audioClipSettings.FadeOut) { FadeOutSound(sfx); } audioSource.clip = audioClips[clipIndex]; audioSource.Play(); }
private void SetSourceSettings(AudioSource source, AudioClipSettings clipSettings) { if (source && clipSettings != null) { source.clip = clipSettings.clip; source.volume = clipSettings.volume; source.panStereo = clipSettings.pan; source.loop = clipSettings.loop; source.playOnAwake = clipSettings.playOnAwake; } }
public void PlaySound(string name) { if (on) { AudioSource newSource = GetSource(); newSource.gameObject.SetActive(true); AudioClipSettings newClip = GetClip(name); SetSourceSettings(newSource, newClip); newSource.Play(); } }
IEnumerator FadeOutSoundCoroutine(GameObject audioObj) { float timer = 0; AudioSource audioSource = audioObj.GetComponent <AudioSource>(); AudioClipSettings audioClipSettings = audioObj.GetComponent <AudioClipSettings>(); audioSource.volume = audioClipSettings.Volume; while (timer < audioClipSettings.FadeDuration) { timer += Time.deltaTime; audioSource.volume = Mathf.Lerp(audioClipSettings.Volume, 0, timer / audioClipSettings.FadeDuration); yield return(null); } yield break; }
// Use this for initialization void Awake() { Assert.IsNull(Instance, "More than 1 instance of AudioPool detected. ONLY HAVE 1 IN THE SCENE PLZ"); Instance = this; DefaultSettings = new AudioClipSettings(); // Set up the object pool of audio sources m_AudioSources = new GameObjectPool(1, m_emptyPrefab, this.gameObject); }
/// <summary> /// Plays a random clip from the given clips. With a random pitch and volume as defined in the settings given. /// </summary> public AudioSource PlayRandom(AudioClip[] clips, Vector3 pos, AudioClipSettings settings) { if (clips.Length > 0) { return PlayRandom(clips[UnityEngine.Random.Range(0, clips.Length)], pos, settings); } else { return null; } }
/// <summary> /// Plays the given audio clip with a random pitch and volume as defined in the settings given. /// </summary> public AudioSource PlayRandom(AudioClip clip, Vector3 pos, AudioClipSettings settings) { if (settings.SingleInstance && m_activeClips.Count > 0) { // Search the list for the matching audio clip for (int i = 0; i < m_activeClips.Count; i++) { if (m_activeClips[i].clip.GetInstanceID() == clip.GetInstanceID()) { // Found and overlap period not over yet // End the function and skip the audio clip if (m_activeClips[i].time < settings.OverlapThreshold && m_activeClips[i].isPlaying) { return null; } // Found and overlap is over else { m_activeClips.RemoveAt(i); break; } } } } // Retrieve unused audio source from pool GameObject newSound = m_AudioSources.New(pos); var cb = newSound.GetComponent<CustomBehaviour>(); var audSrc = cb.GetAudioSource; // Randomization float randomPitch = UnityEngine.Random.Range(settings.MinPitch, settings.MaxPitch); float randomVol = UnityEngine.Random.Range(settings.MinVolume, settings.MaxVolume); newSound.transform.position = pos; audSrc.clip = clip; audSrc.pitch = randomPitch; audSrc.volume = randomVol; audSrc.outputAudioMixerGroup = settings.MixerGroup; if (settings.SingleInstance) { m_activeClips.Add(audSrc); } newSound.SetActive(true); Timing.RunCoroutine(StoreClip(cb), Segment.SlowUpdate); return audSrc; }
public AudioSource PlayPitchShift(AudioClip[] clips, Vector3 pos, AudioClipSettings settings , float semitoneDiff, string pitchShifter) { if (clips.Length > 0) { return PlayPitchShift(clips[UnityEngine.Random.Range(0, clips.Length)], pos, settings, semitoneDiff, pitchShifter); } else { Debug.LogWarning("Empty clips array passed into PlayPitchShift"); return null; } }
/// <summary> /// Plays the given audioclip at a random setting based on AudioClipSettings /// This clip will then be pitch shifted using the mixergroup by the semitone difference given /// Note, make sure your mixergroup has a pitch shifter effect applied /// </summary> public AudioSource PlayPitchShift(AudioClip clip, Vector3 pos, AudioClipSettings settings , float semitoneDiff, string pitchShifter) { float newPitch = 1 * Mathf.Pow(SEMITONE_BASE, semitoneDiff); settings.MixerGroup.audioMixer.SetFloat(pitchShifter, newPitch); /* float test = 0.0f; settings.MixerGroup.audioMixer.GetFloat("CollectiblesPitchShift", out test); Debug.Log("set to " + test);*/ return PlayRandom(clip, pos, settings); }