Skip to content

Instantly share code, notes, and snippets.

@Marsgames
Last active October 7, 2020 13:58
Show Gist options
  • Save Marsgames/c36d40ae366aa131f327b944c8164a8b to your computer and use it in GitHub Desktop.
Save Marsgames/c36d40ae366aa131f327b944c8164a8b to your computer and use it in GitHub Desktop.
Fonctions that can be usefull in all Unity projects (put this file into Assets/Editor folder)
#region Author
/////////////////////////////////////////
// RAPHAEL DAUMAS --> Utils
// https://raphdaumas.wixsite.com/portfolio
// https://github.com/Marsgames
/////////////////////////////////////////
#endregion
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
public static class Utils
{
#if UNITY_EDITOR
private static readonly string color = UnityEditor.EditorGUIUtility.isProSkin ? "<color=#0ff>" : "<color=#f0f>";
#else
private static readonly string color = "<color=#0ff>";
#endif
#region Dumps
public static void Dump<T, U>(Dictionary<T, U> dictionary, bool useWarning = false)
{
string message = "";
Action printFunction;
if (useWarning)
{
printFunction = delegate { Debug.LogWarning(message); };
}
else
{
printFunction = delegate { Debug.Log(message); };
}
message = $"{color}----- Dump(Dictionary<{typeof(T)}, {typeof(U)}>) -----</color>";
printFunction();
foreach (KeyValuePair<T, U> kvp in dictionary)
{
message = ($"{color}{string.Format("Key = {0}, Value = {1}", kvp.Key.ToString(), kvp.Value.ToString())}</color>");
printFunction();
}
}
public static void Dump<T>(T obj, bool useWarning = false)
{
string message = "";
Action printFunction;
if (useWarning)
{
printFunction = delegate { Debug.LogWarning(message); };
}
else
{
printFunction = delegate { Debug.Log(message); };
}
message = $"{color}----- Dump(obj<{typeof(T)}>) -----</color>";
printFunction();
message = ($"{color}{obj.ToString()}</color>");
printFunction();
}
public static void Dump<T>(T[] array, bool useWarning = false)
{
string message = "";
Action printFunction;
if (useWarning)
{
printFunction = delegate { Debug.LogWarning(message); };
}
else
{
printFunction = delegate { Debug.Log(message); };
}
message = $"{color}----- Dump({typeof(T)}[]) -----</color>";
printFunction();
foreach (T element in array)
{
message = ($"{color}{element.ToString()}</color>");
printFunction();
}
}
/// <summary>
/// Use this function to Dump every member of a struct
/// </summary>
/// <param name="structure">struct you want to dump</param>
/// <param name="useWarning">Tell if you want to log this as warning or as normal logs</param>
/// <typeparam name="T"></typeparam>
public static void DumpStruct<T>(T structure, bool useWarning = false)
{
string message = "";
Action printFunction;
if (useWarning)
{
printFunction = delegate { Debug.LogWarning(message); };
}
else
{
printFunction = delegate { Debug.Log(message); };
}
message = $"{color}----- DumpStruct({typeof(T)}[]) -----</color>";
printFunction();
foreach (var field in typeof(T).GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
message = $"{color}{field.Name} = {field.GetValue(structure)}</color>";
printFunction();
}
}
#endregion Dumps
///////////////////////////////////////////////////////////
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment