首先在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中,会自动根据逗号进行数据拆分
小编目前是在校生,还在学习阶段,各方面还有很多不足文章来源:https://www.toymoban.com/news/detail-404298.html
如有其他更好的方法,欢迎指出文章来源地址https://www.toymoban.com/news/detail-404298.html
到了这里,关于Unity导出工作台(Console)数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!