209 lines
6.4 KiB
C#
209 lines
6.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// 控制台面板
|
|
/// </summary>
|
|
public class ConsolePanel : PanelBasic, IPool<ConsoleItem>
|
|
{
|
|
#region ConsoleItem对象池管理
|
|
|
|
public event Action<ConsoleItem> OnRecycle;
|
|
|
|
public event Action<ConsoleItem> OnGet;
|
|
|
|
/// <summary>
|
|
/// 预制体
|
|
/// </summary>
|
|
public ConsoleItem prefab;
|
|
/// <summary>
|
|
/// 对象池最大容量
|
|
/// </summary>
|
|
public int maximum_size = 100;
|
|
|
|
/// <summary>
|
|
/// 对象池
|
|
/// </summary>
|
|
private List<IPoolObject<ConsoleItem>> console_item_pool = new List<IPoolObject<ConsoleItem>>();
|
|
|
|
public RectTransform pool_root;
|
|
|
|
public IPoolObject<ConsoleItem> Get()
|
|
{
|
|
IPoolObject<ConsoleItem> _pool_object = null;
|
|
//上限100个
|
|
if (console_items.Count >= 100)
|
|
{
|
|
_pool_object = console_item_content.transform.GetComponentInChildren<ConsoleItem>();
|
|
}
|
|
else
|
|
{
|
|
if (console_item_pool.Count == 0)
|
|
{
|
|
if (prefab == null)
|
|
{
|
|
prefab = Resources.Load<ConsoleItem>("Prefabs/UIItem/ConsoleItem");
|
|
}
|
|
|
|
var content = GameObject.Instantiate(prefab);
|
|
content.Init(this, content);
|
|
_pool_object = content;
|
|
}
|
|
else
|
|
{
|
|
_pool_object = console_item_pool[console_item_pool.Count - 1];
|
|
console_item_pool.Remove(_pool_object);
|
|
|
|
_pool_object.Content.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
|
|
OnGet?.Invoke(_pool_object.Content);
|
|
return _pool_object;
|
|
}
|
|
|
|
public void Recycle(IPoolObject<ConsoleItem> _pool_object)
|
|
{
|
|
if ((ConsolePanel)_pool_object.Pool != this) return;
|
|
|
|
OnRecycle?.Invoke(_pool_object.Content);
|
|
|
|
if (console_item_pool.Count > maximum_size)
|
|
{
|
|
_pool_object.Dispose();
|
|
}
|
|
else
|
|
{
|
|
console_item_pool.Add(_pool_object);
|
|
_pool_object.Content.transform.SetParent(pool_root.transform);
|
|
_pool_object.Content.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
public void Dispose(IPoolObject<ConsoleItem> _pool_object)
|
|
{
|
|
if ((ConsolePanel)_pool_object.Pool != this) return;
|
|
|
|
GameObject.DestroyImmediate(_pool_object.Content.gameObject);
|
|
|
|
if (console_item_pool.Contains(_pool_object))
|
|
console_item_pool.Remove(_pool_object);
|
|
}
|
|
#endregion
|
|
|
|
public static ConsolePanel Instance;
|
|
/// <summary>
|
|
/// 实例化对象容器
|
|
/// </summary>
|
|
public RectTransform console_item_content;
|
|
/// <summary>
|
|
/// 清空控制台按钮
|
|
/// </summary>
|
|
public Button clear_console_button;
|
|
/// <summary>
|
|
/// 实例化对象列表
|
|
/// </summary>
|
|
public List<ConsoleItem> console_items = new List<ConsoleItem>();
|
|
/// <summary>
|
|
/// 自定义ScrollRect
|
|
/// </summary>
|
|
public CustomScrollRect custom_scroll_rect;
|
|
private int _current_console_count;
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
Instance = this;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
clear_console_button.onClick.AddListener(OnClearConsole);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空
|
|
/// </summary>
|
|
public void OnClearConsole()
|
|
{
|
|
console_items.ForEach(x => x.Recycle());
|
|
console_items.Clear();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_current_console_count != console_items.Count)
|
|
{
|
|
_current_console_count = console_items.Count;
|
|
if (custom_scroll_rect.is_Draging || custom_scroll_rect.is_Scrolling)
|
|
return;
|
|
if (console_item_content.sizeDelta.y > console_item_content.anchoredPosition.y)
|
|
console_item_content.anchoredPosition = new Vector2(console_item_content.anchoredPosition.x, console_item_content.sizeDelta.y);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 控制台输出
|
|
/// </summary>
|
|
/// <param name="_message"></param>
|
|
/// <param name="_type">_type = [log,warning,error]</param>
|
|
public static ConsoleItem ConsoleOutput(string _message, string _type = "log")
|
|
{
|
|
//bool auto_move = Mathf.Abs(current_console_content_position_y - Instance.console_item_content.rect.height) < 10
|
|
// || Instance.console_item_content.rect.height < current_console_content_position_y;
|
|
|
|
string __msg = string.Format("[{0}] ", DateTime.Now.ToString("HH:mm:ss"));
|
|
switch (_type)
|
|
{
|
|
case "log":
|
|
_message = string.Format("<color=#FFFFFF>{0}</color>", _message);
|
|
break;
|
|
case "warning":
|
|
_message = string.Format("<color=#FFFF00>{0}</color>", _message);
|
|
break;
|
|
case "error":
|
|
_message = string.Format("<color=#FF0000>{0}</color>", _message);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
__msg = string.Format("{0}{1}", __msg, _message);
|
|
var _console_item = Instance.Get().Content.NewConsole(__msg);
|
|
_console_item.transform.SetParent(Instance.console_item_content);
|
|
_console_item.transform.SetAsLastSibling();
|
|
if (!Instance.console_items.Contains(_console_item))
|
|
{
|
|
Instance.console_items.Add(_console_item);
|
|
}
|
|
|
|
Canvas.ForceUpdateCanvases();
|
|
Instance.custom_scroll_rect.content.GetComponent<VerticalLayoutGroup>().CalculateLayoutInputVertical();
|
|
Instance.custom_scroll_rect.content.GetComponent<ContentSizeFitter>().SetLayoutVertical();
|
|
Instance.custom_scroll_rect.verticalNormalizedPosition = 0;
|
|
return _console_item;
|
|
|
|
//if (auto_move && Instance.console_item_content.rect.height >= current_console_content_position_y)
|
|
// //update_position_y.Enqueue(Instance.console_item_content.rect.height);
|
|
// Instance.console_item_content.anchoredPosition = new Vector2(Instance.console_item_content.anchoredPosition.x, Instance.console_item_content.rect.height);
|
|
}
|
|
|
|
//[ContextMenu("log")]
|
|
//public void test()
|
|
//{
|
|
// ConsoleOutput("输出测试,log");
|
|
//}
|
|
//[ContextMenu("warning")]
|
|
//public void test1()
|
|
//{
|
|
// ConsoleOutput("输出测试,warn", "warning");
|
|
//}
|
|
//[ContextMenu("error")]
|
|
//public void test2()
|
|
//{
|
|
// ConsoleOutput("输出测试,error", "error");
|
|
//}
|
|
}
|