/
GeekNerd
/
ServerModelingProject
Обзор
Документация
Войти
/
GeekNerd
/
ServerModelingProject
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Assets/Editor/ConsoleLogExporter.cs
107 строк
4 KB
Gorney-Alex
Init
30 май 2026, 19:05
30 май 2026, 19:05
3cdcc49
Код
Авторство
О чём код?
using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text; using UnityEditor; using UnityEngine; public static class ConsoleLogExporter { private const string MenuPath = "Tools/Export Console Logs"; [MenuItem(MenuPath)] public static void ExportConsoleLogs() { string defaultName = $"console_logs_{DateTime.Now:yyyyMMdd_HHmmss}.txt"; string path = EditorUtility.SaveFilePanel("Export Console Logs", Application.dataPath, defaultName, "txt"); if (string.IsNullOrEmpty(path)) { return; } List<string> messages = GetConsoleMessages(); File.WriteAllLines(path, messages, new UTF8Encoding(false)); EditorUtility.RevealInFinder(path); Debug.Log($"[ConsoleLogExporter] Exported {messages.Count} entries to {path}"); } private static List<string> GetConsoleMessages() { var messages = new List<string>(); Type logEntriesType = Type.GetType("UnityEditor.LogEntries,UnityEditor.dll") ?? Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll"); Type logEntryType = Type.GetType("UnityEditor.LogEntry,UnityEditor.dll") ?? Type.GetType("UnityEditorInternal.LogEntry,UnityEditor.dll"); if (logEntriesType == null || logEntryType == null) { Debug.LogWarning("[ConsoleLogExporter] Unable to access Unity console types."); return messages; } const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; MethodInfo getCount = logEntriesType.GetMethod("GetCount", flags); MethodInfo getEntry = logEntriesType.GetMethod("GetEntryInternal", flags) ?? logEntriesType.GetMethod("GetEntry", flags); MethodInfo startGettingEntries = logEntriesType.GetMethod("StartGettingEntries", flags); MethodInfo endGettingEntries = logEntriesType.GetMethod("EndGettingEntries", flags); FieldInfo conditionField = logEntryType.GetField("condition", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? logEntryType.GetField("message", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (getCount == null || getEntry == null || conditionField == null) { Debug.LogWarning("[ConsoleLogExporter] Console API signature not found for this Unity version."); return messages; } int count = (int)getCount.Invoke(null, null); object entry = Activator.CreateInstance(logEntryType); startGettingEntries?.Invoke(null, null); try { for (int i = 0; i < count; i++) { getEntry.Invoke(null, new object[] { i, entry }); string message = conditionField.GetValue(entry)?.ToString() ?? string.Empty; message = FirstLineOnly(message); message = TrimAfterZenject(message); message = message.Trim(); if (!string.IsNullOrEmpty(message)) messages.Add(message); } } finally { endGettingEntries?.Invoke(null, null); } return messages; } private static string TrimAfterZenject(string message) { if (string.IsNullOrEmpty(message)) { return message; } int index = message.IndexOf("Zenject", StringComparison.Ordinal); if (index < 0) { return message; } return message.Substring(0, index + "Zenject".Length); } private static string FirstLineOnly(string message) { if (string.IsNullOrEmpty(message)) return message; int idx = message.IndexOfAny(new[] { '\r', '\n' }); return idx < 0 ? message : message.Substring(0, idx); } }