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.
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).
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.
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.
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.
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.
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.
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.
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;
public class AudioSourceEditor
{
const string name_tag = "[AudioSource player, not saved]";
[MenuItem("CONTEXT/AudioSource/Play")]
static void PlayAudioSource(MenuCommand command)
{
StopAudioSource(command);
AudioSource asrc = (AudioSource)command.context;
var asrc1 = Object.Instantiate(asrc,
asrc.transform.position, asrc.transform.rotation);
asrc1.gameObject.name = name_tag;
foreach (var component in asrc1.GetComponents<Component>())
if (!(component is AudioSource) && !(component is Transform))
Object.DestroyImmediate(component);
while (asrc1.transform.childCount > 0)
Object.DestroyImmediate(asrc.transform.GetChild(
asrc1.transform.childCount - 1).gameObject);
asrc1.gameObject.hideFlags |= HideFlags.DontSave;
asrc1.playOnAwake = false;
asrc1.gameObject.SetActive(true);
asrc1.enabled = true;
asrc1.Play();
}
[MenuItem("CONTEXT/AudioSource/Stop")]
static void StopAudioSource(MenuCommand command)
{
for (int i = 0; i < SceneManager.sceneCount; i++)
foreach (var gobj in SceneManager.GetSceneAt(i).
GetRootGameObjects())
if (gobj.name == name_tag &&
(gobj.hideFlags & HideFlags.DontSaveInEditor) != 0)
Object.DestroyImmediate(gobj);
}
}
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.
using UnityEngine;
public class AudioPreviewTool : MonoBehaviour
{
public AudioSource[] sources;
}
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(AudioPreviewTool))]
[CanEditMultipleObjects]
public class AudioPreviewToolEditor : Editor
{
SerializedProperty propSources;
void OnEnable()
{
propSources = serializedObject.FindProperty("sources");
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
var tool = target as AudioPreviewTool;
serializedObject.Update();
if (GUILayout.Button("Play"))
{
Debug.Log($"{propSources.arraySize}");
for (int i = 0; i < propSources.arraySize; i++)
{
var element = propSources.GetArrayElementAtIndex(i);
if(element.objectReferenceValue != null)
{
var audioSource = (AudioSource)element.objectReferenceValue;
audioSource.Play();
}
}
}
if (GUILayout.Button("Stop"))
{
Debug.Log($"{propSources.arraySize}");
for (int i = 0; i < propSources.arraySize; i++)
{
var element = propSources.GetArrayElementAtIndex(i);
if(element.objectReferenceValue != null)
{
var audioSource = (AudioSource)element.objectReferenceValue;
audioSource.Stop();
}
}
}
}
}
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.