Unity导出工作台(Console)数据

首先在Unity中添加C#脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;
using System.IO;

public class Print : EditorWindow
{
    static Type _LogEntriesType;
    static Type _logEntryType;

    static MethodInfo _GetCountMethod;
    static MethodInfo _StartGettingEntriesMethod;
    static MethodInfo _GetEntryInternalMethod;
    static MethodInfo _EndGettingEntriesMethod;

    static FieldInfo _conditionField;

    static bool _isSupport = false;

    bool _detail = false;

    [MenuItem("Debug/Export Console")]
    static void ShowEditor()
    {
        Print editor = EditorWindow.GetWindowWithRect<Print>(new Rect(-1, -1, 170, 60), true, "Export Console", true);
        editor.Show();
    }

    [MenuItem("Debug/Export Console", validate = true)]
    static bool ExportConsoleMenuValidate()
    {
        return _isSupport && GetEntryCount() > 0;
    }

    private void OnGUI()
    {
        _detail = EditorGUILayout.Toggle("Detail", _detail);
        GUILayout.FlexibleSpace();
        if (GUILayout.Button("Export"))
        {
            if (DoExportConsole(_detail))
            {
                Close();
            }
        }
        GUILayout.Space(5);
    }


    static bool DoExportConsole(bool detail)
    {
        string[] logs = GetConsoleEntries();
        string path = EditorUtility.SaveFilePanel("Export Console", Application.dataPath, "ConsoleLog", "txt");
        if (string.IsNullOrEmpty(path))
        {
            return false;
        }

        if (!detail)
        {
            for (int i = 0; i < logs.Length; ++i)
            {
                using (var sr = new StringReader(logs[i]))
                {
                    logs[i] = sr.ReadLine();
                }
            }
        }
        File.WriteAllLines(path, logs);
        EditorUtility.OpenWithDefaultApp(path);
        return true;
    }

    static Print()
    {
        _LogEntriesType = Type.GetType("UnityEditor.LogEntries,UnityEditor");
        if (_LogEntriesType != null)
        {
            _GetCountMethod = _LogEntriesType.GetMethod("GetCount", BindingFlags.Static | BindingFlags.Public);
            _StartGettingEntriesMethod = _LogEntriesType.GetMethod("StartGettingEntries", BindingFlags.Static | BindingFlags.Public);
            _GetEntryInternalMethod = _LogEntriesType.GetMethod("GetEntryInternal", BindingFlags.Static | BindingFlags.Public);
            _EndGettingEntriesMethod = _LogEntriesType.GetMethod("EndGettingEntries", BindingFlags.Static | BindingFlags.Public);
        }

        _logEntryType = Type.GetType("UnityEditor.LogEntry,UnityEditor");
        if (_logEntryType != null)
        {
            _conditionField = _logEntryType.GetField("message", BindingFlags.Public | BindingFlags.Instance);
        }
        CheckSupport();
    }

    static void CheckSupport()
    {
        if (_LogEntriesType == null ||
            _logEntryType == null ||
            _GetCountMethod == null ||
            _StartGettingEntriesMethod == null ||
            _GetEntryInternalMethod == null ||
            _EndGettingEntriesMethod == null ||
            _conditionField == null)
        {
            _isSupport = false;
        }
        else
        {
            _isSupport = true;
        }
    }

    static string[] GetConsoleEntries()
    {
        if (!_isSupport)
        {
            return null;
        }
        List<string> entries = new List<string>();

        object countObj = _GetCountMethod.Invoke(null, null);

        _StartGettingEntriesMethod.Invoke(null, null);

        int count = int.Parse(countObj.ToString());
        for (int i = 0; i < count; ++i)
        {
            object logEntry = Activator.CreateInstance(_logEntryType);
            object result = _GetEntryInternalMethod.Invoke(null, new object[] { i, logEntry });
            if (bool.Parse(result.ToString()))
            {
                entries.Add(_conditionField.GetValue(logEntry).ToString());
            }
        }
        _EndGettingEntriesMethod.Invoke(null, null);
        return entries.ToArray();
    }

    static int GetEntryCount()
    {
        if (!_isSupport)
        {
            return 0;
        }
        object countObj = _GetCountMethod.Invoke(null, null);
        return int.Parse(countObj.ToString());
    }
}

运行程序

在菜单栏出现

点击 “Export Console”

出现该窗口 

勾选 “Detail”,然后点击 “Export”选项

选择保存位置 

生成的.txt文件中会有工作台的所有内容,如

如果不勾选  “Detail” ,生成的.txt文件中只有打印的内容,如

 导出为Excel文件,我目前没找到直接一些的方法

我的做法是:在不勾选“Detail”时导出的文件,直接复制进Excel中

然后根据需要进行筛选数据

我导出的是坐标,直接复制进Excel中,会自动根据逗号进行数据拆分

小编目前是在校生,还在学习阶段,各方面还有很多不足

如有其他更好的方法,欢迎指出

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值