Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  5. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Audio How to Preview AudioSource - NOT in Play Mode

Discussion in 'Audio & Video' started by april_4_short, Jul 25, 2021.

  1. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    How can an AudioSource and its settings be previewed in the Editor, when not in Play Mode?

    And how to preview the playing of multiple Audio Sources and their associated clips, at the same time, in the Editor, not in Play mode, so as to fine tune settings etc.
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,643
  3. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,643
    Yes, that's for not in Play Mode. (although it requires you have audiosources as play on awake, and if the audio sources are 3d, the is uses the scene view's position).
     
    april_4_short likes this.
  5. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    Cheers.

    Now, talk to me like I'm very stupid.

    How do I preview the AudioSource whilst not in Play Mode?

    I can't find a way to preview the AudioSource. To make it play its AudioClip.
     
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,643
    Assuming you have:

    1. Play on Awake on the audiosource.
    2. If 3D audiosource, the scene view close to the audio source.
    3. NOT "Mute Audio" in the game view (somehow this setting affects edit mode sound as well).

    Just click that button on the scene view window and sound plays.

    All audiosources with Awake on Play will play though, AFAIK there isn't a way to have only a specific one play / preview.
     
    Last edited: Jul 25, 2021
    april_4_short likes this.
  7. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    That will work in Play Mode, when the AudioSource is given an Awake() call.

    How do I preview an AudioSource when NOT in Play Mode?
     
  8. Hannibal_Leo

    Hannibal_Leo

    Joined:
    Nov 5, 2012
    Posts:
    493
    What happens when you click the button from AcidArrows Image in the SCENE VIEW?
     
  9. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    When in Edit Mode... NOTHING!
     
  10. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,643
  11. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    This makes all the difference... in the world... !!!

    THANK YOU!!!

    Sadly, this also makes all the difference in the world, as the world/level has a LOT of sounds, some I'd like to fine tune, in isolation, hence the desire to preview... but the whole world of sounds goes beserk...

    Further evidence Unity doesn't use their own editor, expecting everyone to make little subscenes to test everything out rather than work in a more holistic manner.

    WHY isn't there a preview button on the AudioSource????

    Don't answer. It's rhetorical rage from the umpteenth user hostile experience in Unity.
     
    AcidArrow likes this.
  12. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,643
    I would suggest trying FMOD Studio.

    You can "connect" your fmod studio project to the editor (or even a build of your game), and do live adjustments of audio in FMOD Studio and hear them in game, which is pretty great.

    It has a few weird limitations. Some stuff with microphones are not trivial, and Doppler requires a rigidbody, although you can script around that and pass it a velocity manually.
     
    april_4_short likes this.
  13. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    I've been trying to avoid doing this, as it looks like a fairly comprehensive reworking of everything audio in my game will have to be done.

    I'm already using snapshots and blending between them, pooling clips and AudioSources, got busy little sound managers, a glitchy and non-glitchy engine noise made with granular abusing thing and a few other things I'm forgetting.

    Would probably take a couple of weeks to transition, and I'm not sure about performance ramifications of the change, as I'm running fast and light on mobiles.
     
  14. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    Doppler on rigidBody2D would be fine... if it's 3D, will have to script around it. Am abusing reverb zones and Doppler effect, a LOT. Makes for a futuristic spacey sound, quite easily, with short files and minimal CPU usage. One of the oddly efficient things in Unity. Perhaps because it's so old.
     
  15. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,643
    Absolutely, moving your work to FMOD will definitely be time consuming.
    We're also running on mobiles, I don't think FMOD Studio's slower. Unity is using an older FMOD anyway, and a lot of stuff are simply better designed in FMOD Studio, so if anything, I'd expect FMOD Studio to be faster, although I haven't done any concrete testing.
     
    april_4_short likes this.
  16. ArminRigo

    ArminRigo

    Joined:
    May 19, 2017
    Posts:
    12
    As a concrete solution, here is an editor script that adds commands "Play" and "Stop" to the context menu of the AudioSource objects. It's quite a hack but it seems to work for me. Put it in a .cs file inside a folder called "Editor".

    The "Stop" command is useful for long or looping sounds. For 3D AudioSources, as far as I can tell, it's always played at full volume; it seems that the location of the AudioSource doesn't matter.

    It works by instantiating a copy of the AudioSource into the scene, and playing that. This is done to support prefabs. The copy is marked DontSave and is removed the next time you say "Play" or "Stop" anywhere else. I could also hide it, but I had troubles with that for some reason. Just remove manually this gameobject if it goes in the way; it should not hurt and not be saved into the actual scene file.

    Tested with Unity 2019.1.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using UnityEditor;
    4.  
    5. public class AudioSourceEditor
    6. {
    7.     const string name_tag = "[AudioSource player, not saved]";
    8.  
    9.     [MenuItem("CONTEXT/AudioSource/Play")]
    10.     static void PlayAudioSource(MenuCommand command)
    11.     {
    12.         StopAudioSource(command);
    13.  
    14.         AudioSource asrc = (AudioSource)command.context;
    15.         var asrc1 = Object.Instantiate(asrc,
    16.             asrc.transform.position, asrc.transform.rotation);
    17.         asrc1.gameObject.name = name_tag;
    18.         foreach (var component in asrc1.GetComponents<Component>())
    19.             if (!(component is AudioSource) && !(component is Transform))
    20.                 Object.DestroyImmediate(component);
    21.         while (asrc1.transform.childCount > 0)
    22.             Object.DestroyImmediate(asrc.transform.GetChild(
    23.                 asrc1.transform.childCount - 1).gameObject);
    24.  
    25.         asrc1.gameObject.hideFlags |= HideFlags.DontSave;
    26.         asrc1.playOnAwake = false;
    27.         asrc1.gameObject.SetActive(true);
    28.         asrc1.enabled = true;
    29.         asrc1.Play();
    30.     }
    31.  
    32.     [MenuItem("CONTEXT/AudioSource/Stop")]
    33.     static void StopAudioSource(MenuCommand command)
    34.     {
    35.         for (int i = 0; i < SceneManager.sceneCount; i++)
    36.             foreach (var gobj in SceneManager.GetSceneAt(i).
    37.                                     GetRootGameObjects())
    38.                 if (gobj.name == name_tag &&
    39.                     (gobj.hideFlags & HideFlags.DontSaveInEditor) != 0)
    40.                     Object.DestroyImmediate(gobj);
    41.     }
    42. }
    43.  
     
    JohnTube likes this.
  17. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    367
    I saw someone else having the same issue. I made him a little script to play and stop audio source in the Editor. I customized it to your need.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class AudioPreviewTool : MonoBehaviour
    4. {
    5.     public AudioSource[] sources;
    6. }
    7.  
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [CustomEditor(typeof(AudioPreviewTool))]
    5. [CanEditMultipleObjects]
    6. public class AudioPreviewToolEditor : Editor
    7. {
    8.     SerializedProperty propSources;
    9.  
    10.     void OnEnable()
    11.     {
    12.         propSources = serializedObject.FindProperty("sources");
    13.     }
    14.  
    15.     public override void OnInspectorGUI()
    16.     {
    17.         DrawDefaultInspector();
    18.  
    19.         var tool = target as AudioPreviewTool;
    20.         serializedObject.Update();
    21.  
    22.         if (GUILayout.Button("Play"))
    23.         {
    24.             Debug.Log($"{propSources.arraySize}");
    25.             for (int i = 0; i < propSources.arraySize; i++)
    26.             {
    27.                 var element = propSources.GetArrayElementAtIndex(i);
    28.                 if(element.objectReferenceValue != null)
    29.                 {
    30.                     var audioSource = (AudioSource)element.objectReferenceValue;
    31.                     audioSource.Play();
    32.                 }
    33.             }
    34.         }
    35.  
    36.         if (GUILayout.Button("Stop"))
    37.         {
    38.             Debug.Log($"{propSources.arraySize}");
    39.             for (int i = 0; i < propSources.arraySize; i++)
    40.             {
    41.                 var element = propSources.GetArrayElementAtIndex(i);
    42.                 if(element.objectReferenceValue != null)
    43.                 {
    44.                     var audioSource = (AudioSource)element.objectReferenceValue;
    45.                     audioSource.Stop();
    46.                 }
    47.             }
    48.         }
    49.     }
    50. }
    51.  
    You add the AudioPreviewTool on any GameObject and set all audio sources you want to use. I tuned and tested 3D spatial without going in Playmode while moving the camera around. You can customize the script for more complex needs. Don't forget to put the AudioPreviewToolEditor in an Editor folder, or else it will not work. Don't hesitate to reach out if you are having some issues.

    I appreciate your feedback! I added it to our product board as a need that our users have.
     
    Last edited: Mar 3, 2022
    DriesVienne likes this.