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

Unity Log重新定向的更多相关文章

  1. Unity Log Path

    { //不是开场动画的LOG,是APK的 C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Apk\res\mipmap ...

  2. 定向转发和重定向实现 <select >下拉表单数据传送

    定向转发的特点:   (1). 实行转发时浏览器上的网址不变  (如果你这点忽视了,那你就要接受我无尽的鄙视吧! 哇咔咔~~~)    (2). 实行转发时 :   只有一次请求.  不信,看这下面的 ...

  3. 【Redis】简介与安装

    Linux 安装 [root@redis ~]# wget http://download.redis.io/releases/redis-2.8.19.tar.gz 解压缩redis[root@ha ...

  4. Centos6 安装 Redis

    先确认gcc和tcl已经安装 sudo yum install gcc-c++ sudo yum install tcl 解压, 编译和安装 .tar.gz /usr/src/ cd /usr/src ...

  5. [转载] Redis 起步

    转载地址:http://www.cnblogs.com/shanyou/archive/2012/01/28/2330451.html Rdis和JQuery一样是纯粹为应用而产生的,这里记录的是在C ...

  6. Redis 起步

    Rdis和JQuery一样是纯粹为应用而产生的,这里记录的是在CentOS 5.7上学习入门文章: 1.Redis简介  Redis是一个key-value存储系统.和Memcached类似,但是解决 ...

  7. windows下和linux下 Redis 安装

    Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...

  8. Redis起步

    Rdis和JQuery一样是纯粹为应用而产生的,这里记录的是在CentOS 5.7上学习入门文章: 1.Redis简介  Redis是一个key-value存储系统.和Memcached类似,但是解决 ...

  9. 如何在.Net中使用Redis

    Redis是一个key-value存储系统.和Memcached类似,但是解决了断电后数据完全丢失的情况,而且她支持更多无化的value类型,除了和string外,还支持lists(链表).sets( ...

随机推荐

  1. AtCoder Beginner Contest 106 2018/08/18

    A - Garden Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement There is a ...

  2. 【C】字符串常量和字符数组

    此次博客是转载某位博主的文章,不过现在找不到了,所以先声明一下. 先贴一段代码: #include <stdio.h> int main(int argc, const char** ar ...

  3. bzoj 1731 [Usaco2005 dec]Layout 排队布局——差分约束

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 对差分约束理解更深.还发现美妙博客:http://www.cppblog.com/me ...

  4. 隐藏时间表ribbon按钮

    Ribbon.ContextualTabs.Timesheet.Home.Share;Ribbon.ContextualTabs.Timesheet.Home.ShowHide;Ribbon.Cont ...

  5. WPF中数据绑定问题

    在数据库中字段不区分大小写,可以页面是区分的,一开始以为不区分,可我从数据库查出了数据在前台就是不显示想了半天才发现的. <sdk:DataGrid FrozenColumnCount =&qu ...

  6. 3. 关于sql注入的综合题

    关于sql注入的综合题                          ----------南京邮电大学ctf : http://cms.nuptzj.cn/ 页面上也给了好多信息: 根据这个sm. ...

  7. GPLT天梯赛 L2-022. 重排链表

    L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...

  8. Oracle SQL调优之分区表

    目录 一.分区表简介 二.分区表优势 三.分区表分类 3.1 范围分区 3.2 列表分区 3.3 散列分区 3.4 组合分区 四.分区相关操作 五.分区相关查询 附录:分区表索引失效的操作 一.分区表 ...

  9. 938. Range Sum of BST

    Given the root node of a binary search tree, return the sum of values of all nodes with value betwee ...

  10. springboot 启动

    1. 新建一个java 类,名为Application,代码内容: @ServletComponentScan@SpringBootApplicationpublic class Applicatio ...