console重定向的问题

一般项目都会封装自己的log输出,以便自己能统一对输出log进行处理。但是遇到的问题就是双击过后会调到自己的封装代码中,不会指定到想要的位置。
在网上找有好多的类似的教程,通过生成dll,通过反射,前者更改起来很费事,我就只做了后者。但是在反射的时候遇到
m_CurrentEntriesPtr != NULL && m_IsGettingEntries
原因是指针没有找到,只要在反射的时候顺便取到StartGettingEntriesEndGettingEntries两个方法
,在GetEntryInternal调用的前后调用即可。

protected static EditorWindow loglistviewobj;
private static object listview;
private static object logentryobj;
private static MethodInfo LogEntriesGetEntry;
private static MethodInfo startlogentresget;
private static MethodInfo endlogentresget;
private static FieldInfo entrycontent;
private static FieldInfo rowfieldinfo;
private const string MyCsName = "CExtends";

static bool ishasconsolewindow()
{
    if (listview == null)
    {
        Assembly editorassembly = Assembly.GetAssembly(typeof(EditorWindow));
        Type consolewindowtype = editorassembly.GetType("UnityEditor.ConsoleWindow");
        FieldInfo mConsolewindowFieldinfo =
            consolewindowtype.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic);
        loglistviewobj = mConsolewindowFieldinfo.GetValue(null) as EditorWindow;
        if (loglistviewobj == null)
        {
            listview = null;
            return false;
        }
        FieldInfo mlistviewfieldinfo =
            consolewindowtype.GetField("m_ListView", BindingFlags.Instance | BindingFlags.NonPublic);
        listview = mlistviewfieldinfo.GetValue(loglistviewobj);
        rowfieldinfo = mlistviewfieldinfo.FieldType.GetField("row", BindingFlags.Instance | BindingFlags.Public);
        Type logentriestype = editorassembly.GetType("UnityEditor.LogEntries");
        
        startlogentresget = logentriestype.GetMethod("StartGettingEntries", BindingFlags.Static | BindingFlags.Public);
        endlogentresget = logentriestype.GetMethod("EndGettingEntries", BindingFlags.Static | BindingFlags.Public);
        LogEntriesGetEntry = logentriestype.GetMethod("GetEntryInternal", BindingFlags.Static | BindingFlags.Public);

        Type entryType = editorassembly.GetType("UnityEditor.LogEntry");
        logentryobj = Activator.CreateInstance(entryType);
        entrycontent = entryType.GetField("condition", BindingFlags.Instance | BindingFlags.Public);
    }

    return true;
}

static string getGotoFile(ref int line)
{
    int row = (int) rowfieldinfo.GetValue(listview);
    startlogentresget.Invoke(null, new object[0]);
    LogEntriesGetEntry.Invoke(null, new object[] { row, logentryobj });
    endlogentresget.Invoke(null, new object[0]);
    string condition = entrycontent.GetValue(logentryobj) as string;
    int index = condition.IndexOf(MyCsName, StringComparison.Ordinal);
    if (index < 0)
        return null;
    int lineindex = condition.IndexOf(")", index, StringComparison.Ordinal);
    int lineindex2 = condition.IndexOf(")", lineindex+2, StringComparison.Ordinal);
    condition = condition.Substring(lineindex2 + 2);
    index = condition.IndexOf(".cs:");
    if (index >= 0)
    {
        int endindex = condition.IndexOf(")", index);
        string linstr = condition.Substring(index + 4, endindex- index - 4);
        line = int.Parse(linstr);
        condition = condition.Substring(0, index + 3);
        int gotocsnameindex = condition.LastIndexOf("/");
        return condition.Substring(gotocsnameindex+1);
    }
    return null;
}
private static bool selfopen = false;
[OnOpenAsset(0)]
public static bool openasset(int instanceid, int line)
{
    if (!EditorWindow.focusedWindow.titleContent.text.Equals("Console"))
        return false;
    if (selfopen)
    {
        selfopen = false;
        return false;
    }
    //listview = null;
    if (!ishasconsolewindow())
    {
        return false;
    }

    string filename = getGotoFile(ref line);
    if (filename == null)
    {
        return false;
    }

    if (filename.EndsWith(".cs"))
    {
        string _filename = filename.Substring(0, filename.Length - 3);
        _filename = _filename + " t:MonoScript";
        string[] searchpaths = AssetDatabase.FindAssets(_filename);
        for (int i = 0; i < searchpaths.Length; ++i)
        {
            string path = AssetDatabase.GUIDToAssetPath(searchpaths[i]);
            string csname = path.Substring( path.LastIndexOf("/")+1);
            if (csname.Equals(filename))
            {
                UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(path,typeof(MonoScript));
                selfopen = true;
                AssetDatabase.OpenAsset(obj, line);
                return true;
            }
        }

    }
    return false;

}

注:此文件要放在editor文件夹下不然打包会报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值