Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by Steven-Walker · Dec 02, 2010 at 05:09 PM · editoraudiosourceaudioclip

How to play AudioClip in edit mode?

I want to play an audio clip in edit mode, but I'm not having any luck. I'm creating a custom inspector that works with audio files and I'd like to be able to preview the sounds in the editor.

Here's what I've tried so far. It doesn't work, but I wouldn't expect it to really since I imagine the audio listener is only working in play mode.

var cam : GameObject = GameObject.Find("GUICamera"); var clip : AudioClip = AssetDatabase.LoadAssetAtPath("Assets/Page01/Audio/FullAudio.wav", typeof(AudioClip)) as AudioClip;

var go = new GameObject("PLAY_AUDIO_TEMP"); go.transform.position = cam.transform.position;

var source : AudioSource = go.AddComponent(AudioSource) as AudioSource; source.clip = clip; source.volume = 1.0; source.Play();

Are there any other scripting tools for working with audio in the editor that I've overlooked?

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyo · Dec 07, 2010 at 09:54 PM 0
Share

I guess the standard audio clip preview isn't sufficient? (Click on the audio asset in Project window, then click the play button in the preview window below the inspector.)

avatar image Steven-Walker · Dec 08, 2010 at 06:02 PM 0
Share

It is really tedious to have to keep selecting different objects, especially when the asset database is very large. $$anonymous$$y scene may only use a dozen audio clips, but there are hundreds in the database. To go and find each one just isn't a good workflow thus the reason why I'd like to trigger the audio playback from my custom inspector.

avatar image Bunny83 · Dec 28, 2010 at 05:37 AM 0
Share

You can use the static function AudioSource.PlayClipAtPoint() then you don't need to create a temp GO.

There are two things they come to my $$anonymous$$d: first i think you need an AudioListener in the scene or you can't hear anything. And second, I'm not sure if this affects editor scripts or not but you can toggle the sound at the top of the scene-window.

avatar image yoyo · Jan 13, 2011 at 09:16 PM 0
Share

You could procedurally find all the audio clips (in the scene) and their corresponding asset (in the project) -- let the user choose from a list and you can set Selection.objects to do the selection. Still a workaround, would be better to be able to play directly. Did you try Bunny83's suggestion?

avatar image Steven-Walker · Jan 13, 2011 at 11:52 PM 0
Share

I have tried Bunny83's suggestion, but nothing happens using AudioSource.PLayClipAtPoint(). $$anonymous$$y guess is that the AudioListener component only listens during runtime, not in edit mode.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Flynn · Jan 17, 2011 at 10:59 AM

The audio preview mode must be on in order for sound to play in the editor.

Inside the scene view, near the top, in the left/right corner, there is a little speaker icon. Click that, and your audio should work.

NOTE: The audio preview mode is only available in Unity 3.0

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Steven-Walker · Jan 18, 2011 at 06:28 PM 0
Share

Thank you! I completely overlooked that feature.

avatar image
9

Answer by ocimum · Dec 19, 2015 at 02:57 PM

For playing sound in EditMode you could just create an EditorWindow or a CustomEditor. Then you also don't have a problem with generating any additional GameObject just for holding the AudioSource.

For loading an AudioClip inside an Editor script, you have to execute this line:

 (AudioClip) EditorGUIUtility.Load("Assets/AnotherDirectory/" + audioClipName + ".wav");

For playing or stopping the sound use following methods method:

 public static void PlayClip(AudioClip clip) {
     Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
     Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
     MethodInfo method = audioUtilClass.GetMethod(
         "PlayClip",
         BindingFlags.Static | BindingFlags.Public,
         null,
         new System.Type[] {
         typeof(AudioClip)
     },
     null
     );
     method.Invoke(
         null,
         new object[] {
         clip
     }
     );
 }

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

Have fun!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dasm30 · Feb 22, 2016 at 05:20 AM 0
Share

This works perfectly!

avatar image Alaiing · Mar 28, 2017 at 02:01 PM 0
Share

Awesome. Just used your code in a custom editor and it's perfect. Thanks! :)

avatar image MR_Ford · Oct 13, 2018 at 03:03 PM 0
Share

Thank you work great in my custom editor I used it like this PlayClip((AudioClip)element.FindPropertyRelative("clip").objectReferenceValue);

avatar image anisabboud · Sep 03, 2019 at 09:01 AM 0
Share

Update for Unity 2019: https://forum.unity.com/threads/way-to-play-audio-in-editor-using-an-editor-script.132042/#post-4767824 (need to add int startSample = 0, bool loop = false parameters when invoking PlayClip).

avatar image awsapps · Aug 17, 2021 at 08:35 AM 0
Share

Best solution, but UnityEditor.AudioUtil doesn't provide methods to change the pitch/reverb/volume of the clip though.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Audio Output Data 0 Answers

Microphone recording problems (double sample, audiosource stops working) 0 Answers

Call a function on Object's Texture change! 1 Answer

Extract audio clip from VideoPlayer for real-time processing 0 Answers

Only one audio active. Others do not activate 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges