Search Unity

Audio A small script that attaches an AudioListener to the Editor Scene View Camera

Discussion in 'Audio & Video' started by Docaroo, Jul 10, 2018.

  1. Docaroo

    Docaroo

    Joined:
    Nov 7, 2017
    Posts:
    82
    Hi All,

    In a project I'm working on I'm tweaking a lot of 3D sounds and usually do this in play mode - this gets annoying when changes you make don't save in play mode and you are tweaking many audio sources at once.

    In Unity Editor 3D sound does work if you enable "Toggle Audio on" in the Scene view options and then you can navigate with the camera to hear the 3D sounds changing. The only downside is that in the 3D sound settings curve you can't see the position of the audio listener with regards to the audio source (as in the screenshot) even though the 3D sounds play correctly.

    After a bit of googling on how to get the scene view camera position I have a small script that you simply add to your AudioListener and then the audiolistener will follow the scene view camera and allow you to see your distances to the 3D audiosource correctly! (Note, you must check if Camera.current is null as it will be null if the Editor window isn't in focus and will throw an error otherwise).

    Hope this is useful for others!

    Other Note: Make sure you don't have this enabled when you aren't in Editor mode as it might screw up any other AudioListener controls you have!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [ExecuteInEditMode]
    6. public class EditorAudioListenerFollowsSceneCamera : MonoBehaviour {
    7.  
    8.     // Set the object position to sceneview camera if not null
    9.     void Update() {
    10.         if(Camera.current) { this.transform.position = Camera.current.transform.position; }
    11.     }
    12. }
    13.  
     

    Attached Files:

    sivrikaya likes this.
  2. HKNormann

    HKNormann

    Joined:
    Aug 28, 2018
    Posts:
    3
    Hi, I really like your idea and script!
    I created a slightly different variant that works nicely in builds and disables other audiosources to not give an error.
    It also runs async only when needed and not in its own update loop.


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class EditorAudioListenerFollowsSceneCamera : MonoBehaviour
    7. {
    8.     private void Start()
    9.     {
    10.         if(Application.isEditor)
    11.         {
    12.             foreach (AudioListener a in GameObject.FindObjectsOfType<AudioListener>())
    13.                 a.enabled = false;
    14.  
    15.             this.gameObject.AddComponent<AudioListener>();
    16.  
    17.             StartCoroutine(FollowCamera());
    18.         }
    19.     }
    20.  
    21.  
    22.     IEnumerator FollowCamera()
    23.     {
    24.         while(true)
    25.         {
    26.             if (Camera.current)
    27.             {
    28.                 this.transform.position = Camera.current.transform.position;
    29.             }
    30.             yield return null;
    31.         }
    32.     }
    33. }
    34.  
     
    sivrikaya likes this.
  3. sivrikaya

    sivrikaya

    Joined:
    Oct 19, 2014
    Posts:
    7
    Camera.current seem to get wrong position. After trying various things I find UnityEditor.SceneView.lastActiveSceneView.camera returning more reliable values.
     
    Marrlie likes this.