108 lines
2.7 KiB
C#
108 lines
2.7 KiB
C#
|
|
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using FairyGUI;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
|
|||
|
|
public class TestConsole
|
|||
|
|
{
|
|||
|
|
public int maxLogCount = 20;
|
|||
|
|
public bool errorAutoOpen = false; // 错误自动开启
|
|||
|
|
private bool isOn = false;
|
|||
|
|
private List<string> log_list = new List<string>();
|
|||
|
|
|
|||
|
|
private GComponent _log_view;
|
|||
|
|
private GButton buttonClear;
|
|||
|
|
private GButton buttonOpen;
|
|||
|
|
private GList list;
|
|||
|
|
private Controller log_controller;
|
|||
|
|
|
|||
|
|
private bool _open,_init;
|
|||
|
|
|
|||
|
|
|
|||
|
|
public void Open()
|
|||
|
|
{
|
|||
|
|
if (_open) return;
|
|||
|
|
_open = true;
|
|||
|
|
Application.logMessageReceived += HandleLog;
|
|||
|
|
if (!_init)
|
|||
|
|
{
|
|||
|
|
UIPackage.AddPackage("base/embed/ui/DebugLog");
|
|||
|
|
_log_view = UIPackage.CreateObjectFromURL("ui://DebugLog/Main").asCom;
|
|||
|
|
_log_view.MakeFullScreen();
|
|||
|
|
buttonClear = _log_view.GetChild("btn_clear").asButton;
|
|||
|
|
buttonOpen = _log_view.GetChild("btn_open").asButton;
|
|||
|
|
list = _log_view.GetChild("list").asList;
|
|||
|
|
log_controller = _log_view.GetController("log");
|
|||
|
|
EventRegister();
|
|||
|
|
_init = true;
|
|||
|
|
}
|
|||
|
|
_log_view.AddRelation(GRoot.inst, RelationType.Size);
|
|||
|
|
GRoot.inst.AddChild(_log_view);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Close()
|
|||
|
|
{
|
|||
|
|
if (!_open) return;
|
|||
|
|
_open = false;
|
|||
|
|
Application.logMessageReceived -= HandleLog;
|
|||
|
|
if (!_init) return;
|
|||
|
|
_log_view.RemoveFromParent();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void FillList()
|
|||
|
|
{
|
|||
|
|
list.RemoveChildrenToPool();
|
|||
|
|
foreach (string str in log_list)
|
|||
|
|
{
|
|||
|
|
GComponent gitem = list.AddItemFromPool() as GComponent;
|
|||
|
|
var text = gitem.GetChild("n0").asTextField;
|
|||
|
|
text.text = str;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
///
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="logString">错误信息</param>
|
|||
|
|
/// <param name="stackTrace">跟踪堆栈</param>
|
|||
|
|
/// <param name="type">错误类型</param>
|
|||
|
|
void HandleLog(string logString, string stackTrace, LogType type)
|
|||
|
|
{
|
|||
|
|
log_list.Add(logString);
|
|||
|
|
if (type == LogType.Error&& errorAutoOpen)
|
|||
|
|
{
|
|||
|
|
isOn = true;
|
|||
|
|
log_controller.selectedIndex = 1;
|
|||
|
|
}
|
|||
|
|
if (log_list.Count > maxLogCount)
|
|||
|
|
{
|
|||
|
|
log_list.RemoveAt(0);
|
|||
|
|
}
|
|||
|
|
FillList();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Clear()
|
|||
|
|
{
|
|||
|
|
log_list.Clear();
|
|||
|
|
FillList();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void EventRegister()
|
|||
|
|
{
|
|||
|
|
buttonClear.onClick.Set(() =>
|
|||
|
|
{
|
|||
|
|
Clear();
|
|||
|
|
});
|
|||
|
|
buttonOpen.onClick.Set(() =>
|
|||
|
|
{
|
|||
|
|
isOn = !isOn;
|
|||
|
|
log_controller.selectedIndex = isOn ? 1 : 0;
|
|||
|
|
_log_view.RemoveFromParent();
|
|||
|
|
GRoot.inst.AddChild(_log_view);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|