HierarchyInfo.cs 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6. namespace EnhancedHierarchy
  7. {
  8. public static partial class EnhancedHierarchy
  9. {
  10. public const int UNLAYERED = 0;
  11. public const string UNTAGGED = "Untagged";
  12. public const float ALPHA_THRESHOLD = 0.01f;
  13. private static readonly GUIContent trailingContent = new GUIContent("...");
  14. public static string GameObjectName { get; private set; }
  15. public static string GameObjectTag { get; private set; }
  16. public static bool IsFirstVisible { get; private set; }
  17. public static bool IsRepaintEvent { get; private set; }
  18. public static bool IsGameObject { get; private set; }
  19. public static bool HasTag { get; private set; }
  20. public static bool HasLayer { get; private set; }
  21. public static float LeftIconsWidth { get; private set; }
  22. public static float RightIconsWidth { get; private set; }
  23. public static float LabelSize { get; private set; }
  24. public static Rect RawRect { get; private set; }
  25. public static Rect FullSizeRect { get; private set; }
  26. public static Rect FinalRect { get; private set; }
  27. public static Rect SelectionRect { get; private set; }
  28. public static Rect LabelOnlyRect { get; private set; }
  29. public static Color CurrentColor { get; private set; }
  30. public static Vector2 SelectionStart { get; private set; }
  31. public static GUIStyle CurrentStyle { get; private set; }
  32. public static GameObject CurrentGameObject { get; private set; }
  33. public static List<Object> DragSelection { get; private set; }
  34. public static readonly List<Component> Components = new List<Component>(100);
  35. public static EventType LastEventType { get; private set; }
  36. public static void SetItemInformation(int id, Rect rect)
  37. {
  38. if (!Preferences.Enabled)
  39. return;
  40. using (ProfilerSample.Get("Enhanced Hierarchy"))
  41. using (ProfilerSample.Get())
  42. try
  43. {
  44. CurrentGameObject = EditorUtility.InstanceIDToObject(id) as GameObject;
  45. IsGameObject = CurrentGameObject;
  46. IsRepaintEvent = Event.current.type == EventType.Repaint;
  47. IsFirstVisible = Event.current.type != LastEventType;
  48. LastEventType = Event.current.type;
  49. if (IsGameObject)
  50. {
  51. GameObjectName = CurrentGameObject.name;
  52. try
  53. {
  54. GameObjectTag = CurrentGameObject.tag;
  55. }
  56. catch
  57. {
  58. // I couldn't reproduce this, but it can happen
  59. if (Preferences.DebugEnabled)
  60. Debug.LogWarning("Invalid gameobject tag", CurrentGameObject);
  61. GameObjectTag = "Untagged";
  62. }
  63. LabelSize = EditorStyles.label.CalcSize(Utility.GetTempGUIContent(GameObjectName)).x;
  64. LabelSize += Reflected.IconWidth + 5f; // Icon size
  65. var labelOnlyRect = rect;
  66. labelOnlyRect.xMax = labelOnlyRect.xMin + LabelSize;
  67. LabelOnlyRect = labelOnlyRect;
  68. HasTag = !CurrentGameObject.CompareTag(UNTAGGED) || !Preferences.HideDefaultTag;
  69. HasLayer = CurrentGameObject.layer != UNLAYERED || !Preferences.HideDefaultLayer;
  70. CurrentStyle = Utility.GetHierarchyLabelStyle(CurrentGameObject);
  71. CurrentColor = CurrentStyle.normal.textColor;
  72. CurrentGameObject.GetComponents(Components);
  73. }
  74. if (IsFirstVisible)
  75. FinalRect = RawRect;
  76. RawRect = rect;
  77. rect.xMin = 0f;
  78. //rect.xMax = EditorGUIUtility.currentViewWidth;
  79. FullSizeRect = rect;
  80. }
  81. catch (Exception e)
  82. {
  83. Utility.LogException(e);
  84. }
  85. }
  86. }
  87. }