Playvoi/Playvoi.Client/Assets/ShiroginSDK/Samples/AnalyticsSample/Scripts/LogViewer.cs

70 lines
No EOL
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}