Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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
    awsapps and Hunter_Bobeck like 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. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    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. User340

    User340

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

    AShim-3D

    Joined:
    Jul 13, 2012
    Posts:
    34
    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
    Deadcow_ and SamFernGamer4k like this.
  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
    SamFernGamer4k and CabinIcarus like 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.
     
    SamFernGamer4k and ngwood111 like this.
  10. Gustav

    Gustav

    Joined:
    Jun 10, 2013
    Posts:
    2
    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. }
     
  11. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    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:
    30
  14. Thom_Denick

    Thom_Denick

    Joined:
    May 19, 2014
    Posts:
    15
    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))

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System;
    4. using System.Reflection;
    5.  
    6. public static class EditorSFX
    7. {
    8.  
    9.     public static void PlayClip(AudioClip clip, int startSample = 0, bool loop = false)
    10.     {
    11.         Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
    12.      
    13.         Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
    14.         MethodInfo method = audioUtilClass.GetMethod(
    15.             "PlayPreviewClip",
    16.             BindingFlags.Static | BindingFlags.Public,
    17.             null,
    18.             new Type[] { typeof(AudioClip), typeof(int), typeof(bool) },
    19.             null
    20.         );
    21.  
    22.         Debug.Log(method);
    23.         method.Invoke(
    24.             null,
    25.             new object[] { clip, startSample, loop }
    26.         );
    27.     }
    28.  
    29.     public static void StopAllClips()
    30.     {
    31.         Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
    32.  
    33.         Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
    34.         MethodInfo method = audioUtilClass.GetMethod(
    35.             "StopAllPreviewClips",
    36.             BindingFlags.Static | BindingFlags.Public,
    37.             null,
    38.             new Type[] { },
    39.             null
    40.         );
    41.  
    42.         Debug.Log(method);
    43.         method.Invoke(
    44.             null,
    45.             new object[] { }
    46.         );
    47.     }
    48. }
     
    Last edited: Apr 7, 2021
  15. HexaCraft

    HexaCraft

    Joined:
    Nov 17, 2020
    Posts:
    2
    Where should i attach this script
     
  16. awsapps

    awsapps

    Joined:
    Jun 15, 2021
    Posts:
    74
    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.
     
  17. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    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.
     
    awsapps likes this.
  18. awsapps

    awsapps

    Joined:
    Jun 15, 2021
    Posts:
    74
    Thanks, yesterday I figured out how to make a AudioSource work in EditMode with the [ExecuteAlways] attribute ;)
     
  19. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    Can you show how, PLEASE!!??
     
  20. awsapps

    awsapps

    Joined:
    Jun 15, 2021
    Posts:
    74
    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.
     
  21. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    Yes, please!!!
     
  22. awsapps

    awsapps

    Joined:
    Jun 15, 2021
    Posts:
    74
    It's very rudimentary but with this, you should be able to listen the AudioSource (set it near an AudioListener):

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. #if UNITY_EDITOR
    5. using UnityEditor;
    6. #endif
    7.  
    8. [ExecuteAlways]
    9. public class SoundInEditor : MonoBehaviour
    10. {
    11.     public AudioSource _AudioSource;
    12.     public void Play()
    13.     {
    14.         _AudioSource.Play();
    15.     }
    16. }
    17.  
    18.  
    19. #if UNITY_EDITOR
    20. [CustomEditor(typeof(SoundInEditor))]
    21. public class SoundInEditorDrawer : Editor
    22. {
    23.     Texture2D _PlayButtonIcon;
    24.     private SoundInEditor script;
    25.     private void OnEnable()
    26.     {
    27.         script = (SoundInEditor)target;
    28.     }
    29.     public override void OnInspectorGUI()
    30.     {
    31.  
    32.         if (GUILayout.Button(GetPlayButtonTexture()))
    33.             script.Play();
    34.         EditorGUILayout.LabelField("AudioSource");
    35.         script._AudioSource = (AudioSource)EditorGUILayout.ObjectField(script._AudioSource, typeof(AudioSource), true);
    36.     }
    37.     private Texture2D GetPlayButtonTexture()
    38.     {
    39.         if (_PlayButtonIcon == null)
    40.         {
    41.             byte[] pixel_info = new byte[]
    42.             {
    43.             0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xfe,0xff,0x00,0x00,0x00,0xff,0x00,0x01,0x01,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0x00,0x00,0x01,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0xff,0xff,0xfe,0xfe,0xff,0x01,0x01,0x00,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x01,0x00,0xff,0xfe,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0x01,0x00,0x01,0xff,0x00,0x00,0x01,0xff,0x01,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0x00,0x00,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0x00,0x00,0x00,0xff,0x00,0x01,0x00,0xff,0x00,0x00,0x01,0xff,0x01,0x00,0x01,0xff,0x00,0x00,0x00,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0x01,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x01,0xff,0x00,0x00,0x01,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0x01,0xff,0x00,0x01,0x00,0xff,0x01,0x00,0x00,0xff,0x00,0x00,0x01,0xff,0xff,0xfe,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xfe,0xfe,0xff,0x00,0x00,0x00,0xff,0xfe,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0x01,0x01,0x00,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x01,0x01,0xff,0x00,0x00,0x01,0xff,0x00,0x00,0x01,0xff,0x00,0x01,0x00,0xff,0x01,0x00,0x00,0xff,0x01,0x00,0x01,0xff,0x00,0x01,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xfe,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0x00,0x01,0x00,0xff,0x01,0x00,0x00,0xff,0x01,0x00,0x00,0xff,0x00,0x00,0x01,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0xff,0xff,0xfe,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x01,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xff,0x00,0x00,0x00,0xff,0x01,0x01,0x00,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0x01,0xff,0x01,0x00,0x00,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xfe,0xff,0xff,0xfe,0xff,0xff,0x01,0x01,0x00,0xff,0xfe,0xfe,0xff,0xff,0xff,0xfe,0xfe,0xff,0x00,0x01,0x00,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0x01,0xff,0x01,0x00,0x00,0xff,0x00,0x00,0x00,0xff,0x01,0x00,0x00,0xff,0x00,0x01,0x00,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xfe,0xff,0xff,0x01,0x01,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0xff,0x01,0x01,0x00,0xff,0x01,0x01,0x00,0xff,0x01,0x01,0x01,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xfe,0xfe,0xff,0xff,0xfe,0xfe,0xff,0xff,0x00,0x00,0x00,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0x00,0x01,0x00,0xff,0x00,0x00,0x00,0xff,0x00,0x01,0x01,0xff,0x00,0x00,0x00,0xff,0x00,0x01,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0x00,0x00,0x01,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xfe,0xff,0x00,0x00,0x01,0xff,0xff,0xfe,0xff,0xff,0xfe,0xfe,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0xff,0x00,0x01,0x00,0xff,0x01,0x00,0x01,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xff,0xff,0x01,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x01,0x00,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x01,0xff,0x00,0x01,0x00,0xff,0x01,0x01,0x00,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x01,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xfe,0xff,0xff,0xfe,0xfe,0xff,0xfe,0xff,0xfe,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0xff,0x00,0x01,0x00,0xff,0xff,0xff,0xfe,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xff,0xff,0xfe,0xff,0xff,0xff,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff
    44.             };
    45.             _PlayButtonIcon = new Texture2D(16, 16, TextureFormat.RGBA32, false);
    46.             _PlayButtonIcon.LoadRawTextureData(pixel_info);
    47.             _PlayButtonIcon.Apply();
    48.         }
    49.         return _PlayButtonIcon;
    50.     }
    51.  
    52. }
    53. #endif
     
  23. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    Wow. I'm so glad I asked.

    Was never going to get close to this, in a million years.

    Thank you!

    But that's not enough.

    Miles of gratitude stretch out across the eons. May all your loved ones be blessed.

    THANK YOU!!!

    It just works™
     
  24. awsapps

    awsapps

    Joined:
    Jun 15, 2021
    Posts:
    74
    IAW!
     
    april_4_short likes this.
  25. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    Is this supposed to still work in 2021.2 or were they renamed again?

    EDIT:

    So apparently I had a typo. Yes, this still works. Go string definitions! :)
     
    Last edited: Aug 24, 2021
    april_4_short likes this.
  26. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    823
    I wanted to build the possibility to easily trigger notification sounds for errors or finished processes. This Thread helped me a lot, thanks!
    upload_2021-10-24_22-28-56.png
    If you're in the need for something like that, take a look at the repo:
    https://github.com/JohannesDeml/EditorAudioUtils
     
  27. EntangledGames

    EntangledGames

    Joined:
    Mar 8, 2013
    Posts:
    9
  28. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    303
    Thank you! This is cool.

    Is there any way to set the audio clips by code rather than in the editor? The editor window in Project Settings -> EditorAudioUtils is broken for me - probably due to Odin, because that is usually the culprit. It works in a fresh project, but not in my main project.

    Alternatively, is there any way to play the clip at a specific volume? I can grab an audio clip from my existing audio manager script, but the volume is way too high by default. So I was going to edit it and save a new version of the clip at a lower volume - and that's what led me to my first question.
     
  29. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    823
    Huh, interesting. I just took a look, and with odin it is also not drawing correctly for me. However, it is mostly just drawing the content of the underlying scriptableobject, so you can just change that.
    upload_2021-12-8_22-12-54.png

    You find it in the samples, or if you didn't import the samples it is automatically generated in Assets/Editor Default Resources/EditorNotificationSettings.asset.

    No, you can't set the volume, there is no interface provided for that from unity: https://github.com/Unity-Technologi...tor/Mono/Audio/Bindings/AudioUtil.bindings.cs
     
    _eternal likes this.
  30. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    303
    Thank you! I found the ScriptableObject in Editor Default Resources, and I got it working. All good :)
     
  31. pokelocos

    pokelocos

    Joined:
    Nov 9, 2015
    Posts:
    54
    Hello, I have a answer, I am using the "EditorSFX" class, How can I change the volume of the sound?
     
  32. lamtacla

    lamtacla

    Joined:
    Mar 31, 2019
    Posts:
    12
    Hello,

    This thread helped me a lot.

    Could anybody help me, can Unity can auto play Animation clip when I click to an fbx file, or an .anim file, in the Preview windows under the inspector.
    Or at least, any shortcut key to do it?

    Like the Audio, it take me alot of time to click the Play button, when browse thousands of Animation in project.

    Thank you.
     
  33. theashbot

    theashbot

    Joined:
    Apr 17, 2022
    Posts:
    23
    Hey thank you for the code but I get this message when I try it, and the audio does not play.
    Void PlayPreviewClip(UnityEngine.AudioClip, Int32, Boolean)
     
  34. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    823
    Which Unity version are you on? In my repo I handle the different Unity versions in the EditorAudioUtility class. I think that should work for you, you can just call `EditorAudioUtility.PlayPreviewClip(AudioClip audioClip)`, after you add the package to your project :)
     
    theashbot likes this.
  35. theashbot

    theashbot

    Joined:
    Apr 17, 2022
    Posts:
    23
    thank you for the vary fast response, and sorry for the slow reply. but I am using unity 2021.3.22f

    EDIT: So I imported the repo from GitHub, but sadly it still did not work. the sound just does not play. but when I create a new GameObject attach a audio Source then assign the audio clip then play the audio it works, but I do not like to spawn a new GameObject.
     
    Last edited: Apr 19, 2023
  36. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    823
    That is very odd, I would not expect any problems on 2021.3.x. Can you preview audio clips from the inspector?
    Screenshot 2023-04-19 at 08.53.32.png
    Pressing this button for an audio clip is kind of the same call as the one I'm wrapping in my class.
    And just to be clear, you also tried these buttons in the project settings (once you imported the project and set some audio files in there), and they don't work?
    Screenshot 2023-04-19 at 08.55.05.png

    How and when do you call the sounds? Is it after a heavy operation? Then you might need to stall the call to the next frame (see https://github.com/JohannesDeml/EditorAudioUtils/blob/main/Scripts/Editor/EditorAudioUtility.cs#L58)
     
  37. theashbot

    theashbot

    Joined:
    Apr 17, 2022
    Posts:
    23
    When I am in the preview for the audio clip the sound plays. when I am in the project manager on the EditorAudioUtils it plays, when I call the EditorAudioUtility.PlayNotificationSound function it plays the notification sound but for some odd reason when I call the EditorAudioUtility.PlayPreviewClip it does not want to play the audio clip, but the same audio clip does play when a make a new GameObject then play the sound on it. thank you for the fast rely.
     
  38. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    823
    Hm, I would guess then you probably don't pass the AudioClip correctly, since EditorAudioUtility.PlayNotificationSound also just calls EditorAudioUtility.PlayPreviewClip (see code). Are you sure the AudioClip you are passing is not null / the file you want? Also if you haven't checked, you can also set the audio clip you want to play as a notiificaiton sound. If you can then play that very sound through EditorAudioUtility.PlayNotificationSound I'm very certain that the problem is your setup in the script. If you want to you can share your code and I'll have a look.
     
    KidneyShift likes this.
  39. theashbot

    theashbot

    Joined:
    Apr 17, 2022
    Posts:
    23
    So I did some testing, and I think it has something to do with the audio clip being generated in real time. so my code calls a web API that converts text to audio. When I use the EditorAudioUtility.PlayPreviewClip to play the audio it does not work. but when I make a new Game Object and attach an audio score to it etc. it works. I would give you my code but I plan on releasing it as part of my unity asset that converts Text to speech, and if I did give you the code you would have to set up a Text to speech account for it to work. I think the problem is with it being generated in real time or something like that. because when I save the audio clip as an unity audio file it works just fine. thank you for your time.
     
  40. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    823
    Ah, I see. Runtime generated AudioClips could really be a problem if they just live in memory.
    If you fall back to creating game objects take a look at HideFlags.HideAndDontSave. This should at least make the usage a bit cleaner, as user won't notice the game objects in their hierarchy and they won't be saved. However, you're then still bound to the user not having the game view muted (Which I would imagine is quite common) and I experienced problems with playing clips through audio sources in edit mode. If you will only use it for runtime mode and expect users to have the game unmuted, then I think this is a very viable solution :)
     
  41. theashbot

    theashbot

    Joined:
    Apr 17, 2022
    Posts:
    23
    Thank you for all your help. I did not know about HideFlags.HideAndDontSave so I am going to try that.