From c8083a73834a7baa2421243266ff0c828822c0ee Mon Sep 17 00:00:00 2001 From: tanghai Date: Tue, 8 May 2018 17:52:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0unity=20Log=E9=87=8D=E5=AE=9A?= =?UTF-8?q?=E5=90=91=E5=8A=9F=E8=83=BD=E3=80=82author:=20NOW?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Unity/Assets/Editor/LogRedirection.cs | 106 +++++++++++++++++++++ Unity/Assets/Editor/LogRedirection.cs.meta | 12 +++ Unity/Unity.Editor.csproj | 12 ++- 3 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 Unity/Assets/Editor/LogRedirection.cs create mode 100644 Unity/Assets/Editor/LogRedirection.cs.meta diff --git a/Unity/Assets/Editor/LogRedirection.cs b/Unity/Assets/Editor/LogRedirection.cs new file mode 100644 index 00000000..ebf0c192 --- /dev/null +++ b/Unity/Assets/Editor/LogRedirection.cs @@ -0,0 +1,106 @@ +using System.Reflection; +using System.Text.RegularExpressions; +using UnityEditor; +using UnityEditor.Callbacks; +using UnityEditorInternal; +using UnityEngine; + +/// +/// 日志重定向相关的实用函数。 +/// author: NOW +/// Time: 20180226 +/// +internal static class LogRedirection +{ + private static readonly Regex LogRegex = new Regex(@" \(at (.+)\:(\d+)\)\r?\n"); + + [OnOpenAsset(0)] + private static bool OnOpenAsset(int instanceId, int line) + { + string selectedStackTrace = GetSelectedStackTrace(); + if (string.IsNullOrEmpty(selectedStackTrace)) + { + return false; + } + + if (!selectedStackTrace.Contains("Model.Log")) + { + return false; + } + + Match match = LogRegex.Match(selectedStackTrace); + if (!match.Success) + { + return false; + } + + // 跳过第一次匹配的堆栈 + match = match.NextMatch(); + if (!match.Success) + { + return false; + } + if (!selectedStackTrace.Contains("Hotfix.Log")) + { + InternalEditorUtility.OpenFileAtLineExternal(Application.dataPath.Replace("Assets", "") + match.Groups[1].Value, int.Parse(match.Groups[2].Value)); + + return true; + } + else + { + // 跳过第2次匹配的堆栈 + match = match.NextMatch(); + if (!match.Success) + { + return false; + } + InternalEditorUtility.OpenFileAtLineExternal(Application.dataPath.Replace("Assets", "") + match.Groups[1].Value, int.Parse(match.Groups[2].Value)); + + return true; + } + + + + + } + + private static string GetSelectedStackTrace() + { + Assembly editorWindowAssembly = typeof(EditorWindow).Assembly; + if (editorWindowAssembly == null) + { + return null; + } + + System.Type consoleWindowType = editorWindowAssembly.GetType("UnityEditor.ConsoleWindow"); + if (consoleWindowType == null) + { + return null; + } + + FieldInfo consoleWindowFieldInfo = consoleWindowType.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic); + if (consoleWindowFieldInfo == null) + { + return null; + } + + EditorWindow consoleWindow = consoleWindowFieldInfo.GetValue(null) as EditorWindow; + if (consoleWindow == null) + { + return null; + } + + if (consoleWindow != EditorWindow.focusedWindow) + { + return null; + } + + FieldInfo activeTextFieldInfo = consoleWindowType.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic); + if (activeTextFieldInfo == null) + { + return null; + } + + return (string)activeTextFieldInfo.GetValue(consoleWindow); + } +} \ No newline at end of file diff --git a/Unity/Assets/Editor/LogRedirection.cs.meta b/Unity/Assets/Editor/LogRedirection.cs.meta new file mode 100644 index 00000000..f5ce4337 --- /dev/null +++ b/Unity/Assets/Editor/LogRedirection.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 311d256107e00ed4bbdd3f0d81922b82 +timeCreated: 1525772883 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Unity/Unity.Editor.csproj b/Unity/Unity.Editor.csproj index 2081845c..174a50c9 100644 --- a/Unity/Unity.Editor.csproj +++ b/Unity/Unity.Editor.csproj @@ -12,13 +12,16 @@ {E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} .NETFramework v4.6 - - + + + + VSTU Editor:5 StandaloneWindows:5 2017.1.3p2 - + + 6 @@ -213,6 +216,7 @@ + @@ -241,4 +245,4 @@ - + \ No newline at end of file -- GitLab