BITKit/Src/Unity/Scripts/UX/Debuger/UXDebuger.cs

67 lines
2.0 KiB
C#
Raw Normal View History

2024-07-05 15:07:38 +08:00
using System;
2023-06-05 19:57:17 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit;
2023-06-29 14:57:11 +08:00
using UnityEngine.UIElements;
using Cysharp.Threading.Tasks;
using System.Linq;
2024-11-03 16:38:17 +08:00
using System.Net.Http;
2024-07-05 15:07:38 +08:00
using System.Text;
2024-11-03 16:38:17 +08:00
using UnityEngine.Serialization;
2023-06-29 14:57:11 +08:00
2023-06-05 19:57:17 +08:00
namespace BITKit.UX
{
2023-06-29 14:57:11 +08:00
public sealed class UXDebuger : MonoBehaviour
2023-06-05 19:57:17 +08:00
{
2024-11-03 16:38:17 +08:00
[SerializeField] private Color textColor = Color.black;
[SerializeField]private GUIStyle style;
[SerializeReference, SubclassSelector] private IReference postApi;
[SerializeField] private bool hidePostLog;
2024-07-05 15:07:38 +08:00
private readonly StringBuilder _logBuilder=new();
2024-11-03 16:38:17 +08:00
private readonly HttpClient _httpClient = new();
2024-07-05 15:07:38 +08:00
private void OnEnable()
{
Application.logMessageReceivedThreaded += OnLog;
}
private void OnDisable()
{
Application.logMessageReceivedThreaded -= OnLog;
}
2024-11-03 16:38:17 +08:00
2024-07-05 15:07:38 +08:00
private void OnLog(string condition, string stacktrace, LogType type)
{
2024-11-03 16:38:17 +08:00
_logBuilder.AppendLine(condition);
if (type is LogType.Error or LogType.Exception)
{
_logBuilder.AppendLine(stacktrace);
}
if (string.IsNullOrEmpty(postApi?.Value) is false)
{
try
{
_httpClient.PostAsync(postApi.Value, new StringContent(_logBuilder.ToString())).AsUniTask()
.Forget();
}
catch (Exception)
{
if (hidePostLog) return;
throw;
}
}
2024-07-05 15:07:38 +08:00
}
private void OnGUI()
{
2024-11-03 16:38:17 +08:00
GUILayout.BeginArea(new Rect(120, 120, Screen.width, Screen.height));
2024-07-05 15:07:38 +08:00
GUILayout.BeginVertical();
2024-11-03 16:38:17 +08:00
//颜色更改为黑色
GUI.color = textColor;
GUILayout.Label(_logBuilder.ToString(),style);
2024-07-05 15:07:38 +08:00
GUILayout.EndVertical();
GUILayout.EndArea();
}
2023-06-05 19:57:17 +08:00
}
}