Advertisement
BlackDragonBE

SoundOnScriptReload.cs

Oct 24th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Reflection;
  5. using UnityEditor;
  6.  
  7. [ExecuteInEditMode]
  8. public class SoundOnScriptReload : MonoBehaviour
  9. {
  10.     [UnityEditor.Callbacks.DidReloadScripts]
  11.     private static void OnScriptsReloaded()
  12.     {
  13.         var audioClip = (AudioClip)EditorGUIUtility.Load("Assets/Sounds/" + "ScriptsDone" + ".ogg");
  14.         PlayClip(audioClip);
  15.     }
  16.  
  17.     public static void PlayClip(AudioClip clip)
  18.     {
  19.         Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
  20.         Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
  21.         MethodInfo method = audioUtilClass.GetMethod(
  22.             "PlayClip",
  23.             BindingFlags.Static | BindingFlags.Public,
  24.             null,
  25.             new System.Type[] {
  26.                 typeof(AudioClip)
  27.             },
  28.             null
  29.         );
  30.         method.Invoke(
  31.             null,
  32.             new object[] {
  33.                 clip
  34.             }
  35.         );
  36.     }
  37.     public static void StopAllClips()
  38.     {
  39.         Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
  40.         Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
  41.         MethodInfo method = audioUtilClass.GetMethod(
  42.             "StopAllClips",
  43.             BindingFlags.Static | BindingFlags.Public,
  44.             null,
  45.             new System.Type[] { },
  46.             null
  47.         );
  48.         method.Invoke(
  49.             null,
  50.             new object[] { }
  51.         );
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement