DragonECS-Unity/src/DebugUtils/UnityDebugService/UnityDebugService.cs

102 lines
3.1 KiB
C#
Raw Normal View History

2025-03-21 14:32:50 +08:00
using DCFApixels.DragonECS.Unity.Internal;
using System;
using System.Reflection;
2023-03-27 17:34:04 +08:00
using Unity.Profiling;
2025-03-21 14:32:50 +08:00
using UnityEditor;
2023-03-27 17:34:04 +08:00
using UnityEngine;
2024-10-05 12:45:01 +08:00
#region [InitializeOnLoad]
#if UNITY_EDITOR
2023-03-29 19:58:58 +08:00
namespace DCFApixels.DragonECS
2023-03-27 17:34:04 +08:00
{
2024-10-05 12:45:01 +08:00
using UnityEditor;
2024-03-07 03:18:00 +08:00
[InitializeOnLoad]
2024-10-05 12:45:01 +08:00
public partial class UnityDebugService { }
}
2024-04-29 22:12:51 +08:00
#endif
2024-10-05 12:45:01 +08:00
#endregion
namespace DCFApixels.DragonECS
{
// Методы юнитевского Debug и ProfilerMarker потоко безопасны
public partial class UnityDebugService : DebugService
2023-03-27 17:34:04 +08:00
{
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()
{
2025-03-20 10:34:20 +08:00
if (Instance.GetType() == typeof(UnityDebugService)) { return; }
2024-02-26 12:34:09 +08:00
Set<UnityDebugService>();
}
2024-10-05 12:45:01 +08:00
protected override DebugService CreateThreadInstance()
{
return new UnityDebugService();
}
2025-03-20 10:55:05 +08:00
#if UNITY_2021_3_OR_NEWER
[HideInCallstack]
#endif
2023-03-27 17:34:04 +08:00
public override void Print(string tag, object v)
{
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);
2025-03-21 16:51:03 +08:00
string indexedLink = UnityDebugServiceStorage.NewIndexedLink();
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":
2024-10-05 12:45:01 +08:00
Debug.Log(
2025-03-21 16:51:03 +08:00
$"[<color=#00ff00>{tag}</color>] {msg}{indexedLink}");
break;
case "warning":
2024-10-05 12:45:01 +08:00
Debug.LogWarning(
2025-03-21 16:51:03 +08:00
$"[<color=#ffff00>{tag}</color>] {msg}{indexedLink}");
break;
case "error":
2024-10-05 12:45:01 +08:00
Debug.LogError(
2025-03-21 16:51:03 +08:00
$"[<color=#ff4028>{tag}</color>] {msg}{indexedLink}");
break;
default:
2024-10-05 12:45:01 +08:00
Debug.Log(
2025-03-21 16:51:03 +08:00
$"[{tag}] {msg}{indexedLink}");
break;
2023-03-30 05:34:38 +08:00
}
2023-03-27 17:34:04 +08:00
return;
}
2025-03-21 16:51:03 +08:00
Debug.Log($"{msg}{indexedLink}");
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);
}
}
2024-10-05 12:45:01 +08:00
}