1. Before updating to Windows 10 you may need to return your Pro license. More information here

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
    Messages:
    547
    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
  2. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Messages:
    547
    {Bumping once since it may have been missed since I posted it late Friday}
     
  3. _Daniel_

    _Daniel_

    Joined:
    Feb 28, 2007
    Messages:
    2,037
    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
    Messages:
    547
    Thanks Daniel, I'll definitely give these a shot :)
     
  5. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Messages:
    547
    The gameObject.hideFlags = HideFlags.HideAndDontSave; worked! Thanks Daniel.
     
  6. _Daniel_

    _Daniel_

    Joined:
    Feb 28, 2007
    Messages:
    2,037
    Happy to hear :)
     
  7. AShim.3D

    AShim.3D

    Joined:
    Jul 13, 2012
    Messages:
    28
    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
    Messages:
    21
    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