Files
ichni_Creator_Studio/Assets/Scripts/DynamicUI/MainUI/LogWindow/LogWindow.cs
2025-07-12 18:27:10 +08:00

77 lines
2.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Lean.Pool;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
namespace Ichni.Editor
{
public class LogWindow : StaticWindow
{
public GameObject logTextPrefab;
List<string> savedTexts;
public RectTransform textRect;
public Button copyAllTextsButton;
public Button removeAllTextsButton;
public Queue<LogText> logTexts;
public int logTextCapacity = 4;
protected override void Start()
{
base.Start();
savedTexts = new List<string>();
logTexts = new Queue<LogText>();
copyAllTextsButton.onClick.AddListener(CopyAllText);
removeAllTextsButton.onClick.AddListener(RemoveAllText);
}
public static void Log(string text, Color color = default)
{
EditorManager.instance.uiManager.mainPage.logWindow.AddLog(text, color);
}
private void AddLog(string text, Color color = default)
{
CheckLogTextCapacity();
LogText logText = LeanPool.Spawn(logTextPrefab, textRect).GetComponent<LogText>();
if (color == default) color = Color.white;
savedTexts.Add(text);
logText.SetLogText(text, color);
logTexts.Enqueue(logText);
}
private void CheckLogTextCapacity()
{
if (logTexts.Count >= logTextCapacity)
{
LeanPool.Despawn(logTexts.Dequeue().gameObject);
}
}
private void CopyAllText()
{
string allText = "";
foreach (string text in savedTexts)
{
allText += text + "\n";
}
GUIUtility.systemCopyBuffer = allText;
}
private void RemoveAllText()
{
foreach (LogText logText in logTexts)
{
LeanPool.Despawn(logText.gameObject);
}
logTexts.Clear();
savedTexts.Clear();
}
}
}