Warnings.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace EnhancedHierarchy.Icons {
  6. public sealed class Warnings : IconBase {
  7. private const int MAX_STRING_LEN = 750;
  8. private const float ICONS_WIDTH = 16f;
  9. public static StringBuilder goLogs = new StringBuilder(MAX_STRING_LEN);
  10. public static StringBuilder goWarnings = new StringBuilder(MAX_STRING_LEN);
  11. public static StringBuilder goErrors = new StringBuilder(MAX_STRING_LEN);
  12. private static readonly GUIContent tempTooltipContent = new GUIContent();
  13. private LogEntry log;
  14. private LogEntry warning;
  15. private LogEntry error;
  16. public override string Name { get { return "Logs, Warnings and Errors"; } }
  17. public override float Width {
  18. get {
  19. var result = 0f;
  20. if (goLogs.Length > 0)
  21. result += ICONS_WIDTH;
  22. if (goWarnings.Length > 0)
  23. result += ICONS_WIDTH;
  24. if (goErrors.Length > 0)
  25. result += ICONS_WIDTH;
  26. return result;
  27. }
  28. }
  29. public override Texture2D PreferencesPreview { get { return Styles.warningIcon; } }
  30. //public override string PreferencesTooltip { get { return "Some tag for the tooltip here"; } }
  31. public override void Init() {
  32. if (!EnhancedHierarchy.IsGameObject)
  33. return;
  34. log = null;
  35. warning = null;
  36. error = null;
  37. goLogs.Length = 0;
  38. goWarnings.Length = 0;
  39. goErrors.Length = 0;
  40. var contextEntries = (List<LogEntry>)null;
  41. var components = EnhancedHierarchy.Components;
  42. for (var i = 0; i < components.Count; i++)
  43. if (!components[i])
  44. goWarnings.AppendLine("Missing MonoBehaviour\n");
  45. foreach (var entry in LogEntry.compileEntries
  46. .Where(entry => entry.ClassType != null)
  47. .Where(entry => EnhancedHierarchy.Components
  48. .Any(comp => comp && (comp.GetType() == entry.ClassType || comp.GetType().IsAssignableFrom(entry.ClassType))))) {
  49. var isWarning = entry.HasMode(EntryMode.ScriptCompileWarning | EntryMode.AssetImportWarning);
  50. if (goWarnings.Length < MAX_STRING_LEN && isWarning)
  51. goWarnings.AppendLine(entry.ToString());
  52. else if (goErrors.Length < MAX_STRING_LEN && !isWarning)
  53. goErrors.AppendLine(entry.ToString());
  54. if (isWarning && warning == null && !string.IsNullOrEmpty(entry.File))
  55. warning = entry;
  56. if (!isWarning && error == null && !string.IsNullOrEmpty(entry.File))
  57. error = entry;
  58. }
  59. if (LogEntry.gameObjectEntries.TryGetValue(EnhancedHierarchy.CurrentGameObject, out contextEntries))
  60. for (var i = 0; i < contextEntries.Count; i++) {
  61. var entry = contextEntries[i];
  62. var isLog = entry.HasMode(EntryMode.ScriptingLog);
  63. var isWarning = entry.HasMode(EntryMode.ScriptingWarning);
  64. var isError = entry.HasMode(EntryMode.ScriptingError | EntryMode.ScriptingException | EntryMode.ScriptingAssertion);
  65. if (isLog && goLogs.Length < MAX_STRING_LEN)
  66. goLogs.AppendLine(entry.ToString());
  67. else if (isWarning && goWarnings.Length < MAX_STRING_LEN)
  68. goWarnings.AppendLine(entry.ToString());
  69. else if (isError && goErrors.Length < MAX_STRING_LEN)
  70. goErrors.AppendLine(entry.ToString());
  71. if (isLog && log == null && !string.IsNullOrEmpty(entry.File))
  72. log = entry;
  73. if (isWarning && warning == null && !string.IsNullOrEmpty(entry.File))
  74. warning = entry;
  75. if (isError && error == null && !string.IsNullOrEmpty(entry.File))
  76. error = entry;
  77. }
  78. }
  79. public override void DoGUI(Rect rect) {
  80. if ((!EnhancedHierarchy.IsRepaintEvent && !Preferences.OpenScriptsOfLogs) || !EnhancedHierarchy.IsGameObject)
  81. return;
  82. rect.xMax = rect.xMin + 17f;
  83. rect.yMax += 1f;
  84. DoSingleGUI(ref rect, goLogs, Styles.infoIcon, log);
  85. DoSingleGUI(ref rect, goWarnings, Styles.warningIcon, warning);
  86. DoSingleGUI(ref rect, goErrors, Styles.errorIcon, error);
  87. }
  88. private void DoSingleGUI(ref Rect rect, StringBuilder str, Texture2D icon, LogEntry entry) {
  89. if (str.Length == 0)
  90. return;
  91. if (Utility.ShouldCalculateTooltipAt(rect))
  92. tempTooltipContent.tooltip = Preferences.Tooltips ? str.ToString().TrimEnd('\n', '\r') : string.Empty;
  93. tempTooltipContent.image = icon;
  94. if (GUI.Button(rect, tempTooltipContent, Styles.iconButton))
  95. if (entry != null)
  96. entry.OpenToEdit();
  97. rect.x += ICONS_WIDTH;
  98. }
  99. }
  100. }