Unity Community


Results 1 to 8 of 8

  1. Location
    Texas
    Posts
    519

    Way to play audio in editor using an editor script? [Solved]

    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 by Nathan; 04-16-2012 at 11:23 AM.


  2. Location
    Texas
    Posts
    519
    {Bumping once since it may have been missed since I posted it late Friday}


  3. Location
    Sacramento CA, USA
    Posts
    1,604
    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. Location
    Texas
    Posts
    519
    Thanks Daniel, I'll definitely give these a shot


  5. Location
    Texas
    Posts
    519
    The gameObject.hideFlags = HideFlags.HideAndDontSave; worked! Thanks Daniel.


  6. Location
    Sacramento CA, USA
    Posts
    1,604


  7. Location
    Ukraine
    Posts
    24
    Quote Originally Posted by _Daniel_ View Post
    I dont know of any editor methods to play a sound cleanly for you (like one might look like EditorUtility.PlaySound()).
    You can make this method:
    Code:  
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System;
    4. using System.Reflection;
    5.  
    6. public static class PublicAudioUtil {
    7.    
    8.     public static void PlayClip(AudioClip clip) {
    9.         Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
    10.         Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
    11.         MethodInfo method = audioUtilClass.GetMethod(
    12.             "PlayClip",
    13.             BindingFlags.Static | BindingFlags.Public,
    14.             null,
    15.             new System.Type[] {
    16.                 typeof(AudioClip)
    17.             },
    18.             null
    19.         );
    20.         method.Invoke(
    21.             null,
    22.             new object[] {
    23.                 clip
    24.             }
    25.         );
    26.     } // PlayClip()
    27.  
    28. } // class PublicAudioUtil
    Last edited by AShim.3D; 10-21-2013 at 08:00 AM. Reason: class with method


  8. Location
    Brazil, Sao Paulo
    Posts
    4
    Quote Originally Posted by AShim.3D View Post
    You can make this method:
    Code:  
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Reflection;
    4. ...
    5. pulic static void PlayClip(AudioClip clip) {
    6.     Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
    7.     Type audioUtilClass = assembly.GetType("UnityEditor.AudioUtil");
    8.     MethodInfo method = audioUtilClass.GetMethod(
    9.         "PlayClip",
    10.         BindingFlags.Static | BindingFlags.Public,
    11.         null,
    12.         new System.Type[] {
    13.             typeof(AudioClip)
    14.         },
    15.         null
    16.     );
    17.     method.Invoke(
    18.         null,
    19.         new object[] {
    20.             clip
    21.         }
    22.     );
    23. }
    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 :

    Code:  
    1. public static void PlayClip(AudioClip clip) {
    2.     Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
    3.    System.Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
    4.     MethodInfo method = audioUtilClass.GetMethod(
    5.         "PlayClip",
    6.         BindingFlags.Static | BindingFlags.Public,
    7.         null,
    8.         new System.Type[] {
    9.             typeof(AudioClip)
    10.         },
    11.         null
    12.     );
    13.     method.Invoke(
    14.         null,
    15.         new object[] {
    16.             clip
    17.         }
    18.     );
    Now, there's one question in the air... how do i stop the sound?!
    hahah, again, thanks!
    Last edited by Mukabr; 10-20-2013 at 02:50 PM. Reason: found the awnser

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •