Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Way to play audio in editor using an editor script?

Discussion in 'Scripting' started by NathanWarden, Apr 13, 2012.

  1. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    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
     
    Last edited: Apr 16, 2012
    Hunter_Bobeck likes this.
  2. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    {Bumping once since it may have been missed since I posted it late Friday}
     
  3. HonorableDaniel

    HonorableDaniel

    Joined:
    Feb 28, 2007
    Posts:
    2,965
    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.
     
  4. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Thanks Daniel, I'll definitely give these a shot :)
     
  5. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    The gameObject.hideFlags = HideFlags.HideAndDontSave; worked! Thanks Daniel.
     
  6. HonorableDaniel

    HonorableDaniel

    Joined:
    Feb 28, 2007
    Posts:
    2,965
    Happy to hear :)
     
  7. AShim-3D

    AShim-3D

    Joined:
    Jul 13, 2012
    Posts:
    32
    You can make this method:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System;
    5. using System.Reflection;
    6.  
    7. public static class PublicAudioUtil {
    8.    
    9.     public static void PlayClip(AudioClip clip) {
    10.         Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
    11.         Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
    12.         MethodInfo method = audioUtilClass.GetMethod(
    13.             "PlayClip",
    14.             BindingFlags.Static | BindingFlags.Public,
    15.             null,
    16.             new System.Type[] {
    17.                 typeof(AudioClip)
    18.             },
    19.             null
    20.         );
    21.         method.Invoke(
    22.             null,
    23.             new object[] {
    24.                 clip
    25.             }
    26.         );
    27.     } // PlayClip()
    28.  
    29. } // class PublicAudioUtil
    30.  
     
    Last edited: Oct 21, 2013
  8. Mukabr

    Mukabr

    Joined:
    Jun 10, 2013
    Posts:
    61
    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!
     
    Last edited: Oct 20, 2013
    CabinIcarus likes this.
  9. ocimum

    ocimum

    Joined:
    Apr 19, 2015
    Posts:
    12
    For everybody who would like to stop the clips started, try this:

    Code (CSharp):
    1. public static void StopAllClips() {
    2.         Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
    3.         Type audioUtilClass =
    4.               unityEditorAssembly.GetType("UnityEditor.AudioUtil");
    5.         MethodInfo method = audioUtilClass.GetMethod(
    6.             "StopAllClips",
    7.             BindingFlags.Static | BindingFlags.Public,
    8.             null,
    9.             new System.Type[]{},
    10.             null
    11.         );
    12.         method.Invoke(
    13.             null,
    14.             new object[] {}
    15.         );
    16.     }
    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.
     
  10. Gustav

    Gustav

    Joined:
    Jun 10, 2013
    Posts:
    1
    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.

    Code (CSharp):
    1. public static void PlayClip(AudioClip clip, int startSample = 0, bool loop = false)
    2. {
    3.     System.Reflection.Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
    4.     System.Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
    5.     System.Reflection.MethodInfo method = audioUtilClass.GetMethod(
    6.         "PlayClip",
    7.         System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public,
    8.         null,
    9.         new System.Type[] { typeof(AudioClip), typeof(int), typeof(bool) },
    10.         null
    11.     );
    12.     method.Invoke(
    13.         null,
    14.         new object[] { clip, startSample, loop }
    15.     );
    16. }
     
    IgorAherne, Ghosthowl, dis-s and 9 others like this.
  11. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    881
    thank's !
     
  12. ahsan9898

    ahsan9898

    Joined:
    Oct 8, 2019
    Posts:
    6
    Hey is there a method to pause an audio clip instead of Stop?
     
  13. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    26
unityunity