SFXUtil.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #if UNITY_EDITOR
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using System.Reflection;
  7. namespace KairoEngine.SFX
  8. {
  9. public static class SFXUtil
  10. {
  11. public static void PlayClip(AudioClip clip, int startSample = 0, bool loop = false)
  12. {
  13. System.Reflection.Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
  14. System.Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
  15. System.Reflection.MethodInfo method = audioUtilClass.GetMethod(
  16. "PlayClip",
  17. System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public,
  18. null,
  19. new System.Type[] { typeof(AudioClip), typeof(int), typeof(bool) },
  20. null
  21. );
  22. method.Invoke(
  23. null,
  24. new object[] { clip, startSample, loop }
  25. );
  26. }
  27. public static void StopClip(AudioClip clip) {
  28. Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
  29. System.Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
  30. MethodInfo method = audioUtilClass.GetMethod(
  31. "StopClip",
  32. BindingFlags.Static | BindingFlags.Public,
  33. null,
  34. new System.Type[] {
  35. typeof(AudioClip)
  36. },
  37. null
  38. );
  39. method.Invoke(
  40. null,
  41. new object[] {
  42. clip
  43. }
  44. );
  45. }
  46. public static void StopAllClips () {
  47. Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
  48. System.Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
  49. MethodInfo method = audioUtilClass.GetMethod(
  50. "StopAllClips",
  51. BindingFlags.Static | BindingFlags.Public
  52. );
  53. method.Invoke(
  54. null,
  55. null
  56. );
  57. }
  58. }
  59. }
  60. #endif