161 lines
5.0 KiB
C#
161 lines
5.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Northbound
|
|
{
|
|
public class DebugLogUI : MonoBehaviour
|
|
{
|
|
[Header("Settings")]
|
|
public int maxLogMessages = 50;
|
|
public float logDisplayDuration = 5f;
|
|
public KeyCode toggleKey = KeyCode.BackQuote;
|
|
|
|
[Header("UI References")]
|
|
public GameObject logPanel;
|
|
public Text logText;
|
|
public GameObject scrollContent;
|
|
public ScrollRect scrollRect;
|
|
|
|
private Queue<string> _logMessages = new Queue<string>();
|
|
private bool _isVisible = true;
|
|
|
|
private void Start()
|
|
{
|
|
// 이미 씬에 있는지 확인
|
|
if (logPanel != null)
|
|
{
|
|
logPanel = CreateLogPanel();
|
|
_isVisible = true;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(toggleKey))
|
|
{
|
|
ToggleLogPanel();
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (logPanel != null)
|
|
{
|
|
Application.logMessageReceived += HandleLog;
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (logPanel != null)
|
|
{
|
|
Application.logMessageReceived -= HandleLog;
|
|
}
|
|
}
|
|
|
|
private GameObject CreateLogPanel()
|
|
{
|
|
GameObject panel = new GameObject("DebugLogPanel");
|
|
panel.AddComponent<Canvas>();
|
|
Canvas canvas = panel.GetComponent<Canvas>();
|
|
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
|
canvas.sortingOrder = 9999;
|
|
|
|
RectTransform canvasRect = canvas.GetComponent<RectTransform>();
|
|
canvasRect.anchorMin = Vector2.zero;
|
|
canvasRect.anchorMax = new Vector2(Screen.width, Screen.height);
|
|
canvasRect.sizeDelta = Vector2.zero;
|
|
canvasRect.localScale = Vector3.one;
|
|
|
|
// 배경
|
|
GameObject background = new GameObject("Background");
|
|
background.transform.SetParent(canvas.transform, false);
|
|
Image bgImage = background.AddComponent<Image>();
|
|
bgImage.color = new Color(0, 0, 0, 0.8f);
|
|
|
|
RectTransform bgRect = background.AddComponent<RectTransform>();
|
|
bgRect.anchorMin = Vector2.zero;
|
|
bgRect.anchorMax = Vector2.one;
|
|
bgRect.sizeDelta = new Vector2(600, 300);
|
|
bgRect.localPosition = new Vector2(-300, -150);
|
|
|
|
// 로그 내용
|
|
GameObject content = new GameObject("Content");
|
|
content.transform.SetParent(background.transform, false);
|
|
|
|
RectTransform contentRect = content.AddComponent<RectTransform>();
|
|
contentRect.anchorMin = Vector2.zero;
|
|
contentRect.anchorMax = Vector2.one;
|
|
contentRect.sizeDelta = new Vector2(580, 280);
|
|
contentRect.localPosition = new Vector2(-290, -140);
|
|
|
|
// 텍스트
|
|
Text logTextComponent = content.AddComponent<Text>();
|
|
logTextComponent.fontSize = 14;
|
|
logTextComponent.alignment = TextAnchor.UpperLeft;
|
|
logTextComponent.color = Color.white;
|
|
|
|
// 스크롤뷰
|
|
GameObject scrollView = new GameObject("ScrollView");
|
|
scrollView.transform.SetParent(content.transform, false);
|
|
scrollRect = scrollView.AddComponent<ScrollRect>();
|
|
scrollRect.content = contentRect;
|
|
scrollRect.viewport = new RectTransform();
|
|
scrollRect.horizontal = false;
|
|
scrollRect.vertical = true;
|
|
scrollRect.scrollSensitivity = 1;
|
|
|
|
return panel;
|
|
}
|
|
|
|
private void HandleLog(string logString, string stackTrace, LogType type)
|
|
{
|
|
string timestamp = System.DateTime.Now.ToString("HH:mm:ss");
|
|
string logMessage = $"[{timestamp}] {logString}";
|
|
|
|
_logMessages.Enqueue(logMessage);
|
|
if (_logMessages.Count > maxLogMessages)
|
|
{
|
|
_logMessages.Dequeue();
|
|
}
|
|
|
|
UpdateLogText();
|
|
}
|
|
|
|
private void UpdateLogText()
|
|
{
|
|
if (logText == null) return;
|
|
|
|
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
|
foreach (string msg in _logMessages)
|
|
{
|
|
sb.AppendLine(msg);
|
|
}
|
|
|
|
logText.text = sb.ToString();
|
|
}
|
|
|
|
public void ToggleLogPanel()
|
|
{
|
|
if (logPanel != null)
|
|
{
|
|
_isVisible = !_isVisible;
|
|
logPanel.SetActive(_isVisible);
|
|
}
|
|
}
|
|
|
|
public void Log(string message)
|
|
{
|
|
if (logPanel != null)
|
|
{
|
|
logPanel.SetActive(true);
|
|
_isVisible = true;
|
|
}
|
|
|
|
Debug.Log($"[DebugLogUI] {message}");
|
|
Debug.Log($"[DebugLogUI] 로그 패널 활성됨: {message}");
|
|
}
|
|
}
|
|
}
|