一般项目都会封装自己的log输出,以便自己能统一对输出log进行处理。但是遇到的问题就是双击过后会调到自己的封装代码中,不会指定到想要的位置。
在网上找有好多的类似的教程,通过生成dll,通过反射,前者更改起来很费事,我就只做了后者。但是在反射的时候遇到
m_CurrentEntriesPtr != NULL && m_IsGettingEntries
原因是指针没有找到,只要在反射的时候顺便取到StartGettingEntries和EndGettingEntries两个方法
,在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文件夹下不然打包会报错

1151

被折叠的 条评论
为什么被折叠?



