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