DragonECS-Unity/src/Debug/UnityDebugService.cs

75 lines
2.0 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-03-07 03:18:00 +08:00
[InitializeOnLoad]
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
}
bool hasTag = string.IsNullOrEmpty(tag) == false;
if (hasTag)
2023-03-27 17:34:04 +08:00
{
2023-05-07 00:50:44 +08:00
log = $".[{tag}] {v}";
2023-03-30 05:34:38 +08:00
string taglower = tag.ToLower();
if (taglower.Contains("warning"))
{
Debug.LogWarning(log);
return;
}
if (taglower.Contains("error"))
{
Debug.LogError(log);
return;
}
Debug.Log(log);
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);
}
}
}