Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Audio How to Virtualize Inaudible Sound?

Discussion in 'Audio & Video' started by lod3, Jan 10, 2020.

  1. lod3


    Mar 21, 2012

    After a few hours of unsuccessful Googling, I thought I would ask here: Is there a way to force inaudible sounds to virtualize, or do I need to use a third-party audio system like WWISE? This is for standard audio sources playing in the scene.

    I have a bigger scene, but sound is very-well spaced and configured so that the Player can never hear more than 4 or maybe 5 at once, yet all far away sounds are still being reported as playing, according to the Audio Profiler, which is consuming all my real audio voices. I've actually got way more real voices than virtual, but I don't understand what it takes to virtualize these when the player cannot hear them.

    Any help appreciated! Thank you.
  2. JLF


    Feb 25, 2013
    Hi, as far as I know the only way to force a sound to virtualise is by reducing the Real Voice count so that virtualisation kicks in sooner but, if you want to prevent Audio Sources from playing unnecessarily you might be better off pausing them instead.

    This example script will pause Audio Sources if they're out of range of the player.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class CheckIfAudible : MonoBehaviour
    6. {
    7.   AudioListener audioListener;
    8.   AudioSource audioSource;
    9.   float distanceFromPlayer;
    11.   void Start()
    12.   {
    13.     // Finds the Audio Listener and the Audio Source on the object
    14.     audioListener = Camera.main.GetComponent<AudioListener>();
    15.     audioSource = gameObject.GetComponent<AudioSource>();
    16.   }
    18.   void Update()
    19.   {
    20.     distanceFromPlayer = Vector3.Distance(transform.position, audioListener.transform.position);
    22.     if (distanceFromPlayer <= audioSource.maxDistance)
    23.     {
    24.       ToggleAudioSource(true);
    25.     }
    26.     else
    27.     {
    28.       ToggleAudioSource(false);
    29.     }
    30.   }
    32.   void ToggleAudioSource(bool isAudible)
    33.   {
    34.     if (!isAudible && audioSource.isPlaying)
    35.     {
    36.       audioSource.Pause();
    37.     }
    38.     else if (isAudible && !audioSource.isPlaying)
    39.     {
    40.       audioSource.Play();
    41.     }
    42.   }
    43. }
    This comes from a whole article I wrote on my blog about Audio Optimisation, which you might find useful if you want to know more about how Unity optimises audio:
  3. lod3


    Mar 21, 2012
    Thanks for the reply! I've read that article, actually, great stuff. I'll try this out tonight. Thanks again!
    JLF likes this.