Advertisement
Powerhoof

Unity project window audio buttons

Mar 23rd, 2023
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.23 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using PowerTools;
  6. using System.Reflection;
  7.  
  8.  
  9. namespace PowerTools.Quest
  10. {
  11.  
  12. [InitializeOnLoad]
  13. public static class AudioProjectWindowDetails
  14. {
  15.     static AudioProjectWindowDetails()
  16.     {
  17.         EditorApplication.projectWindowItemOnGUI += DrawAssetDetails;
  18.     }
  19.  
  20.     static void DrawAssetDetails(string guid, Rect rect)
  21.     {
  22.         Event ev = Event.current;
  23.         if (Application.isPlaying || (ev.type != EventType.Repaint && ev.type != EventType.MouseUp && ev.type != EventType.MouseDown/*  && ev.type != EventType.ExecuteCommand*/ ) || !IsMainListAsset(rect))      
  24.             return;
  25.        
  26.         string assetPath = AssetDatabase.GUIDToAssetPath(guid);
  27.         if (AssetDatabase.IsValidFolder(assetPath))    
  28.             return;
  29.        
  30.         Object asset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);
  31.         if (asset == null)  // this entry could be Favourites or Packages. Ignore it.
  32.             return;
  33.  
  34.         if ( asset is AudioClip )
  35.         {
  36.             if ( DrawAudioClip(rect, asset as AudioClip) )
  37.                 return;
  38.         }
  39.  
  40.         GameObject gobj = (asset as GameObject);
  41.         if ( gobj == null )
  42.             return;
  43.         if ( DrawAudioCue(rect,gobj) )
  44.             return;
  45.     }
  46.  
  47.     static bool DrawAudioClip(Rect rect, AudioClip clip)
  48.     {      
  49.         // Draw "New Cue" button, or "Add Cue" if there's an selected audiocue selected
  50.         if ( clip == null )
  51.             return false;
  52.        
  53.         EditorLayouter layout = new EditorLayouter(rect);
  54.         layout
  55.             .Stretched // name
  56.             .Fixed(80) // Add
  57.             //.Space
  58.             .Fixed(40); // Play
  59.         layout.Skip();
  60.  
  61.         GUIStyle style = new GUIStyle(EditorStyles.miniButton);
  62.         style.fixedHeight = style.fixedHeight-4f;
  63.        
  64.         if ( Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<AudioCue>() != null)
  65.         {
  66.             if ( GUI.Button(layout,"Add to Cue", style) )
  67.             {
  68.                 AudioCue cue = Selection.activeGameObject.GetComponent<AudioCue>();            
  69.                 cue.m_sounds.Add( new AudioCue.Clip() { m_sound = clip, m_weight = 100 } );
  70.                 cue.GetShuffledIndex().SetWeights( cue.m_sounds, (item)=>item.m_weight );              
  71.                 EditorUtility.SetDirty(Selection.activeGameObject);
  72.             }
  73.         }
  74.         else
  75.         {
  76.             if ( GUI.Button(layout,"New Cue", style) )
  77.             {
  78.                 if ( Selection.objects.Length > 0 && System.Array.Find(Selection.objects, item=>item == clip) )
  79.                     AudioCueEditor.ContextCreateAudioCue(new MenuCommand(null)); // create all seelected into single cue
  80.                 else
  81.                     AudioCueEditor.CreateAudioCue(new AudioClip[]{clip}); // create new cue from single item
  82.             }
  83.         }
  84.         // Play button
  85.         if ( GUI.Button(layout,"Play",style))
  86.         {
  87.             PlayClip(clip,0,false);
  88.         }
  89.         return true;
  90.    
  91.     }
  92.     public static void PlayClip(AudioClip clip, int startSample, bool loop)
  93.     {
  94.         Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
  95.         System.Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
  96.         MethodInfo method = audioUtilClass.GetMethod(
  97.             "PlayPreviewClip",
  98.             BindingFlags.Static | BindingFlags.Public,
  99.             null,
  100.             new System.Type[] {
  101.             typeof(AudioClip),
  102.             typeof(System.Int32),
  103.             typeof(System.Boolean)
  104.         },
  105.         null
  106.         );
  107.         method?.Invoke(
  108.             null,
  109.             new object[] { clip,startSample,loop }
  110.         );
  111.    
  112.         //SetClipSamplePosition(clip, startSample);
  113.     }
  114.  
  115.     static bool DrawAudioCue(Rect rect, GameObject gobj)
  116.     {
  117.         AudioCue cue = gobj.GetComponent<AudioCue>();
  118.         if ( cue == null )
  119.             return false;
  120.        
  121.         const int width = 40;
  122.         rect.x += rect.width-width;
  123.         rect.width = width;
  124.  
  125.         GUIStyle style = new GUIStyle(EditorStyles.miniButton);
  126.         style.fixedHeight = style.fixedHeight-4f;
  127.        
  128.         if ( SystemAudio.GetValid() && SystemAudio.IsPlaying(gobj.name) )
  129.         {
  130.             if ( GUI.Button(rect,"Stop", style) )
  131.             {
  132.                     if ( Application.isEditor && SystemAudio.GetValid() )
  133.                             GameObject.DestroyImmediate( SystemAudio.Get.gameObject );
  134.                     SystemAudio.Stop(gobj.name,0.1f);
  135.             }          
  136.         }
  137.         else if ( GUI.Button(rect,"Play", style) )
  138.         {          
  139.             if ( Application.isEditor && SystemAudio.GetValid() )
  140.                     GameObject.DestroyImmediate( SystemAudio.Get.gameObject );
  141.             SystemAudio.Play(cue);//(asset as GameObject).GetComponent<AudioCue>());
  142.         }
  143.         return true;
  144.     }
  145.  
  146.     static bool IsMainListAsset(Rect rect)
  147.     {
  148.         // Don't draw details if project view shows large preview icons:
  149.         if (rect.height > 20)
  150.         {
  151.             return false;
  152.         }
  153.         // Don't draw details if this asset is a sub asset:
  154.         if (rect.x > 16)
  155.         {
  156.             return false;
  157.         }
  158.         return true;
  159.     }
  160.    
  161. }
  162.  
  163. }
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement