Просмотр исходного кода

增加unity Log重定向功能。author: NOW

tanghai 7 лет назад
Родитель
Сommit
c8083a7383

+ 106 - 0
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;
+
+/// <summary>
+/// 日志重定向相关的实用函数。
+/// author: NOW
+/// Time: 20180226
+/// </summary>
+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);
+    }
+}

+ 12 - 0
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: 

+ 8 - 4
Unity/Unity.Editor.csproj

@@ -12,13 +12,16 @@
     <ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
     <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
-    <TargetFrameworkProfile></TargetFrameworkProfile>
-    <CompilerResponseFile></CompilerResponseFile>
+    <TargetFrameworkProfile>
+    </TargetFrameworkProfile>
+    <CompilerResponseFile>
+    </CompilerResponseFile>
     <UnityProjectGenerator>VSTU</UnityProjectGenerator>
     <UnityProjectType>Editor:5</UnityProjectType>
     <UnityBuildTarget>StandaloneWindows:5</UnityBuildTarget>
     <UnityVersion>2017.1.3p2</UnityVersion>
-    <RootNamespace></RootNamespace>
+    <RootNamespace>
+    </RootNamespace>
     <LangVersion>6</LangVersion>
   </PropertyGroup>
   <PropertyGroup>
@@ -213,6 +216,7 @@
     <Compile Include="Assets\Editor\GlobalConfigEditor\GlobalConfigEditor.cs" />
     <Compile Include="Assets\Editor\Helper\EditorResHelper.cs" />
     <Compile Include="Assets\Editor\Helper\MongoHelper.cs" />
+    <Compile Include="Assets\Editor\LogRedirection.cs" />
     <Compile Include="Assets\Editor\Proto2CsEditor\Proto2CSEditor.cs" />
     <Compile Include="Assets\Editor\ReferenceCollectorEditor\ReferenceCollectorEditor.cs" />
     <Compile Include="Assets\Editor\RsyncEditor\RsyncConfig.cs" />
@@ -241,4 +245,4 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="GenerateTargetFrameworkMonikerAttribute" />
-</Project>
+</Project>