IconBase.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using EnhancedHierarchy.Icons;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace EnhancedHierarchy {
  8. public enum IconPosition {
  9. AfterObjectName = 1,
  10. BeforeObjectName = 2,
  11. RightMost = 4,
  12. SafeArea = AfterObjectName | RightMost,
  13. All = SafeArea | BeforeObjectName
  14. }
  15. public abstract class IconBase {
  16. private const float DEFAULT_WIDTH = 16f;
  17. public static None none = new None();
  18. public virtual string Name { get { return GetType().Name; } }
  19. public virtual float Width { get { return DEFAULT_WIDTH; } } // May be called multiple times in the same frame for the same object
  20. public virtual IconPosition Side { get { return IconPosition.SafeArea; } }
  21. public virtual Texture2D PreferencesPreview { get { return null; } }
  22. public virtual string PreferencesTooltip { get { return null; } }
  23. public static IconBase[] AllLeftIcons { get; private set; }
  24. public static IconBase[] AllRightIcons { get; private set; }
  25. public static IconBase[] AllLeftOfNameIcons { get; private set; }
  26. static IconBase() {
  27. var baseType = typeof(IconBase);
  28. icons = baseType.Assembly.GetTypes()
  29. .Where(t => t != baseType && baseType.IsAssignableFrom(t))
  30. .Select(t => (IconBase)Activator.CreateInstance(t))
  31. .ToDictionary(t => t.Name);
  32. AllLeftIcons = icons.Select(i => i.Value).Where(i => (i.Side & IconPosition.AfterObjectName) != 0).ToArray();
  33. AllRightIcons = icons.Select(i => i.Value).Where(i => (i.Side & IconPosition.RightMost) != 0).ToArray();
  34. AllLeftOfNameIcons = icons.Select(i => i.Value).Where(i => (i.Side & IconPosition.BeforeObjectName) != 0).ToArray();
  35. }
  36. public virtual void Init() { } // Guaranteed to be called only once for each obj in every frame before any DoGUI() and get Width calls
  37. public abstract void DoGUI(Rect rect);
  38. private static readonly Dictionary<string, IconBase> icons = new Dictionary<string, IconBase>();
  39. protected static ChildrenChangeMode AskChangeModeIfNecessary(List<GameObject> objs, ChildrenChangeMode reference, string title, string message) {
  40. var controlPressed = Event.current.control || Event.current.command;
  41. switch (reference) {
  42. case ChildrenChangeMode.ObjectOnly:
  43. return controlPressed ? ChildrenChangeMode.ObjectAndChildren : ChildrenChangeMode.ObjectOnly;
  44. case ChildrenChangeMode.ObjectAndChildren:
  45. return controlPressed ? ChildrenChangeMode.ObjectOnly : ChildrenChangeMode.ObjectAndChildren;
  46. default:
  47. for (var i = 0; i < objs.Count; i++)
  48. if (objs[i] && objs[i].transform.childCount > 0)
  49. try {
  50. return (ChildrenChangeMode)EditorUtility.DisplayDialogComplex(title, message, "Yes, change children", "No, this object only", "Cancel");
  51. } finally {
  52. //Unity bug, DisplayDialogComplex makes the unity partially lose focus
  53. if (EditorWindow.focusedWindow)
  54. EditorWindow.focusedWindow.Focus();
  55. }
  56. return ChildrenChangeMode.ObjectOnly;
  57. }
  58. }
  59. protected static List<GameObject> GetSelectedObjectsAndCurrent() {
  60. if (!Preferences.ChangeAllSelected || Selection.gameObjects.Length < 2)
  61. return EnhancedHierarchy.CurrentGameObject ?
  62. new List<GameObject> { EnhancedHierarchy.CurrentGameObject } :
  63. new List<GameObject>();
  64. return Selection.gameObjects
  65. .Where(obj => !EditorUtility.IsPersistent(obj)) // Makes sure the object is part of the scene and not the project
  66. .Union(EnhancedHierarchy.CurrentGameObject ? new [] { EnhancedHierarchy.CurrentGameObject } : new GameObject[0])
  67. .Distinct()
  68. .ToList();
  69. }
  70. public static bool operator ==(IconBase left, IconBase right) {
  71. if (ReferenceEquals(left, right))
  72. return true;
  73. if (ReferenceEquals(left, null))
  74. return false;
  75. if (ReferenceEquals(right, null))
  76. return false;
  77. return left.Name == right.Name;
  78. }
  79. public static bool operator !=(IconBase left, IconBase right) {
  80. return !(left == right);
  81. }
  82. public override string ToString() {
  83. return Name;
  84. }
  85. public override int GetHashCode() {
  86. return Name.GetHashCode();
  87. }
  88. public override bool Equals(object obj) {
  89. return obj as IconBase == this;
  90. }
  91. public static implicit operator IconBase(string name) {
  92. try { return icons[name]; } catch { return none; }
  93. }
  94. public static implicit operator string(IconBase icon) {
  95. return icon.ToString();
  96. }
  97. }
  98. }