- Home /
How to play AudioClip in edit mode?
I want to play an audio clip in edit mode, but I'm not having any luck. I'm creating a custom inspector that works with audio files and I'd like to be able to preview the sounds in the editor.
Here's what I've tried so far. It doesn't work, but I wouldn't expect it to really since I imagine the audio listener is only working in play mode.
var cam : GameObject = GameObject.Find("GUICamera"); var clip : AudioClip = AssetDatabase.LoadAssetAtPath("Assets/Page01/Audio/FullAudio.wav", typeof(AudioClip)) as AudioClip;
var go = new GameObject("PLAY_AUDIO_TEMP"); go.transform.position = cam.transform.position;
var source : AudioSource = go.AddComponent(AudioSource) as AudioSource; source.clip = clip; source.volume = 1.0; source.Play();
Are there any other scripting tools for working with audio in the editor that I've overlooked?
I guess the standard audio clip preview isn't sufficient? (Click on the audio asset in Project window, then click the play button in the preview window below the inspector.)
It is really tedious to have to keep selecting different objects, especially when the asset database is very large. $$anonymous$$y scene may only use a dozen audio clips, but there are hundreds in the database. To go and find each one just isn't a good workflow thus the reason why I'd like to trigger the audio playback from my custom inspector.
You can use the static function AudioSource.PlayClipAtPoint() then you don't need to create a temp GO.
There are two things they come to my $$anonymous$$d: first i think you need an AudioListener in the scene or you can't hear anything. And second, I'm not sure if this affects editor scripts or not but you can toggle the sound at the top of the scene-window.
You could procedurally find all the audio clips (in the scene) and their corresponding asset (in the project) -- let the user choose from a list and you can set Selection.objects to do the selection. Still a workaround, would be better to be able to play directly. Did you try Bunny83's suggestion?
I have tried Bunny83's suggestion, but nothing happens using AudioSource.PLayClipAtPoint(). $$anonymous$$y guess is that the AudioListener component only listens during runtime, not in edit mode.
Answer by Flynn · Jan 17, 2011 at 10:59 AM
The audio preview mode must be on in order for sound to play in the editor.
Inside the scene view, near the top, in the left/right corner, there is a little speaker icon. Click that, and your audio should work.
NOTE: The audio preview mode is only available in Unity 3.0
Answer by ocimum · Dec 19, 2015 at 02:57 PM
For playing sound in EditMode
you could just create an EditorWindow
or a CustomEditor
. Then you also don't have a problem with generating any additional GameObject
just for holding the AudioSource
.
For loading an AudioClip
inside an Editor
script, you have to execute this line:
(AudioClip) EditorGUIUtility.Load("Assets/AnotherDirectory/" + audioClipName + ".wav");
For playing or stopping the sound use following methods method:
public static void PlayClip(AudioClip clip) {
Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
MethodInfo method = audioUtilClass.GetMethod(
"PlayClip",
BindingFlags.Static | BindingFlags.Public,
null,
new System.Type[] {
typeof(AudioClip)
},
null
);
method.Invoke(
null,
new object[] {
clip
}
);
}
public static void StopAllClips() {
Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
MethodInfo method = audioUtilClass.GetMethod(
"StopAllClips",
BindingFlags.Static | BindingFlags.Public,
null,
new System.Type[]{},
null
);
method.Invoke(
null,
new object[] {}
);
}
Have fun!
Awesome. Just used your code in a custom editor and it's perfect. Thanks! :)
Thank you work great in my custom editor I used it like this PlayClip((AudioClip)element.FindPropertyRelative("clip").objectReferenceValue);
Update for Unity 2019: https://forum.unity.com/threads/way-to-play-audio-in-editor-using-an-editor-script.132042/#post-4767824 (need to add int startSample = 0, bool loop = false parameters when invoking PlayClip).
Best solution, but UnityEditor.AudioUtil doesn't provide methods to change the pitch/reverb/volume of the clip though.