using System; using System.Collections; using System.Collections.Generic; using DG.Tweening; using Lean.Pool; using TMPro; using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UI; namespace Ichni.Editor { public class LogWindow : StaticWindow { public GameObject logTextPrefab; List savedTexts; public RectTransform textRect; public Button copyAllTextsButton; public Button removeAllTextsButton; public List logTexts; // 改为 List public int logTextCapacity = 4; protected override void Start() { base.Start(); savedTexts = new List(); logTexts = new List(); // 初始化为 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); } public static TMP_Text LogText(string text, Color color = default) { LogWindow logWindow = EditorManager.instance.uiManager.mainPage.logWindow; logWindow.AddLog(text, color); return logWindow.logTexts[0].logText; } private void AddLog(string text, Color color = default) { LogText logText = LeanPool.Spawn(logTextPrefab, textRect).GetComponent(); 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(); 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(); 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(); } } }