Skip to content

Instantly share code, notes, and snippets.

@Syy9
Created July 30, 2018 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Syy9/b26fc40a0469daddaed4395e9b27a8a8 to your computer and use it in GitHub Desktop.
Save Syy9/b26fc40a0469daddaed4395e9b27a8a8 to your computer and use it in GitHub Desktop.
How to Play Select Audio
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