首页 > 技术文章 > Unity Log重新定向

kanekiken 2019-03-17 08:58 原文

Unity Log重新定向

使用Unity的Log的时候有时候需要封装一下Debug.Log(message),可以屏蔽Log或者把log内容写到文本中。通过把文本内容传送到服务器中,查找bug出现的原因。但是封装之后的日志系统如果双击跳转的时候,会跳转到自定义的日志系统脚本里面去,有些不太方便。

1、通过反射修改找到日志打印的具体位置

  1. 通过反射知道点击的日志的文本内容
  2. 通过正则表达式去匹配找到打印日志的脚本和具体的行号,如果是封装的脚本的话,继续匹配,直到结束。
public static class LogRedirect
{
private static readonly Regex LogRegex = new Regex(@" \(at (.+)\:(\d+)\)\r?\n");

/// <summary>
/// 用于在Unity中打开资产的回调属性(例如,在项目浏览器中双击资源时会触发回调)。
///将此属性添加到静态方法将使Unity在打开资产时调用该方法。该方法应具有以下签名:
///static bool OnOpenAsset(int instanceID, int line)
/// 如果处理资产的开放则返回true;如果外部工具应打开它,则返回false。
/// </summary>
/// <param name="instanceId"></param>
/// <param name="line"></param>
/// <returns></returns>
[OnOpenAssetAttribute(0)]
private static bool OnOpenAsset(int instanceId, int line)
{
string name = EditorUtility.InstanceIDToObject(instanceId).name;

string msg = GetSelectedStackTrace();
Tools.FileHelper.CreateFile(Path.Combine(Application.dataPath, "ADebug/LogFile"), msg);
if (string.IsNullOrEmpty(msg)) return false;
if (!msg.Contains("Debugger.cs")) return false;
Match match = LogRegex.Match(msg);
if (!match.Success) return false;

match = match.NextMatch();
if (!match.Success) return false;

InternalEditorUtility.OpenFileAtLineExternal(
Path.Combine(Application.dataPath, match.Groups[1].Value.Substring(7)), int.Parse(match.Groups[2].Value));
return true;
}


/// <summary>
/// 获取点击Log的文本信息
/// </summary>
/// <returns></returns>
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);
}
}

2、把自定义的日志脚本打包成dll文件导入。

推荐阅读