HKI Core
PublicAudioUtil.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEditor;
3 using System;
4 using System.Reflection;
5 
6 namespace HKI.Core.Audio.Editor
7 {
11  public static class PublicAudioUtil
12  {
13  // Interface functions
14  public static void PlayClip(AudioClip clip)
15  {
16  if(clip == null)
17  return;
18 
19  StopClipPlaying();
20 
21  Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
22 
23  Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
24 
25  MethodInfo method = audioUtilClass.GetMethod("PlayClip", BindingFlags.Static | BindingFlags.Public, null, new System.Type[] { typeof(AudioClip) }, null);
26 
27  method.Invoke(null, new object[] { clip });
28  }
29 
30  public static bool IsClipPlaying(AudioClip clip)
31  {
32  if (clip == null)
33  return false;
34 
35  Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
36 
37  Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
38 
39  MethodInfo method = audioUtilClass.GetMethod("IsClipPlaying", BindingFlags.Static | BindingFlags.Public, null, new System.Type[] { typeof(AudioClip) }, null);
40 
41  return (bool)method.Invoke(null, new object[] { clip });
42  }
43 
44  public static void StopClipPlaying()
45  {
46  Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
47 
48  Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
49 
50  MethodInfo method = audioUtilClass.GetMethod("StopAllClips", BindingFlags.Static | BindingFlags.Public, null, new Type[] { }, null);
51  method.Invoke(null, new object[] { });
52  }
53  }
54 }
static void PlayClip(AudioClip clip)
This class provides the capability to listen into audio clips in the editor.
static bool IsClipPlaying(AudioClip clip)