• Unity Log重新定向


    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+)) ? ");

    /// <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文件导入。

  • 相关阅读:
    运行.bat批处理,CMD窗口隐藏,并制作为EXE文件
    TinyXML:一个优秀的C++ XML解析器(转载)
    2013编程之美资格赛【传话游戏】
    linux GTK教程(消息机制/标签/按钮/图像/文本/对话框/菜单/容器)
    c++强制类型转换(总结)
    string与char*的转换(转载)
    网络数据包捕获函数库Libpcap安装与使用(非常强大)
    Linux 高级Socket编程
    linux GTK 安装
    .dll和.lib文件的生成和使用 c++
  • 原文地址:https://www.cnblogs.com/kanekiken/p/10545363.html
Copyright © 2020-2023  润新知