Guest User

Untitled

a guest
Nov 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using UnityEditor;
  4. using UnityEngine;
  5.  
  6. public class Example
  7. {
  8. [InitializeOnLoadMethod]
  9. private static void Hoge()
  10. {
  11. // AudioClip は用意する必要がある
  12. var clip = AssetDatabase.LoadAssetAtPath<AudioClip>( "Assets/TypeWriter Sound Effects/Key 1.wav" );
  13.  
  14. var isDown = false;
  15. EditorApplication.CallbackFunction function = () =>
  16. {
  17. var e = Event.current;
  18. if ( !isDown && e.type == EventType.KeyDown )
  19. {
  20. isDown = true;
  21. PreviewAutioClip( clip );
  22. }
  23. if ( isDown && e.type == EventType.KeyUp )
  24. {
  25. isDown = false;
  26. }
  27. };
  28. var bindingAttr = BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic;
  29. var info = typeof( EditorApplication ).GetField( "globalEventHandler", bindingAttr );
  30. var functions = info.GetValue( null ) as EditorApplication.CallbackFunction;
  31. functions += function;
  32. info.SetValue( null, functions );
  33. }
  34.  
  35. private static void PreviewAutioClip( AudioClip clip )
  36. {
  37. var unityEditorAssembly = typeof( AudioImporter ).Assembly;
  38. var audioUtilClass = unityEditorAssembly.GetType( "UnityEditor.AudioUtil" );
  39.  
  40. var method = audioUtilClass.GetMethod
  41. (
  42. "PlayClip",
  43. BindingFlags.Static | BindingFlags.Public,
  44. null,
  45. new Type[] { typeof(AudioClip) },
  46. null
  47. );
  48.  
  49. method.Invoke( null, new object[] { clip } );
  50. }
  51. }
Add Comment
Please, Sign In to add comment