DebugLogItem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. using System.Text.RegularExpressions;
  7. #endif
  8. // A UI element to show information about a debug entry
  9. namespace IngameDebugConsole
  10. {
  11. public class DebugLogItem : MonoBehaviour, IPointerClickHandler
  12. {
  13. #pragma warning disable 0649
  14. // Cached components
  15. [SerializeField]
  16. private RectTransform transformComponent;
  17. public RectTransform Transform { get { return transformComponent; } }
  18. [SerializeField]
  19. private Image imageComponent;
  20. public Image Image { get { return imageComponent; } }
  21. [SerializeField]
  22. private Text logText;
  23. [SerializeField]
  24. private Image logTypeImage;
  25. // Objects related to the collapsed count of the debug entry
  26. [SerializeField]
  27. private GameObject logCountParent;
  28. [SerializeField]
  29. private Text logCountText;
  30. #pragma warning restore 0649
  31. // Debug entry to show with this log item
  32. private DebugLogEntry logEntry;
  33. // Index of the entry in the list of entries
  34. private int entryIndex;
  35. public int Index { get { return entryIndex; } }
  36. private DebugLogRecycledListView manager;
  37. public void Initialize( DebugLogRecycledListView manager )
  38. {
  39. this.manager = manager;
  40. }
  41. public void SetContent( DebugLogEntry logEntry, int entryIndex, bool isExpanded )
  42. {
  43. this.logEntry = logEntry;
  44. this.entryIndex = entryIndex;
  45. Vector2 size = transformComponent.sizeDelta;
  46. if( isExpanded )
  47. {
  48. logText.horizontalOverflow = HorizontalWrapMode.Wrap;
  49. size.y = manager.SelectedItemHeight;
  50. }
  51. else
  52. {
  53. logText.horizontalOverflow = HorizontalWrapMode.Overflow;
  54. size.y = manager.ItemHeight;
  55. }
  56. transformComponent.sizeDelta = size;
  57. logText.text = isExpanded ? logEntry.ToString() : logEntry.logString;
  58. logTypeImage.sprite = logEntry.logTypeSpriteRepresentation;
  59. }
  60. // Show the collapsed count of the debug entry
  61. public void ShowCount()
  62. {
  63. logCountText.text = logEntry.count.ToString();
  64. logCountParent.SetActive( true );
  65. }
  66. // Hide the collapsed count of the debug entry
  67. public void HideCount()
  68. {
  69. logCountParent.SetActive( false );
  70. }
  71. // This log item is clicked, show the debug entry's stack trace
  72. public void OnPointerClick( PointerEventData eventData )
  73. {
  74. #if UNITY_EDITOR
  75. if( eventData.button == PointerEventData.InputButton.Right )
  76. {
  77. Match regex = Regex.Match( logEntry.stackTrace, @"\(at .*\.cs:[0-9]+\)$", RegexOptions.Multiline );
  78. if( regex.Success )
  79. {
  80. string line = logEntry.stackTrace.Substring( regex.Index + 4, regex.Length - 5 );
  81. int lineSeparator = line.IndexOf( ':' );
  82. MonoScript script = AssetDatabase.LoadAssetAtPath<MonoScript>( line.Substring( 0, lineSeparator ) );
  83. if( script != null )
  84. AssetDatabase.OpenAsset( script, int.Parse( line.Substring( lineSeparator + 1 ) ) );
  85. }
  86. }
  87. else
  88. manager.OnLogItemClicked( this );
  89. #else
  90. manager.OnLogItemClicked( this );
  91. #endif
  92. }
  93. public float CalculateExpandedHeight( string content )
  94. {
  95. string text = logText.text;
  96. HorizontalWrapMode wrapMode = logText.horizontalOverflow;
  97. logText.text = content;
  98. logText.horizontalOverflow = HorizontalWrapMode.Wrap;
  99. float result = logText.preferredHeight;
  100. logText.text = text;
  101. logText.horizontalOverflow = wrapMode;
  102. return Mathf.Max( manager.ItemHeight, result );
  103. }
  104. // Return a string containing complete information about the debug entry
  105. public override string ToString()
  106. {
  107. return logEntry.ToString();
  108. }
  109. }
  110. }