DragonECS-Unity/src/DebugUtils/UnityDebugService.cs

86 lines
2.5 KiB
C#
Raw Normal View History

2023-03-27 17:34:04 +08:00
using System;
using Unity.Profiling;
2024-03-07 03:18:00 +08:00
using UnityEditor;
2023-03-27 17:34:04 +08:00
using UnityEngine;
2023-03-29 19:58:58 +08:00
namespace DCFApixels.DragonECS
2023-03-27 17:34:04 +08:00
{
2024-04-29 22:12:51 +08:00
#if UNITY_EDITOR
2024-03-07 03:18:00 +08:00
[InitializeOnLoad]
2024-04-29 22:12:51 +08:00
#endif
2023-03-27 17:34:04 +08:00
public class UnityDebugService : DebugService
{
private ProfilerMarker[] _profilerMarkers = new ProfilerMarker[64];
2024-03-07 03:18:00 +08:00
static UnityDebugService()
{
Activate();
}
2024-02-26 12:34:09 +08:00
public static void Activate()
{
Set<UnityDebugService>();
}
2023-03-27 17:34:04 +08:00
public override void Print(string tag, object v)
{
2023-03-30 05:34:38 +08:00
string log;
2024-02-26 12:34:09 +08:00
if (v is Exception e)
{
Debug.LogException(e);
2024-03-03 03:51:49 +08:00
return;
2024-02-26 12:34:09 +08:00
}
string msg = AutoConvertObjectToString(v);
2024-02-26 12:34:09 +08:00
bool hasTag = string.IsNullOrEmpty(tag) == false;
if (hasTag)
2023-03-27 17:34:04 +08:00
{
2023-03-30 05:34:38 +08:00
string taglower = tag.ToLower();
switch (taglower)
2023-03-30 05:34:38 +08:00
{
case "pass":
log = $"[<color=#00ff00>{tag}</color>] {msg}";
Debug.Log(log);
break;
case "warning":
log = $"[<color=#ffff00>{tag}</color>] {msg}";
Debug.LogWarning(log);
break;
case "error":
log = $"[<color=#ff4028>{tag}</color>] {msg}";
Debug.LogError(log);
break;
default:
log = $"[{tag}] {msg}";
Debug.Log(log);
break;
2023-03-30 05:34:38 +08:00
}
2023-03-27 17:34:04 +08:00
return;
}
2023-03-30 05:34:38 +08:00
Debug.Log(v);
2023-03-27 17:34:04 +08:00
}
2023-06-30 01:17:49 +08:00
public override void Break()
{
Debug.Break();
}
2024-02-26 12:34:09 +08:00
public sealed override void ProfilerMarkBegin(int id)
2023-03-27 17:34:04 +08:00
{
_profilerMarkers[id].Begin();
}
2024-02-26 12:34:09 +08:00
public sealed override void ProfilerMarkEnd(int id)
2023-03-27 17:34:04 +08:00
{
_profilerMarkers[id].End();
}
2024-02-26 12:34:09 +08:00
protected sealed override void OnDelProfilerMark(int id)
2023-03-27 17:34:04 +08:00
{
_profilerMarkers[id] = default;
}
2024-02-26 12:34:09 +08:00
protected sealed override void OnNewProfilerMark(int id, string name)
2023-03-27 17:34:04 +08:00
{
2024-02-26 12:34:09 +08:00
if (id >= _profilerMarkers.Length)
{
Array.Resize(ref _profilerMarkers, _profilerMarkers.Length << 1);
}
2023-03-27 17:34:04 +08:00
_profilerMarkers[id] = new ProfilerMarker(ProfilerCategory.Scripts, name);
}
}
}