Unity Tool - Lua编辑

2018-03-22  本文已影响0人  姚宏民

用途


方案

[OnOpenAsset(0)]
private static bool OnOpenAssetLog(int instanceID, int line)
{
    // 点击Project asset,判断文件后缀即可。
    if (line == -1)
    {
        var path = AssetDatabase.GetAssetPath(instanceID);
        if (path.EndsWith(".lua", System.StringComparison.OrdinalIgnoreCase))
            return TextEditorTool.OpenText(path, 0, 0);
    }

    // 点击日志,判断日志内容,分析出lua文件路径和行号
    var log = ConsoleWindowSelectedLog;
    var match = Regex.Match(log, @"\[(.*.lua):(.*?)\]:");
    if (match.Success && match.Groups.Count > 2)
    {
        var matchPath = Path.Combine("Assets/Game/Lua", match.Groups[1].Value);
        var matchLine = int.Parse(match.Groups[2].Value);

        TextEditorTool.OpenText(matchPath, matchLine, 0);
    }

    // 不做任何处理
    return false;
}

static string ConsoleWindowSelectedLog
{
    get
    {
        // 获取日志窗口
        var editorWindowAssembly = Assembly.GetAssembly(typeof(UnityEditor.EditorWindow));
        if (editorWindowAssembly == null)
            return null;

        var consoleWindowType = editorWindowAssembly.GetType("UnityEditor.ConsoleWindow");
        if (consoleWindowType == null)
            return null;

        var consoleWindowField = consoleWindowType.GetField("ms_ConsoleWindow",
            System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
        if (consoleWindowField == null)
            return null;

        var consoleWindowInst = consoleWindowField.GetValue(null);
        if (consoleWindowInst == null)
            return null;

        // 日志窗口处于选中状态,返回日志内容
        if ((object)UnityEditor.EditorWindow.focusedWindow == consoleWindowInst)
        {
            var listViewState = editorWindowAssembly.GetType("UnityEditor.ListViewState");
            if (listViewState == null)
                return null;

            var listViewField = consoleWindowType.GetField("m_ListView",
                System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            if (listViewField == null)
                return null;

            var consoleWindowListView = listViewField.GetValue(consoleWindowInst);
            if (consoleWindowListView == null)
                return null;

            var activeText = consoleWindowType.GetField("m_ActiveText",
                System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            if (activeText == null)
                return null;

            return activeText.GetValue(consoleWindowInst).ToString();
        }

        return null;
    }
}
21:01:30.318-61: [gamenet/gamenet.lua:195]:DisconnectLoginServer!
stack traceback:
    [C]: in function 'print_log'
    ...
上一篇下一篇

猜你喜欢

热点阅读