MonoBehaviourIcon.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace EnhancedHierarchy.Icons {
  7. public sealed class MonoBehaviourIcon : IconBase {
  8. private static readonly Dictionary<Type, string> monoBehaviourNames = new Dictionary<Type, string>();
  9. private static readonly StringBuilder goComponents = new StringBuilder(500);
  10. private static readonly GUIContent tempTooltipContent = new GUIContent();
  11. private static bool hasMonoBehaviour;
  12. public override string Name { get { return "MonoBehaviour Icon"; } }
  13. public override float Width { get { return hasMonoBehaviour ? 15f : 0f; } }
  14. public override IconPosition Side { get { return IconPosition.All; } }
  15. public override Texture2D PreferencesPreview { get { return AssetPreview.GetMiniTypeThumbnail(typeof(MonoScript)); } }
  16. //public override string PreferencesTooltip { get { return "Some tag for the tooltip here"; } }
  17. public override void Init() {
  18. hasMonoBehaviour = false;
  19. if (!EnhancedHierarchy.IsGameObject)
  20. return;
  21. var components = EnhancedHierarchy.Components;
  22. for (var i = 0; i < components.Count; i++)
  23. if (components[i] is MonoBehaviour) {
  24. hasMonoBehaviour = true;
  25. break;
  26. }
  27. }
  28. public override void DoGUI(Rect rect) {
  29. if (!EnhancedHierarchy.IsRepaintEvent || !EnhancedHierarchy.IsGameObject || !hasMonoBehaviour)
  30. return;
  31. if (Utility.ShouldCalculateTooltipAt(rect) && Preferences.Tooltips) {
  32. goComponents.Length = 0;
  33. var components = EnhancedHierarchy.Components;
  34. for (var i = 0; i < components.Count; i++)
  35. if (components[i] is MonoBehaviour)
  36. goComponents.AppendLine(GetComponentName(components[i]));
  37. tempTooltipContent.tooltip = goComponents.ToString().TrimEnd('\n', '\r');
  38. } else
  39. tempTooltipContent.tooltip = string.Empty;
  40. rect.yMin += 1f;
  41. rect.yMax -= 1f;
  42. rect.xMin += 1f;
  43. GUI.DrawTexture(rect, Styles.monobehaviourIconTexture, ScaleMode.ScaleToFit);
  44. EditorGUI.LabelField(rect, tempTooltipContent);
  45. }
  46. private static string GetComponentName(Component component) {
  47. string result;
  48. var type = component.GetType();
  49. if (monoBehaviourNames.TryGetValue(type, out result))
  50. return result;
  51. else
  52. return monoBehaviourNames[type] = type.ToString();
  53. }
  54. }
  55. }