SoundIcon.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEditor.AnimatedValues;
  4. using UnityEngine;
  5. namespace EnhancedHierarchy.Icons {
  6. public sealed class SoundIcon : IconBase {
  7. private static AudioSource audioSource;
  8. private static AnimBool currentAnim;
  9. private static readonly Dictionary<AudioSource, AnimBool> sourcesAnim = new Dictionary<AudioSource, AnimBool>();
  10. private static Texture icon;
  11. public override string Name { get { return "Audio Source Icon"; } }
  12. public override float Width {
  13. get {
  14. if (audioSource == null || currentAnim == null)
  15. return 0f;
  16. return currentAnim.faded * (base.Width - 2f);
  17. }
  18. }
  19. public override Texture2D PreferencesPreview { get { return AssetPreview.GetMiniTypeThumbnail(typeof(AudioSource)); } }
  20. //public override string PreferencesTooltip { get { return "Some tag for the tooltip here"; } }
  21. static SoundIcon() {
  22. EditorApplication.update += () => {
  23. if (!Preferences.IsButtonEnabled(new SoundIcon()))
  24. return;
  25. foreach (var kvp in sourcesAnim)
  26. if (kvp.Key && kvp.Value != null)
  27. kvp.Value.target = kvp.Key.isPlaying;
  28. };
  29. }
  30. public override void Init() {
  31. if (!EnhancedHierarchy.IsGameObject)
  32. return;
  33. var comps = EnhancedHierarchy.Components;
  34. audioSource = null;
  35. for (var i = 0; i < comps.Count; i++)
  36. if (comps[i] is AudioSource) {
  37. audioSource = comps[i] as AudioSource;
  38. break;
  39. }
  40. if (!audioSource)
  41. return;
  42. if (!sourcesAnim.TryGetValue(audioSource, out currentAnim)) {
  43. sourcesAnim[audioSource] = currentAnim = new AnimBool(audioSource.isPlaying);
  44. currentAnim.valueChanged.AddListener(EditorApplication.RepaintHierarchyWindow);
  45. }
  46. }
  47. public override void DoGUI(Rect rect) {
  48. if (!EnhancedHierarchy.IsRepaintEvent || !EnhancedHierarchy.IsGameObject || !audioSource || Width <= 1f)
  49. return;
  50. using(ProfilerSample.Get()) {
  51. if (!icon)
  52. icon = EditorGUIUtility.ObjectContent(null, typeof(AudioSource)).image;
  53. rect.yMax -= 1f;
  54. rect.yMin += 1f;
  55. GUI.DrawTexture(rect, icon, ScaleMode.StretchToFill);
  56. }
  57. }
  58. }
  59. }