void ActualPlay() { if (!GhostTools.SafeToInstantiate(gameObject)) { Debug.LogWarning("Audio playing is only supporting while game is running."); return; } if (audioCollection == null) { Debug.LogWarning(name + " has no audio connection referenced!"); return; } GameObject audioGO = new GameObject(audioCollection.name); audioGO.transform.parent = AudioParent().transform; audioGO.transform.position = transform.position; AudioSource newSource = audioGO.AddComponent <AudioSource>(); newSource.spread = 180; newSource.dopplerLevel = 0; newSource.rolloffMode = AudioRolloffMode.Linear; newSource.clip = audioCollection.GetRandomClip(); newSource.playOnAwake = false; newSource.outputAudioMixerGroup = audioCollection.mixerGroup; newSource.volume = audioCollection.volume * volume; newSource.pitch = audioCollection.Pitch(); newSource.spatialBlend = 1; newSource.maxDistance = audioCollection.maxDistance; newSource.Play(); Destroy(audioGO, audioCollection.audioLifetime); }
// One-shot functions (Non-diagetic) public void PlayRandomUIAudio(AudioCollection clipCollection) { if (clipCollection == null) { return; } // Try to get a random clip. If unsuccessful, AudioClip randomClip = clipCollection.GetRandomClip(); if (randomClip != null) { // 1. Create new Audio object GameObject oneShotObject = new GameObject(randomClip.name); oneShotObject.transform.parent = oneShotClips.transform; CreateAudioSource(randomClip, oneShotObject, this.transform.position, clipCollection.Volume, clipCollection.PitchVariance, false, false); Destroy(oneShotObject, randomClip.length); } }
public void Play() { if (audioCollection == null) { Debug.LogWarning(name + " has no audio connection referenced!"); return; } GameObject audioGO = new GameObject(audioCollection.name); audioGO.transform.parent = AudioParent().transform; AudioSource newSource = audioGO.AddComponent <AudioSource>(); newSource.clip = audioCollection.GetRandomClip(); newSource.playOnAwake = false; newSource.outputAudioMixerGroup = audioCollection.mixerGroup; newSource.volume = audioCollection.volume; newSource.pitch = audioCollection.Pitch(); newSource.Play(); Destroy(audioGO, audioCollection.audioLifetime); }
// Plays a continuous random audio clip parented to a given source. // Multiple Audio Clips can play simultaneously. public GameObject PlayRandomContinuousAudio(AudioCollection clipCollection, Transform source, float maxDuration = 10f) { if (clipCollection == null) { return(null); } // Try to get a random clip. If unsuccessful, AudioClip randomClip = clipCollection.GetRandomClip(); GameObject continuousObject = null; if (randomClip != null) { // 1. Create new Audio object continuousObject = new GameObject(randomClip.name); continuousObject.transform.parent = source.transform; Vector3 positionOffset = cameraLocationOffset; CreateAudioSource(randomClip, continuousObject, source.position - new Vector3(positionOffset.x, 0, positionOffset.z), clipCollection.Volume, clipCollection.PitchVariance, true); Destroy(continuousObject, maxDuration); } return(continuousObject); }