Files
ichni_Creator_Studio/Assets/Scripts/DynamicUI/MainUI/LogWindow/LogWindow.cs
2025-08-31 15:27:02 +08:00

95 lines
3.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
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 List<LogText> logTexts; // 改为 List
public int logTextCapacity = 4;
protected override void Start()
{
base.Start();
savedTexts = new List<string>();
logTexts = new List<LogText>(); // 初始化为 List
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)
{
LogText logText = LeanPool.Spawn(logTextPrefab, textRect).GetComponent<LogText>();
if (color == default) color = Color.white;
savedTexts.Add(text);
logText.SetLogText(text, color);
// 插入到头部
logTexts.Insert(0, logText);
// 超出容量则移除最后一个
if (logTexts.Count > logTextCapacity)
{
LogText logText1 = logTexts[logTexts.Count - 1];
RectTransform rt = logText1.GetComponent<RectTransform>();
rt.DOComplete();
rt.DOAnchorPos(new Vector2(0, -23 * (logTexts.Count - 1)), 0.2f).SetEase(Ease.OutCubic);
logText1.logText.DOColor(new Color(0, 0, 0, 0), 0.2f).OnComplete(() =>
{
LeanPool.Despawn(logText1.gameObject);
});
logTexts.RemoveAt(logTexts.Count - 1);
}
logText.logText.color = new Color(0, 0, 0, 0);
logText.logText.DOColor(color, 0.2f);
// 更新所有 log 的位置
for (int i = 0; i < logTexts.Count; i++)
{
RectTransform rt = logTexts[i].GetComponent<RectTransform>();
rt.DOComplete();
rt.DOAnchorPos(new Vector2(0, -23 * i), 0.2f).SetEase(Ease.OutCubic);
}
}
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();
}
}
}