Created
July 30, 2018 15:40
-
-
Save Syy9/b26fc40a0469daddaed4395e9b27a8a8 to your computer and use it in GitHub Desktop.
How to Play Select Audio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using System.Linq; | |
using System.Reflection; | |
using System; | |
public static class AutoPlayPreviewAudio { | |
[InitializeOnLoadMethod] | |
static void Init() | |
{ | |
Selection.selectionChanged += OnSelectionChanged; | |
} | |
private static void OnSelectionChanged() | |
{ | |
var clip = Selection.objects.Where(obj => obj is AudioClip).FirstOrDefault(); | |
if(clip == null) | |
return; | |
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}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment