Files
test/Assets/Editor/EditorLogTools.cs
T
2025-09-16 15:34:48 +07:00

89 lines
2.9 KiB
C#

// Assets/Editor/EditorLogTools.cs
#if UNITY_EDITOR
using System;
using System.IO;
using System.Threading;
using UnityEditor;
using UnityEngine;
public static class EditorLogTools
{
[MenuItem("Tools/Logs/Delete Editor.log")]
public static void DeleteEditorLogMenu()
{
string path = GetEditorLogPath();
if (string.IsNullOrEmpty(path))
{
EditorUtility.DisplayDialog("Editor.log", "Không xác định được đường dẫn Editor.log trên hệ điều hành này.", "OK");
return;
}
if (!File.Exists(path))
{
EditorUtility.DisplayDialog("Editor.log", $"Không tìm thấy file (có thể đã bị xóa):\n{path}", "OK");
return;
}
try
{
TryDeleteOrTruncate(path);
EditorUtility.DisplayDialog("Editor.log", "Đã xoá/clear Editor.log ✅", "OK");
}
catch (Exception e)
{
Debug.LogError($"[EditorLogTools] Delete failed: {e}");
EditorUtility.DisplayDialog("Editor.log", "Xoá thất bại. Xem Console để biết chi tiết.", "OK");
}
}
// ===== Helpers =====
private static void TryDeleteOrTruncate(string path)
{
// Thử xóa với một vài lần retry (phòng trường hợp bị lock ngắn hạn)
const int retries = 3;
for (int i = 0; i < retries; i++)
{
try
{
File.Delete(path);
return; // xóa OK
}
catch (IOException)
{
// đợi rồi thử lại
Thread.Sleep(80);
}
}
// Nếu vẫn không xóa được (bị lock bởi Editor), ta truncate để file rỗng
using var fs = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
fs.SetLength(0);
fs.Flush(true);
}
private static string GetEditorLogPath()
{
switch (Application.platform)
{
case RuntimePlatform.WindowsEditor:
// C:\Users\<user>\AppData\Local\Unity\Editor\Editor.log
string localApp = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
return Path.Combine(localApp, "Unity", "Editor", "Editor.log");
case RuntimePlatform.OSXEditor:
// ~/Library/Logs/Unity/Editor.log
string home = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
return Path.Combine(home, "Library", "Logs", "Unity", "Editor", "Editor.log");
case RuntimePlatform.LinuxEditor:
// ~/.config/unity3d/Editor.log
string homeLinux = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
return Path.Combine(homeLinux, ".config", "unity3d", "Editor.log");
default:
return null;
}
}
}
#endif