美文网首页
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编辑

    用途 关联Project窗口lua脚本文件 关联Console窗口lua输出的日志 方案 监听打开资源事件Unit...

  • Ubuntu常用软件

    Unity-Tweak sudo apt-get install unity-tweak-tool 编辑器vim ...

  • 学习常用链接

    //Lua Lua table详解 Lua 元表详解 云风博客 //Unity Unity知识点 栈和队列 Uni...

  • lua文档

    http://tool.oschina.net/apidocs/apidoc?api=lua

  • ubuntu实用软件

    1 unity-tweak-tool unity-tweak-tool是一个很好用的系统设置软件。 安装方法:su...

  • lua中c#变量传入和lua之间的回调

    C# Lua lua_Tool uiset 注意的是在DoString中使用require 'uiset' 相当于...

  • Ubuntu仿Mac界面美化

    完成以下步骤后界面如下图所示 安装unity-tweak-tool unity-tweak-tool是一款实用的u...

  • Ubuntu

    1.修改桌边工具 sudo apt-get install unity-tweak-tool unity-twea...

  • lua的string.format格式化多参数问题

    测试地址:https://www.nhooo.com/tool/lua/[https://www.nhooo.co...

  • Lua math(三) tool

    前言# 今天这个系列的函数我将其命名为工具,之所以这么命名是因为这个系列函数没有复杂的公式,仅仅是取数字的一部分,...

网友评论

      本文标题:Unity Tool - Lua编辑

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