Skip to content

Instantly share code, notes, and snippets.

@Syy9
Created July 30, 2018 15:40
Show Gist options
  • Save Syy9/b26fc40a0469daddaed4395e9b27a8a8 to your computer and use it in GitHub Desktop.
Save Syy9/b26fc40a0469daddaed4395e9b27a8a8 to your computer and use it in GitHub Desktop.

Revisions

  1. Syy9 created this gist Jul 30, 2018.
    34 changes: 34 additions & 0 deletions AutoPlayPreviewAudio.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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});
    }
    }