Preferences.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace EnhancedHierarchy {
  8. public enum TintMode {
  9. Flat = 0,
  10. GradientRightToLeft = 1,
  11. GradientLeftToRight = 2,
  12. }
  13. /// <summary>
  14. /// Per layer color setting.
  15. /// </summary>
  16. [Serializable]
  17. public struct LayerColor {
  18. [SerializeField]
  19. public int layer;
  20. [SerializeField]
  21. public Color color;
  22. [SerializeField]
  23. public TintMode mode;
  24. public LayerColor(int layer) : this(layer, Color.clear) { }
  25. public LayerColor(int layer, Color color, TintMode mode = TintMode.GradientRightToLeft) {
  26. this.layer = layer;
  27. this.color = color;
  28. this.mode = mode;
  29. }
  30. public static implicit operator LayerColor(int layer) {
  31. return new LayerColor(layer);
  32. }
  33. public static bool operator ==(LayerColor left, LayerColor right) {
  34. return left.layer == right.layer;
  35. }
  36. public static bool operator !=(LayerColor left, LayerColor right) {
  37. return left.layer != right.layer;
  38. }
  39. public override bool Equals(object obj) {
  40. if (!(obj is LayerColor))
  41. return false;
  42. return ((LayerColor)obj).layer == layer;
  43. }
  44. public override int GetHashCode() {
  45. return layer.GetHashCode();
  46. }
  47. }
  48. /// <summary>
  49. /// Save and load hierarchy preferences.
  50. /// </summary>
  51. public static partial class Preferences {
  52. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
  53. private class AutoPrefItemAttribute : Attribute {
  54. public string Key { get; private set; }
  55. public AutoPrefItemAttribute(string key = null) { Key = key; }
  56. }
  57. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
  58. private class AutoPrefItemDefaultValueAttribute : Attribute {
  59. public object DefaultValue { get; private set; }
  60. public AutoPrefItemDefaultValueAttribute(object defaultValue) { DefaultValue = defaultValue; }
  61. }
  62. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
  63. private class AutoPrefItemLabelAttribute : Attribute {
  64. public GUIContent Label { get; private set; }
  65. public AutoPrefItemLabelAttribute(string label, string tooltip = null) { Label = new GUIContent(label, tooltip); }
  66. }
  67. private static Color DefaultOddSortColor { get { return EditorGUIUtility.isProSkin ? new Color(0f, 0f, 0f, 0.1f) : new Color(1f, 1f, 1f, 0.2f); } }
  68. private static Color DefaultEvenSortColor { get { return EditorGUIUtility.isProSkin ? new Color(0f, 0f, 0f, 0f) : new Color(1f, 1f, 1f, 0f); } }
  69. private static Color DefaultLineColor { get { return new Color(0f, 0f, 0f, 0.2f); } }
  70. private static Color DefaultHoverTint { get { return EditorGUIUtility.isProSkin ? new Color(0f, 0f, 0f, 0.2f) : new Color(0.12f, 0.12f, 0.12f, 0.2f); } }
  71. #region PrefItems
  72. [AutoPrefItem]
  73. [AutoPrefItemDefaultValue(true)]
  74. [AutoPrefItemLabel("Enabled", "Enable or disable the entire plugin, it will be automatically disabled if any error occurs")]
  75. public static PrefItem<bool> Enabled;
  76. [AutoPrefItem]
  77. [AutoPrefItemDefaultValue(0)]
  78. [AutoPrefItemLabel("Right Margin", "Margin for icons, useful if you have more extensions that also uses hierarchy")]
  79. public static PrefItem<int> RightMargin;
  80. [AutoPrefItem]
  81. [AutoPrefItemDefaultValue(0)]
  82. [AutoPrefItemLabel("Left Margin", "Margin for icons, useful if you have more extensions that also uses hierarchy")]
  83. public static PrefItem<int> LeftMargin;
  84. [AutoPrefItem]
  85. [AutoPrefItemDefaultValue(14)]
  86. [AutoPrefItemLabel("Indent", "Indent for labels, useful for thin hierarchies")]
  87. public static PrefItem<int> Indent;
  88. [AutoPrefItem]
  89. [AutoPrefItemDefaultValue(0.8f)]
  90. [AutoPrefItemLabel("Hierarchy Tree Opacity", "The opacity of the tree view lines connecting child transforms to their parent, useful if you have multiple children inside children")]
  91. public static PrefItem<float> TreeOpacity;
  92. [AutoPrefItem]
  93. [AutoPrefItemDefaultValue(0.5f)]
  94. [AutoPrefItemLabel("Stem Proportion", "Stem length for hierarchy items that have no children")]
  95. public static PrefItem<float> TreeStemProportion;
  96. [AutoPrefItem]
  97. [AutoPrefItemDefaultValue(true)]
  98. [AutoPrefItemLabel("Select on Tree", "Select the parent when you click on the tree lines\n\nTHIS MAY AFFECT PERFORMANCE")]
  99. public static PrefItem<bool> SelectOnTree;
  100. [AutoPrefItem]
  101. [AutoPrefItemDefaultValue(true)]
  102. [AutoPrefItemLabel("Tooltips", "Shows tooltips, like this one")]
  103. public static PrefItem<bool> Tooltips;
  104. [AutoPrefItem]
  105. [AutoPrefItemDefaultValue(true)]
  106. [AutoPrefItemLabel("Relevant Tooltips Only", "Hide tooltips that have static texts")]
  107. public static PrefItem<bool> RelevantTooltipsOnly;
  108. [AutoPrefItem]
  109. [AutoPrefItemDefaultValue(true)]
  110. [AutoPrefItemLabel("Enhanced selection", "Allow selecting GameObjects by dragging over them with right mouse button")]
  111. public static PrefItem<bool> EnhancedSelection;
  112. [AutoPrefItem]
  113. [AutoPrefItemLabel("Highlight tint", "Tint the item under the mouse cursor")]
  114. public static PrefItem<Color> HoverTintColor;
  115. [AutoPrefItem]
  116. [AutoPrefItemDefaultValue(false)]
  117. [AutoPrefItemLabel("Hide native icon", "Hide the native icon on the left side of the name, introducted in Unity 2018.3")]
  118. public static PrefItem<bool> DisableNativeIcon;
  119. [AutoPrefItem]
  120. [AutoPrefItemDefaultValue(true)]
  121. [AutoPrefItemLabel("Trailing", "Append ... when names are bigger than the view area")]
  122. public static PrefItem<bool> Trailing;
  123. [AutoPrefItem]
  124. [AutoPrefItemDefaultValue(true)]
  125. [AutoPrefItemLabel("Select locked objects", "Allow selecting objects that are locked")]
  126. public static PrefItem<bool> AllowSelectingLockedObjects;
  127. [AutoPrefItem]
  128. [AutoPrefItemDefaultValue(false)]
  129. [AutoPrefItemLabel("Pick locked objects", "Allow picking objects that are locked on scene view\nObjects locked before you change this option will not be affected\nRequires Unity 2019.3 or newer")]
  130. public static PrefItem<bool> AllowPickingLockedObjects;
  131. [AutoPrefItem]
  132. [AutoPrefItemDefaultValue(true)]
  133. [AutoPrefItemLabel("Change all selected", "This will make the enable, lock, layer, tag and static buttons affect all selected objects in the hierarchy")]
  134. public static PrefItem<bool> ChangeAllSelected;
  135. [AutoPrefItem]
  136. [AutoPrefItemDefaultValue(true)]
  137. [AutoPrefItemLabel("Left side button at leftmost", "Put the left button to the leftmost side of the hierarchy, if disabled it will be next to the game object name")]
  138. public static PrefItem<bool> LeftmostButton;
  139. [AutoPrefItem]
  140. [AutoPrefItemDefaultValue(true)]
  141. [AutoPrefItemLabel("Open scripts of logs", "Clicking on warnings, logs or errors will open the script to edit in your IDE or text editor\n\nMAY AFFECT PERFORMANCE")]
  142. public static PrefItem<bool> OpenScriptsOfLogs;
  143. [AutoPrefItem]
  144. [AutoPrefItemDefaultValue(false)]
  145. [AutoPrefItemLabel("Replace default child toggle", "Replace the default toggle for expanding children to a new one that shows the children count")]
  146. public static PrefItem<bool> NumericChildExpand;
  147. [AutoPrefItem]
  148. [AutoPrefItemDefaultValue(true)]
  149. [AutoPrefItemLabel("Smaller font", "Use a smaller font on the minilabel for narrow hierarchies")]
  150. public static PrefItem<bool> SmallerMiniLabel;
  151. [AutoPrefItem]
  152. [AutoPrefItemDefaultValue(15)]
  153. [AutoPrefItemLabel("Icons Size", "The size of the icons in pixels")]
  154. public static PrefItem<int> IconsSize;
  155. [AutoPrefItem]
  156. [AutoPrefItemDefaultValue(true)]
  157. [AutoPrefItemLabel("Centralize when possible", "Centralize minilabel when there's only tag or only layer on it")]
  158. public static PrefItem<bool> CentralizeMiniLabelWhenPossible;
  159. [AutoPrefItem]
  160. [AutoPrefItemDefaultValue(true)]
  161. [AutoPrefItemLabel("Hide \"Untagged\" tag", "Hide default tag on minilabel")]
  162. public static PrefItem<bool> HideDefaultTag;
  163. [AutoPrefItem]
  164. [AutoPrefItemDefaultValue(true)]
  165. [AutoPrefItemLabel("Hide \"Default\" layer", "Hide default layer on minilabel")]
  166. public static PrefItem<bool> HideDefaultLayer;
  167. [AutoPrefItem]
  168. [AutoPrefItemDefaultValue(false)]
  169. [AutoPrefItemLabel("Hide default icon", "Hide the default game object icon")]
  170. public static PrefItem<bool> HideDefaultIcon;
  171. [AutoPrefItem]
  172. [AutoPrefItemDefaultValue(1)]
  173. [AutoPrefItemLabel("Line thickness", "Separator line thickness")]
  174. public static PrefItem<int> LineSize;
  175. [AutoPrefItem]
  176. [AutoPrefItemLabel("Odd row tint", "The tint of odd rows")]
  177. public static PrefItem<Color> OddRowColor;
  178. [AutoPrefItem]
  179. [AutoPrefItemLabel("Even row tint", "The tint of even rows")]
  180. public static PrefItem<Color> EvenRowColor;
  181. [AutoPrefItem]
  182. [AutoPrefItemLabel("Line tint", "The tint of separators line")]
  183. public static PrefItem<Color> LineColor;
  184. [AutoPrefItem]
  185. [AutoPrefItemLabel("Left side button", "The button that will appear in the left side of the hierarchy\nLooks better with \"Hierarchy Tree\" disabled")]
  186. public static PrefItem<IconData> LeftSideButtonPref;
  187. [AutoPrefItem]
  188. [AutoPrefItemLabel("Mini label", "The little label next to the GameObject name")]
  189. public static PrefItem<int[]> MiniLabels;
  190. [AutoPrefItem]
  191. [AutoPrefItemDefaultValue(ChildrenChangeMode.ObjectAndChildren)]
  192. [AutoPrefItemLabel("Lock", "Which objects will be locked when you click on the lock toggle")]
  193. public static PrefItem<ChildrenChangeMode> LockAskMode;
  194. [AutoPrefItem]
  195. [AutoPrefItemDefaultValue(ChildrenChangeMode.Ask)]
  196. [AutoPrefItemLabel("Layer", "Which objects will have their layer changed when you click on the layer button or on the mini label")]
  197. public static PrefItem<ChildrenChangeMode> LayerAskMode;
  198. [AutoPrefItem]
  199. [AutoPrefItemDefaultValue(ChildrenChangeMode.ObjectOnly)]
  200. [AutoPrefItemLabel("Tag", "Which objects will have their tag changed when you click on the tag button or on the mini label")]
  201. public static PrefItem<ChildrenChangeMode> TagAskMode;
  202. [AutoPrefItem]
  203. [AutoPrefItemDefaultValue(ChildrenChangeMode.Ask)]
  204. [AutoPrefItemLabel("Static", "Which flags will be changed when you click on the static toggle")]
  205. public static PrefItem<ChildrenChangeMode> StaticAskMode;
  206. [AutoPrefItem]
  207. [AutoPrefItemDefaultValue(ChildrenChangeMode.ObjectOnly)]
  208. [AutoPrefItemLabel("Icon", "Which objects will have their icon changed when you click on the icon button")]
  209. public static PrefItem<ChildrenChangeMode> IconAskMode;
  210. [AutoPrefItem]
  211. [AutoPrefItemLabel("Icons next to the object name", "The icons that appear next to the game object name")]
  212. public static PrefItem<IconList> LeftIcons;
  213. [AutoPrefItem]
  214. [AutoPrefItemLabel("Icons on the rightmost", "The icons that appear to the rightmost of the hierarchy")]
  215. public static PrefItem<IconList> RightIcons;
  216. [AutoPrefItem]
  217. [AutoPrefItemLabel("Per layer row color", "Set a row color for each different layer")]
  218. public static PrefItem<List<LayerColor>> PerLayerRowColors;
  219. #endregion
  220. public static MiniLabelProvider[] miniLabelProviders;
  221. public static IconBase LeftSideButton {
  222. get { return LeftSideButtonPref.Value.Icon; }
  223. set {
  224. LeftSideButtonPref.Value.Icon = value;
  225. LeftSideButtonPref.ForceSave();
  226. }
  227. }
  228. public static bool ProfilingEnabled {
  229. get {
  230. #if HIERARCHY_PROFILING
  231. return true;
  232. #else
  233. return false;
  234. #endif
  235. }
  236. }
  237. public static bool DebugEnabled {
  238. get {
  239. #if HIERARCHY_DEBUG
  240. return true;
  241. #else
  242. return false;
  243. #endif
  244. }
  245. }
  246. public static bool MiniLabelTagEnabled {
  247. get { return miniLabelProviders.Any(ml => ml is TagMiniLabel); }
  248. }
  249. public static bool MiniLabelLayerEnabled {
  250. get { return miniLabelProviders.Any(ml => ml is LayerMiniLabel); }
  251. }
  252. public static bool EnhancedSelectionSupported {
  253. get { return Application.platform == RuntimePlatform.WindowsEditor; }
  254. }
  255. static Preferences() {
  256. InitializePreferences();
  257. Enabled.Label.text = string.Format("Enabled ({0}+H)", Utility.CtrlKey);
  258. #if UNITY_2018_3_OR_NEWER
  259. LeftSideButtonPref.DefaultValue = new IconData() { Icon = new Icons.None() };
  260. #else
  261. LeftSideButtonPref.DefaultValue = new IconData() { Icon = new Icons.GameObjectIcon() };
  262. #endif
  263. LineColor.DefaultValue = DefaultLineColor;
  264. OddRowColor.DefaultValue = DefaultOddSortColor;
  265. EvenRowColor.DefaultValue = DefaultEvenSortColor;
  266. HoverTintColor.DefaultValue = DefaultHoverTint;
  267. var defaultLeftIcons = new IconList { new Icons.MonoBehaviourIcon(), new Icons.Warnings(), new Icons.SoundIcon() };
  268. var defaultRightIcons = new IconList { new Icons.Active(), new Icons.Lock(), new Icons.Static(), new Icons.PrefabApply() };
  269. var defaultLayerColors = new List<LayerColor> { new LayerColor(5, new Color(0f, 0f, 1f, 0.3019608f)) };
  270. LeftIcons.DefaultValue = defaultLeftIcons;
  271. RightIcons.DefaultValue = defaultRightIcons;
  272. PerLayerRowColors.DefaultValue = defaultLayerColors;
  273. MiniLabels.DefaultValue = new [] {
  274. Array.IndexOf(MiniLabelProvider.MiniLabelsTypes, typeof(LayerMiniLabel)),
  275. Array.IndexOf(MiniLabelProvider.MiniLabelsTypes, typeof(TagMiniLabel))
  276. };
  277. minilabelsNames = MiniLabelProvider.MiniLabelsTypes
  278. .Select(ml => ml == null? "None": ObjectNames.NicifyVariableName(ml.Name.Replace("MiniLabel", "")))
  279. .ToArray();
  280. leftIconsList = GenerateReordableList(LeftIcons);
  281. rightIconsList = GenerateReordableList(RightIcons);
  282. leftIconsList.onAddDropdownCallback = (rect, newList) => LeftIconsMenu.DropDown(rect);
  283. rightIconsList.onAddDropdownCallback = (rect, newList) => RightIconsMenu.DropDown(rect);
  284. rowColorsList = GenerateReordableList(PerLayerRowColors);
  285. rowColorsList.onAddDropdownCallback = (rect, newList) => RowColorsMenu.DropDown(rect);
  286. rowColorsList.drawElementCallback = (rect, index, focused, active) => {
  287. GUI.changed = false;
  288. rect.xMin -= EditorGUI.indentLevel * 16f;
  289. PerLayerRowColors.Value[index] = LayerColorField(rect, PerLayerRowColors.Value[index]);
  290. if (GUI.changed)
  291. PerLayerRowColors.ForceSave();
  292. };
  293. RecreateMiniLabelProviders();
  294. }
  295. public static void RecreateMiniLabelProviders() {
  296. miniLabelProviders = MiniLabels.Value
  297. .Select(ml => MiniLabelProvider.MiniLabelsTypes.ElementAtOrDefault(ml))
  298. .Where(ml => ml != null)
  299. .Select(ml => Activator.CreateInstance(ml)as MiniLabelProvider)
  300. .ToArray();
  301. }
  302. public static bool IsButtonEnabled(IconBase button) {
  303. if (button == null)
  304. return false;
  305. if (LeftSideButton == button)
  306. return true;
  307. return RightIcons.Value.Contains(button) || LeftIcons.Value.Contains(button);
  308. }
  309. public static void ForceDisableButton(IconBase button) {
  310. if (button == null)
  311. Debug.LogError("Removing null button");
  312. else
  313. Debug.LogWarning("Disabling \"" + button.Name + "\", most likely because it threw an exception");
  314. if (LeftSideButton == button)
  315. LeftSideButton = IconBase.none;
  316. RightIcons.Value.Remove(button);
  317. LeftIcons.Value.Remove(button);
  318. RightIcons.ForceSave();
  319. LeftIcons.ForceSave();
  320. }
  321. private static void InitializePreferences() {
  322. var type = typeof(Preferences);
  323. var members = type.GetMembers(ReflectionHelper.FULL_BINDING);
  324. foreach (var member in members)
  325. try {
  326. if (member == null)
  327. continue;
  328. var prefItemType = (Type)null;
  329. var prop = member as PropertyInfo;
  330. var field = member as FieldInfo;
  331. switch (member.MemberType) {
  332. case MemberTypes.Field:
  333. if (typeof(IPrefItem).IsAssignableFrom(field.FieldType))
  334. prefItemType = field.FieldType;
  335. else
  336. continue;
  337. break;
  338. case MemberTypes.Property:
  339. if (typeof(IPrefItem).IsAssignableFrom(prop.PropertyType))
  340. prefItemType = prop.PropertyType;
  341. else
  342. continue;
  343. break;
  344. default:
  345. continue;
  346. }
  347. var keyAttribute = (AutoPrefItemAttribute)member.GetCustomAttributes(typeof(AutoPrefItemAttribute), true).FirstOrDefault();
  348. var labelAttribute = (AutoPrefItemLabelAttribute)member.GetCustomAttributes(typeof(AutoPrefItemLabelAttribute), true).FirstOrDefault();
  349. var defaultValueAttribute = (AutoPrefItemDefaultValueAttribute)member.GetCustomAttributes(typeof(AutoPrefItemDefaultValueAttribute), true).FirstOrDefault();
  350. var key = member.Name;
  351. var defaultValue = (object)null;
  352. var label = new GUIContent(key);
  353. //var savedValueType = prefItemType.GetGenericArguments()[0];
  354. if (keyAttribute == null)
  355. continue;
  356. if (!string.IsNullOrEmpty(keyAttribute.Key))
  357. key = keyAttribute.Key;
  358. if (labelAttribute != null)
  359. label = labelAttribute.Label;
  360. if (defaultValueAttribute != null)
  361. defaultValue = defaultValueAttribute.DefaultValue;
  362. //else if(savedValueType.IsValueType)
  363. // defaultValue = Activator.CreateInstance(savedValueType);
  364. var prefItem = Activator.CreateInstance(prefItemType, key, defaultValue, label.text, label.tooltip);
  365. switch (member.MemberType) {
  366. case MemberTypes.Field:
  367. field.SetValue(null, prefItem);
  368. break;
  369. case MemberTypes.Property:
  370. prop.SetValue(null, prefItem, null);
  371. break;
  372. }
  373. } catch (Exception e) {
  374. Debug.LogException(e);
  375. }
  376. }
  377. }
  378. }