Created
August 23, 2025 15:01
-
-
Save ShacharWeis/1e654da40611d040c429e7b7d3441cfd to your computer and use it in GitHub Desktop.
Debug log helper for Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System; | |
| using System.Linq.Expressions; | |
| using System.Text; | |
| using System.Globalization; | |
| using System.Collections.Generic; | |
| namespace Packet39 | |
| { | |
| public static class FancyDebugLogger | |
| { | |
| static string GetName<T>(Expression<Func<T>> expr) | |
| { | |
| if (expr == null) return "null"; | |
| Expression body = expr.Body; | |
| if (body is UnaryExpression ue && ue.Operand != null) | |
| body = ue.Operand; | |
| if (body is MemberExpression me) | |
| return me.Member.Name; | |
| if (body is MethodCallExpression mce) | |
| return mce.Method.Name; | |
| return expr.Body.ToString(); | |
| } | |
| static object GetValue<T>(Expression<Func<T>> expr) | |
| { | |
| if (expr == null) return null; | |
| try | |
| { | |
| return expr.Compile().Invoke(); | |
| } | |
| catch | |
| { | |
| return null; | |
| } | |
| } | |
| static string FormatValue(object val) | |
| { | |
| if (val == null) return "null"; | |
| if (val is float f) return f.ToString("G9", CultureInfo.InvariantCulture); | |
| if (val is double d) return d.ToString("G17", CultureInfo.InvariantCulture); | |
| if (val is IFormattable fmt) return fmt.ToString(null, CultureInfo.InvariantCulture); | |
| return val.ToString(); | |
| } | |
| static void BuildAndLog(MonoBehaviour mb, string prefix, params KeyValuePair<string, object>[] items) | |
| { | |
| if (mb == null) return; | |
| // Unity rich text colors | |
| string className = $"<color=green>{mb.GetType().Name}</color>"; | |
| string objectName = $"<color=yellow>{(mb.gameObject != null ? mb.gameObject.name : "null")}</color>"; | |
| var sb = new StringBuilder(); | |
| sb.Append('[').Append(className).Append("][") | |
| .Append(objectName).Append("] "); | |
| if (!string.IsNullOrEmpty(prefix)) | |
| { | |
| sb.Append($"<color=#EE9B9B><b>{prefix}:</b></color> "); | |
| } | |
| for (int i = 0; i < items.Length; i++) | |
| { | |
| var kv = items[i]; | |
| string varName = $"<color=white>{kv.Key}</color>"; | |
| string varValue = $"<color=#00BFFF>{FormatValue(kv.Value)}</color>"; | |
| sb.Append(varName).Append('=').Append(varValue); | |
| if (i < items.Length - 1) sb.Append(' '); | |
| } | |
| Debug.Log(sb.ToString()); | |
| } | |
| public static void FancyLog(this MonoBehaviour mb, string prefix, params Expression<Func<object>>[] expressions) | |
| { | |
| var items = new List<KeyValuePair<string, object>>(); | |
| foreach (var expr in expressions) | |
| { | |
| items.Add(new KeyValuePair<string, object>(GetName(expr), GetValue(expr))); | |
| } | |
| BuildAndLog(mb, prefix, items.ToArray()); | |
| } | |
| public static void FancyLog(this MonoBehaviour mb, params Expression<Func<object>>[] expressions) | |
| { | |
| var items = new List<KeyValuePair<string, object>>(); | |
| foreach (var expr in expressions) | |
| { | |
| items.Add(new KeyValuePair<string, object>(GetName(expr), GetValue(expr))); | |
| } | |
| BuildAndLog(mb, null, items.ToArray()); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
this.FancyLog("Check this out",() => foundLetters, () => totalLetters, () => progess);
Will result in this console output:
[ProgressHelper][Progress] Check this out: foundLetters=2 totalLetters=65 progess=0.0307692308
Format:
[ClassName][GameObjectName] HeaderString (optional): VarName=Value VarName=Value ...
Output is colorized: