Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Runtime.CompilerServices;
- using UnityEngine;
- public static class Trace
- {
- [Conditional("UNITY_EDITOR")]
- public static void Info(string message, [CallerMemberName] string memberName = "")
- {
- UnityEngine.Debug.Log(Format(memberName, message));
- }
- [Conditional("UNITY_EDITOR")]
- public static void Warning(string message, [CallerMemberName] string memberName = "")
- {
- UnityEngine.Debug.LogWarning(Format(memberName, message));
- }
- [Conditional("UNITY_EDITOR")]
- public static void Error(string message, [CallerMemberName] string memberName = "")
- {
- UnityEngine.Debug.LogError(Format(memberName, message));
- }
- [Conditional("UNITY_EDITOR")]
- public static void Exception(Exception exception, [CallerMemberName] string memberName = "")
- {
- UnityEngine.Debug.LogError(Format(memberName, exception.ToString()));
- }
- private static string Format(string memberName, string message)
- {
- var typeName = GetCallerTypeName();
- var color = ColorUtility.ToHtmlStringRGB(GetColor(typeName));
- return $"[frame:{Time.frameCount}] <color=#{color}>[{typeName}.{memberName}]</color> {message}";
- }
- private static string GetCallerTypeName()
- {
- var type = new StackFrame(skipFrames: 3).GetMethod().DeclaringType;
- while (type != null && type.IsDefined(typeof(CompilerGeneratedAttribute), false))
- {
- type = type.DeclaringType;
- }
- return type?.Name ?? "Unknown";
- }
- private static Color GetColor(string typeName)
- {
- var hash = FNV1aHash(typeName);
- var hue = (hash % 360) / 360f;
- return Color.HSVToRGB(hue, 0.6f, 0.9f);
- }
- private static uint FNV1aHash(string text)
- {
- uint hash = 2166136261;
- foreach (var character in text)
- {
- hash ^= character;
- hash *= 16777619;
- }
- return hash;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment