This repository has been archived on 2025-04-28. You can view files and clone it, but cannot push or open issues or pull requests.
ARPlusSystem/ARPlusSystem-250418/Assets/Scripts/Util/UGUILogger.cs

69 lines
1.9 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace Util
{
public class UGUILogger : MonoBehaviour
{
public Text logText;
private readonly StringBuilder logContent = new StringBuilder();
public ScrollRect scrollRect;
public int maxLines = 10000; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private readonly Queue<string> logQueue = new Queue<string>();
public float autoScrollDelay = 10f;
private Coroutine autoScrollCoroutine;
private bool isUserInteracting;
void Start()
{
Application.logMessageReceived += (logString, trace, type) => {
if(logString.Contains("[Info]")) { // ɸѡGameplay<61><79>ǩ
AddLog(logString);
}
};
}
public void ClearLog() {
logContent.Clear();
logText.text = "";
}
public void AddLog(string newLog)
{
logQueue.Enqueue(newLog);
// <20><>̬ƴ<CCAC><C6B4><EFBFBD>ı<EFBFBD>
logText.text = string.Join("\n", logQueue);
// ǿ<><C7BF>ˢ<EFBFBD>²<EFBFBD><C2B2>֣<EFBFBD><D6A3>ؼ<EFBFBD><D8BC><EFBFBD><EFBFBD><EFBFBD>
LayoutRebuilder.ForceRebuildLayoutImmediate(scrollRect.content);
}
void Update()
{
// ǿ<>ƹ<EFBFBD><C6B9><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD>õף<C3B5><30>ײ<EFBFBD><D7B2><EFBFBD><31><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
scrollRect.verticalNormalizedPosition = 0;
if (logQueue.Count > maxLines)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ղ<EFBFBD><D5B2><EFBFBD>
logQueue.Clear();
logText.text = "";
// <20><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
scrollRect.verticalNormalizedPosition = 1;
}
}
}
public enum LogTag
{
Info,
Warning
}
public static class CustomLogger {
public static void LogWithTag(string message, LogTag tag) {
Debug.Log($"[{tag}][{DateTime.Now:yyyy-MM-dd HH:mm:ss}]{message}");
}
}
}