70 lines
No EOL
1.9 KiB
C#
70 lines
No EOL
1.9 KiB
C#
using System;
|
||
using System.Text;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class LogViewer : MonoBehaviour
|
||
{
|
||
[Header("UI Ayarları")] [SerializeField]
|
||
private TextMeshProUGUI logText; // Text veya TextMeshProUGUI olabilir
|
||
|
||
[SerializeField]
|
||
private ScrollRect scrollRect;
|
||
|
||
[SerializeField]
|
||
private Button clearButton;
|
||
|
||
|
||
[Header("Filtrelenecek Class İsimleri")] [SerializeField]
|
||
private string[] targetClassNames = { "AnalyticsSampleUI", "AnalyticsService", "AnalyticsService" };
|
||
|
||
private readonly StringBuilder logBuilder = new();
|
||
|
||
private void OnEnable()
|
||
{
|
||
Application.logMessageReceived += OnLogReceived;
|
||
clearButton.onClick.AddListener(ClearLog);
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
clearButton.onClick.RemoveAllListeners();
|
||
Application.logMessageReceived -= OnLogReceived;
|
||
}
|
||
|
||
private void ClearLog()
|
||
{
|
||
logBuilder.Clear();
|
||
logText.text = string.Empty;
|
||
}
|
||
|
||
private void OnLogReceived(string logString, string stackTrace, LogType type)
|
||
{
|
||
// Eğer stack trace içinde hedef classlardan biri varsa ekrana yaz
|
||
foreach (var className in targetClassNames)
|
||
if (stackTrace.Contains(className))
|
||
{
|
||
AddLog(logString, type);
|
||
break;
|
||
}
|
||
}
|
||
|
||
private void AddLog(string message, LogType type)
|
||
{
|
||
var color = type switch
|
||
{
|
||
LogType.Warning => "yellow",
|
||
LogType.Error => "red",
|
||
_ => "white"
|
||
};
|
||
|
||
logBuilder.AppendLine($"<color={color}>[{DateTime.Now:HH:mm:ss}] {message}</color>");
|
||
logText.text = logBuilder.ToString();
|
||
|
||
// Scroll'u hep en alta indir (yeni logları göstermek için)
|
||
if (scrollRect != null)
|
||
Canvas.ForceUpdateCanvases(); // UI güncellemesi
|
||
scrollRect.verticalNormalizedPosition = 0f;
|
||
}
|
||
} |