Przeglądaj źródła

增加编辑器Catch Exception的报错堆栈跳转 (#591)

Co-authored-by: zhangguoju <zhangguoju@123.com>
王二 1 rok temu
rodzic
commit
bfe2983189

+ 10 - 1
Unity/Assets/Scripts/Editor/LogRedirection/LogRedirection.cs

@@ -26,7 +26,16 @@ namespace ET
             string codePath = AssetDatabase.GetAssetPath(instanceID);
             string codePath = AssetDatabase.GetAssetPath(instanceID);
             if (logFileRegex.IsMatch(codePath))
             if (logFileRegex.IsMatch(codePath))
             {
             {
-                Match stackLineMatch = Regex.Match(GetStackTrace(), @"\(at (.+):([0-9]+)\)");
+                var content = GetStackTrace();
+                // 如果有手动输入的地址,就跳过
+                var hrefMath = Regex.Match(content, @"<a href=""(.*?)"" line=""(\w+)\"">.*?</a>");
+                if (hrefMath.Success)
+                {
+                    OpenIDE(hrefMath.Groups[1].Value, int.Parse(hrefMath.Groups[2].Value));
+                    return true;
+                }
+                
+                Match stackLineMatch = Regex.Match(content, @"\(at (.+):([0-9]+)\)");
                 while (stackLineMatch.Success)
                 while (stackLineMatch.Success)
                 {
                 {
                     codePath = stackLineMatch.Groups[1].Value;
                     codePath = stackLineMatch.Groups[1].Value;

+ 15 - 0
Unity/Assets/Scripts/Loader/UnityLogger.cs

@@ -1,4 +1,5 @@
 using System;
 using System;
+using System.Text.RegularExpressions;
 
 
 namespace ET
 namespace ET
 {
 {
@@ -26,8 +27,22 @@ namespace ET
 
 
         public void Error(string msg)
         public void Error(string msg)
         {
         {
+#if UNITY_EDITOR
+            msg = Msg2LinkStackMsg(msg);
+#endif
             UnityEngine.Debug.LogError(msg);
             UnityEngine.Debug.LogError(msg);
         }
         }
+        
+        private static string Msg2LinkStackMsg(string msg)
+        {
+            msg = Regex.Replace(msg,@"at (.*?) in (.*?\.cs):(\w+)", match =>
+            {
+                string path = match.Groups[2].Value;
+                string line = match.Groups[3].Value;
+                return $"{match.Groups[1].Value}\n<a href=\"{path}\" line=\"{line}\">{path}:{line}</a>";
+            });
+            return msg;
+        }
 
 
         public void Error(Exception e)
         public void Error(Exception e)
         {
         {