Way to play audio in editor using an editor script?

Hey all,

Is there a way to play an audio clip in the editor using an editor script? It’s part of an editor utility tool that I’m developing.

The way I have it setup now and working is that I’m:

  1. Spawning a game object
  2. Adding an audio source and its’ clip
  3. Playing it
  4. Destroying the object.

The problem is that Unity detects a change to the scene even though I’m not trying to edit the scene.

This is working for my needs, however, but is there a better way?

Thanks,
Nathan

2 Likes

{Bumping once since it may have been missed since I posted it late Friday}

I dont know of any editor methods to play a sound cleanly for you (like one might look like EditorUtility.PlaySound()).

How about if you reused the same GameObject, and call it the soundplayer. Create it once, keep a reference to it, and set its hideflags to hideanddontsave. I’m not sure if this will fix your issue of unity detecting a scene change, but its worth a shot.

Thanks Daniel, I’ll definitely give these a shot :slight_smile:

The gameObject.hideFlags = HideFlags.HideAndDontSave; worked! Thanks Daniel.

Happy to hear :slight_smile:

You can make this method:

using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;

public static class PublicAudioUtil {
    
    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
            }
        );
    } // PlayClip()

} // class PublicAudioUtil
3 Likes

Hi, i tried using this code, but i get :
error CS0246: The type or namespace name `Type’ could not be found. Are you missing a using directive or an assembly reference?

Can you help me with that?

thanks.

Edit:

Well, i figured out :

Now, there’s one question in the air… how do i stop the sound?!
hahah, again, thanks!

2 Likes

For everybody who would like to stop the clips started, try this:

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[] {}
        );
    }

There is also another private Method inside AudioUtil which is called StopAudioClip, the problem is just, you would need to know the reference to the clip which is playing at the moment.
If you want to know which Methods are available inside AudioUtil check this out.

2 Likes

Thank you guys for this. I had a problem in Unity 2019.3 where the “PlayClip” method couldn’t be found. So I updated it to use the extern function in AudioUtil with startSample and loop.

public static void PlayClip(AudioClip clip, int startSample = 0, bool loop = false)
{
    System.Reflection.Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
    System.Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
    System.Reflection.MethodInfo method = audioUtilClass.GetMethod(
        "PlayClip",
        System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public,
        null,
        new System.Type[] { typeof(AudioClip), typeof(int), typeof(bool) },
        null
    );
    method.Invoke(
        null,
        new object[] { clip, startSample, loop }
    );
}
21 Likes

thank’s !

Hey is there a method to pause an audio clip instead of Stop?

It doesn’t work to start it at a specific time. Does anyone know a solution?
I have found this How to Play AudioClip from Editor from a Start Sample/Time - Questions & Answers - Unity Discussions
but I can’t figure it out.

These changed again in Unity 2020. Here’s the renamed classes. The full list of internal Audio Util classes can be found here (auto mod won’t let me post. Search for AudioUtil.bindings.cs on Unity (it’s in github))

using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;

public static class EditorSFX
{

    public static void PlayClip(AudioClip clip, int startSample = 0, bool loop = false)
    {
        Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
      
        Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
        MethodInfo method = audioUtilClass.GetMethod(
            "PlayPreviewClip",
            BindingFlags.Static | BindingFlags.Public,
            null,
            new Type[] { typeof(AudioClip), typeof(int), typeof(bool) },
            null
        );

        Debug.Log(method);
        method.Invoke(
            null,
            new object[] { clip, startSample, loop }
        );
    }

    public static void StopAllClips()
    {
        Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;

        Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
        MethodInfo method = audioUtilClass.GetMethod(
            "StopAllPreviewClips",
            BindingFlags.Static | BindingFlags.Public,
            null,
            new Type[] { },
            null
        );

        Debug.Log(method);
        method.Invoke(
            null,
            new object[] { }
        );
    }
}
17 Likes

Where should i attach this script

Hi, did you manage to play the AudioSource in EditorMode? If so, could you share some code? I’m looking for a way to change the spitch and reverb sound while in edition and I don’t mind creating and deleting AudioSource gameobjects during the edition.

It’s been a minute, but there was a post on Unity Answers that had some mostly usable code for this. It was similar to the above (i.e. required reflection).

I borrowed some of this for Snapcam, but since I’m between computers right now, I don’t have access to it at the moment. However, it was just a quick google search. Just letting you know in case you want to poke around.

1 Like

Thanks, yesterday I figured out how to make a AudioSource work in EditMode with the [ExecuteAlways] attribute :wink:

Can you show how, PLEASE!!??

The idea is to have a Monobehaviour with an AudioSource attached to it AND insert the [ExecuteAlways] attribute before the class declaration.
Then, from some click event ( for example, creating a button in a custom drawer), order this Monovehabiour to play the AudioSource.

Ask for an example if you need it.