Guest User

Trace.cs

a guest
Apr 17th, 2026
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5.  
  6. public static class Trace
  7. {
  8.     [Conditional("UNITY_EDITOR")]
  9.     public static void Info(string message, [CallerMemberName] string memberName = "")
  10.     {
  11.         UnityEngine.Debug.Log(Format(memberName, message));
  12.     }
  13.  
  14.     [Conditional("UNITY_EDITOR")]
  15.     public static void Warning(string message, [CallerMemberName] string memberName = "")
  16.     {
  17.         UnityEngine.Debug.LogWarning(Format(memberName, message));
  18.     }
  19.  
  20.     [Conditional("UNITY_EDITOR")]
  21.     public static void Error(string message, [CallerMemberName] string memberName = "")
  22.     {
  23.         UnityEngine.Debug.LogError(Format(memberName, message));
  24.     }
  25.  
  26.     [Conditional("UNITY_EDITOR")]
  27.     public static void Exception(Exception exception, [CallerMemberName] string memberName = "")
  28.     {
  29.         UnityEngine.Debug.LogError(Format(memberName, exception.ToString()));
  30.     }
  31.  
  32.     private static string Format(string memberName, string message)
  33.     {
  34.         var typeName = GetCallerTypeName();
  35.         var color = ColorUtility.ToHtmlStringRGB(GetColor(typeName));
  36.  
  37.         return $"[frame:{Time.frameCount}] <color=#{color}>[{typeName}.{memberName}]</color> {message}";
  38.     }
  39.  
  40.     private static string GetCallerTypeName()
  41.     {
  42.         var type = new StackFrame(skipFrames: 3).GetMethod().DeclaringType;
  43.  
  44.         while (type != null && type.IsDefined(typeof(CompilerGeneratedAttribute), false))
  45.         {
  46.             type = type.DeclaringType;
  47.         }
  48.  
  49.         return type?.Name ?? "Unknown";
  50.     }
  51.  
  52.     private static Color GetColor(string typeName)
  53.     {
  54.         var hash = FNV1aHash(typeName);
  55.         var hue = (hash % 360) / 360f;
  56.  
  57.         return Color.HSVToRGB(hue, 0.6f, 0.9f);
  58.     }
  59.  
  60.     private static uint FNV1aHash(string text)
  61.     {
  62.         uint hash = 2166136261;
  63.  
  64.         foreach (var character in text)
  65.         {
  66.             hash ^= character;
  67.             hash *= 16777619;
  68.         }
  69.  
  70.         return hash;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment