前言
UnityConsoleWindow双击定位Debug.Log输出日志。
但是一般封装Debug.Log日志定位就不是自己想要的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| using System.Collections.Generic; using System.Reflection; using System.Text.RegularExpressions; using UnityEditor; public class LogEditor { private static LogEditor m_Instance; public static LogEditor GetInstacne() { if (m_Instance == null) { m_Instance = new LogEditor(); } return m_Instance; } private const string DEBUGERFILEPATH = "Assets/Script/Util/HHLogger.cs"; private int m_DebugerFileInstanceId; private Type m_ConsoleWindowType = null; private FieldInfo m_ActiveTextInfo; private FieldInfo m_ConsoleWindowFileInfo; private LogEditor() { UnityEngine. Object debuggerFile = AssetDatabase.LoadAssetAtPath(DEBUGERFILEPATH,typeof(UnityEngine.Object)); m_DebugerFileInstanceId = debuggerFile.GetInstanceID(); m_ConsoleWindowType = Type.GetType("UnityEditor.ConsoleWindow,UnityEditor"); m_ActiveTextInfo = m_ConsoleWindowType.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic); m_ConsoleWindowFileInfo = m_ConsoleWindowType.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic); } [UnityEditor.Callbacks.OnOpenAssetAttribute(-1)] private static bool OnOpenAsset(int instanceID, int line) { if (instanceID == LogEditor.GetInstacne().m_DebugerFileInstanceId && line != -1) { return LogEditor.GetInstacne().FindCode(); } return false; } public bool FindCode() { var windowInstance = m_ConsoleWindowFileInfo.GetValue(null); var activeText = m_ActiveTextInfo.GetValue(windowInstance); string[] contentStrings = activeText.ToString().Split('\n'); List<string> filePath = new List<string>(); for (int index = 0; index < contentStrings.Length; index++) { if (contentStrings[index].Contains("at")) { filePath.Add(contentStrings[index]); } } bool success = PingAndOpen(filePath[1]); return success; } public bool PingAndOpen(string fileContext) { string regexRule = @"at ([\w\W]*):(\d+)\)"; Match match = Regex.Match(fileContext, regexRule); if (match.Groups.Count > 1) { string path = match.Groups[1].Value; string line = match.Groups[2].Value; UnityEngine.Object codeObject = AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object)); if (codeObject == null) { return false; } EditorGUIUtility.PingObject(codeObject); AssetDatabase.OpenAsset(codeObject, int.Parse(line)); return true; } return false; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| public static class HHLogger {
static bool isTrue = false;
public static void DEBUG(params object[] arrParams) { if (isTrue) return; object msg; parseParams(out msg, arrParams); Debug.Log(msg); }
public static void WARN(params object[] arrParams) { if (isTrue) return; object msg; parseParams(out msg, arrParams); Debug.LogWarning(msg); }
public static void ERROR(params object[] arrParams) { if (isTrue) return; object msg; parseParams(out msg, arrParams); Debug.LogError(msg); }
public static void parseParams(out object msg, params object[] arrParams) { msg = string.Empty; for (int i = 0; i < arrParams.Length; i++) { msg += "[" + arrParams[i] + "]"; } } }
|
解决在Unity中封装Debug.Log后代码行定位问题
https://blog.csdn.net/sinat_34870723/article/details/84979106