美文网首页
Unity Tool - Lua编辑

Unity Tool - Lua编辑

作者: 姚宏民 | 来源:发表于2018-03-22 00:20 被阅读0次

    用途

    • 关联Project窗口lua脚本文件
    • 关联Console窗口lua输出的日志

    方案

    • 监听打开资源事件
      Unity3D提供了监听文件打开事件函数属性 Unity OnOpenAssetAttribute
      我们只需要从中判断出这是一个lua相关的文件或者日志信息即可
    [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'
        ...
    
    • TODO 参考Unity文件管理,在Preference窗口定义文件关联应用

    相关文章

      网友评论

          本文标题:Unity Tool - Lua编辑

          本文链接:https://www.haomeiwen.com/subject/zycoqftx.html