Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Reflection;
- using UnityEditor;
- using UnityEngine;
- public class Example
- {
- [InitializeOnLoadMethod]
- private static void Hoge()
- {
- // AudioClip は用意する必要がある
- var clip = AssetDatabase.LoadAssetAtPath<AudioClip>( "Assets/TypeWriter Sound Effects/Key 1.wav" );
- var isDown = false;
- EditorApplication.CallbackFunction function = () =>
- {
- var e = Event.current;
- if ( !isDown && e.type == EventType.KeyDown )
- {
- isDown = true;
- PreviewAutioClip( clip );
- }
- if ( isDown && e.type == EventType.KeyUp )
- {
- isDown = false;
- }
- };
- var bindingAttr = BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic;
- var info = typeof( EditorApplication ).GetField( "globalEventHandler", bindingAttr );
- var functions = info.GetValue( null ) as EditorApplication.CallbackFunction;
- functions += function;
- info.SetValue( null, functions );
- }
- private static void PreviewAutioClip( AudioClip clip )
- {
- var unityEditorAssembly = typeof( AudioImporter ).Assembly;
- var audioUtilClass = unityEditorAssembly.GetType( "UnityEditor.AudioUtil" );
- var method = audioUtilClass.GetMethod
- (
- "PlayClip",
- BindingFlags.Static | BindingFlags.Public,
- null,
- new Type[] { typeof(AudioClip) },
- null
- );
- method.Invoke( null, new object[] { clip } );
- }
- }
Add Comment
Please, Sign In to add comment