Skip to content

Instantly share code, notes, and snippets.

@javier-games
Created May 24, 2019 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javier-games/ccd2a76d34c5d19e772adb8c4b8840d2 to your computer and use it in GitHub Desktop.
Save javier-games/ccd2a76d34c5d19e772adb8c4b8840d2 to your computer and use it in GitHub Desktop.
Usefull utilities for debuging on Unity.

Debug Utils

Usefull utilities for debuging.

  • The May 24th, 2019 version contains: -- Debug.Log Methods to visible only in debug mode and unity editor. -- Filter System to show only desired messages.

Add this feature to your project by just adding this files. Additionally, Option A) Move the DebuUtilsSetupEditor.cs to Assets/Editor. Or option B) Add UNITY_EDITOR directives to this file to avoid compiling errors.

Unity Version Twitter GitHub

using UnityEngine;
/// <summary>
///
/// Debug Utils.
///
/// <para>
/// Usefull utilities for debuging. | The May 24th, 2019 version contains:
/// 1.- Debug.Log Methods visible only in debug mode and unity editor.
/// 2.- Filter System to show only desired messages.
/// </para>
///
/// <para> By Javier García | @jvrgms | 2019 </para>
/// </summary>
public static class DebugUtils {
#region Nested Classes
/// <summary> Level. </summary>
public enum Level {
Debug = 0, // Level with debugging purposes.
Info = 1, // Level for public important info.
DevInfo = 1, // Level for info just for development.
Warning = 3, // Level for public warnings.
DevWarning = 4, // Level for development warnings.
Error = 5, // Level for public errors.
DevError = 6 // Level for development errors.
};
#endregion
#region Filter System
/// <summary> Gets the debug filter. </summary>
/// <value>The debug filter.</value>
public static int Filter { get; set; } = 0;
/// <summary> Sets completely the debug filter. </summary>
/// <param name="levels">Filters.</param>
public static void SetFilter (params Level[] levels) {
Filter = 0;
foreach (Level level in levels)
AddLevelToFilter (level);
}
/// <summary> Gets the array of levels in filter. </summary>
/// <returns>The levels.</returns>
public static Level[] GetFilteredLevels () {
int levelsCount = 0;
for(int i = 0; i <= 6; i++)
if (IsInFilter ((Level)i))
levelsCount++;
Level[] levels = new Level[levelsCount];
for (int i = 0; i <= 6; i++)
if (IsInFilter ((Level)i))
levels[i] = (Level)i;
return levels;
}
/// <summary> Adds a new filter. </summary>
/// <param name="level">Filter.</param>
public static void AddLevelToFilter (Level level) {
Filter |= 1 << (int)level;
}
/// <summary> Removes a level from filter. </summary>
/// <param name="level">Level.</param>
public static void RemoveLevelFromFilter (Level level) {
if (IsInFilter (level))
Filter &= ~(1 << (int)level);
}
/// <summary> Defines if a filter belongs to the debug filter. </summary>
/// <returns><c>true</c> if is in debugfilter.</returns>
/// <param name="filter">Filter.</param>
public static bool IsInFilter (Level filter) {
return (Filter & (1 << (int)filter)) > 0;
}
#endregion
#region Private Methods
/// <summary> Convert an object to an understandable string. </summary>
/// <returns>The string.</returns>
/// <param name="s">S.</param>
private static string DebuggableString (object s) {
//For debug purposes the array is checked to evidentiate null objects.
return s != null ? s.ToString () : "null";
}
/// <summary> Convert an array to an understandable string array. </summary>
/// <returns>The string.</returns>
/// <param name="array">Array.</param>
private static string[] DebuggableString (params object[] array) {
string[] stringArray = new string[array.Length];
for (int i = 0; i < array.Length; i++)
stringArray[i] = DebuggableString (array[i]);
return stringArray;
}
#endregion
#region Log Methods
#region Public
/// <summary> Logs a debug message to the Unity Console. </summary>
/// <param name="array">String array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void Log (params object[] array) {
if (IsInFilter (Level.Debug))
Debug.Log (StringUtils.Concat (DebuggableString (array)));
}
/// <summary> Logs a formated debug message to the Unity Console. </summary>
/// <param name="format">Format.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogFormat (string format, params object[] array) {
if (IsInFilter (Level.Debug))
Debug.LogFormat (format, array);
}
/// <summary> Logs an info message to the Unity Console. </summary>
/// <param name="array">String array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogInfo (params object[] array) {
if (IsInFilter (Level.Info))
Debug.Log (StringUtils.Concat (DebuggableString (array)));
}
/// <summary> Logs a formated info message to the Unity Console. </summary>
/// <param name="format">Format.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogInfoFormat (string format, params object[] array) {
if (IsInFilter (Level.Info))
Debug.LogFormat (format, array);
}
/// <summary> Logs an error message to the Unity Console. </summary>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogError (params object[] array) {
if (IsInFilter (Level.Error))
Debug.LogError (StringUtils.Concat (DebuggableString (array)));
}
/// <summary> Logs an error message to the Unity Console. </summary>
/// <param name="context">Context.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogError (Object context, params object[] array) {
if (IsInFilter (Level.Error))
Debug.LogError (StringUtils.Concat (DebuggableString (array)), context);
}
/// <summary> Logs an formated error message to the Unity Console. </summary>
/// <param name="format">Format.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogErrorFormat (string format, params object[] array) {
if (IsInFilter (Level.Error))
Debug.LogErrorFormat (format, array);
}
/// <summary> Logs a warning message to the Unity Console. </summary>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogWarning (params object[] array) {
if (IsInFilter (Level.Warning))
Debug.LogWarning (StringUtils.Concat (DebuggableString (array)));
}
/// <summary> Logs a warning message to the Unity Console. </summary>
/// <param name="context">Context.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogWarning (Object context, params object[] array) {
if (IsInFilter (Level.Warning))
Debug.LogWarning (StringUtils.Concat (DebuggableString (array)), context);
}
/// <summary> Logs a formated warning message to the Unity Console. </summary>
/// <param name="format">Format.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogWarningFormat (string format, params object[] array) {
if (IsInFilter (Level.Warning))
Debug.LogWarningFormat (format, array);
}
/// <summary> Logs an assertion message to the Unity Console. </summary>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogAssertion (params object[] array) {
if (IsInFilter (Level.Error))
Debug.LogAssertion (StringUtils.Concat (DebuggableString (array)));
}
/// <summary> Logs an assertion message to the Unity Console. </summary>
/// <param name="context">Context.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogAssertion (Object context, params object[] array) {
if (IsInFilter (Level.Error))
Debug.LogAssertion (StringUtils.Concat (DebuggableString (array)), context);
}
/// <summary> Logs a formated assertion message to the Unity Console. </summary>
/// <param name="format">Format.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogAssertionFormat (string format, params object[] array) {
if (IsInFilter (Level.Error))
Debug.LogAssertionFormat (format, array);
}
/// <summary> Logs an exception message to the Unity Console. </summary>
/// <param name="exception">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogException (System.Exception exception) {
if (IsInFilter (Level.Error))
Debug.LogException (exception);
}
/// <summary> Logs an exception message to the Unity Console. </summary>
/// <param name="context">Context.</param>
/// <param name="exception">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
public static void LogException (Object context, System.Exception exception) {
if (IsInFilter (Level.Error))
Debug.LogException (exception, context);
}
#endregion
#region Internal
/// <summary> Logs an info message to the Unity Console. </summary>
/// <param name="array">String array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevInfo (params object[] array) {
if (IsInFilter (Level.DevInfo))
Debug.Log (StringUtils.Concat (DebuggableString (array)));
}
/// <summary> Logs a formated info message to the Unity Console. </summary>
/// <param name="format">Format.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevInfoFormat (string format, params object[] array) {
if (IsInFilter (Level.DevInfo))
Debug.LogFormat (format, array);
}
/// <summary> Logs an error message to the Unity Console. </summary>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevError (params object[] array) {
if (IsInFilter (Level.DevError))
Debug.LogError (StringUtils.Concat (DebuggableString (array)));
}
/// <summary> Logs an error message to the Unity Console. </summary>
/// <param name="context">Context.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevError (Object context, params object[] array) {
if (IsInFilter (Level.DevError))
Debug.LogError (StringUtils.Concat (DebuggableString (array)), context);
}
/// <summary> Logs a formated errpr message to the Unity Console. </summary>
/// <param name="format">Format.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevErrorFormat (string format, params object[] array) {
if (IsInFilter (Level.DevError))
Debug.LogErrorFormat (format, array);
}
/// <summary> Logs a warning message to the Unity Console. </summary>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevWarning (params object[] array) {
if (IsInFilter (Level.DevWarning))
Debug.LogWarning (StringUtils.Concat (DebuggableString (array)));
}
/// <summary> Logs a warning message to the Unity Console. </summary>
/// <param name="context">Context.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevWarning (Object context, params object[] array) {
if (IsInFilter (Level.DevWarning))
Debug.LogWarning (StringUtils.Concat (DebuggableString (array)), context);
}
/// <summary> Logs a formated warning message to the Unity Console. </summary>
/// <param name="format">Format.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevWarningFormat (string format, params object[] array) {
if (IsInFilter (Level.DevWarning))
Debug.LogWarningFormat (format, array);
}
/// <summary> Logs an assertion message to the Unity Console. </summary>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevAssertion (params object[] array) {
if (IsInFilter (Level.DevError))
Debug.LogAssertion (StringUtils.Concat (DebuggableString (array)));
}
/// <summary> Logs an assertion message to the Unity Console. </summary>
/// <param name="context">Context.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevAssertion (Object context, params object[] array) {
if (IsInFilter (Level.DevError))
Debug.LogAssertion (StringUtils.Concat (DebuggableString (array)), context);
}
/// <summary> Logs a formated assertion message to the Unity Console. </summary>
/// <param name="format">Format.</param>
/// <param name="array">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevAssertionFormat (string format, params object[] array) {
if (IsInFilter (Level.DevError))
Debug.LogAssertionFormat (format, array);
}
/// <summary> Logs an error message to the Unity Console. </summary>
/// <param name="exception">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevException (System.Exception exception) {
if (IsInFilter (Level.DevError))
Debug.LogException (exception);
}
/// <summary> Logs an error message to the Unity Console. </summary>
/// <param name="context">Context.</param>
/// <param name="exception">Array.</param>
[System.Diagnostics.Conditional ("DEBUG")]
internal static void LogDevException (Object context, System.Exception exception) {
if (IsInFilter (Level.DevError))
Debug.LogException (exception, context);
}
#endregion
#endregion
}
using UnityEngine;
/// <summary>
///
/// Debug Utils Setup.
///
/// <para>
/// Sets the filter in Debug Utils on awake.
/// </para>
///
/// <para> By Javier García | @jvrgms | 2019 </para>
/// </summary>
public class DebugUtilsSetup : MonoBehaviour {
[SerializeField]
private int filter; // Filter assigned from editor.
/// <summary> Called on awake. </summary>
private void Awake () {
// Assining the filter to DebugUtils.
DebugUtils.Filter = filter;
// Testing the filters.
DebugUtils.Log ("Debug Logs Enabled");
DebugUtils.LogInfo ("Info Logs Enabled");
DebugUtils.LogWarning ("Warning Logs Enabled");
DebugUtils.LogError ("Error Logs Enabled");
DebugUtils.LogDevInfo ("Info Logs Enabled");
DebugUtils.LogDevWarning ("Warning Logs Enabled");
DebugUtils.LogDevError ("Error Logs Enabled");
}
}
using UnityEngine;
using UnityEditor;
/// <summary>
///
/// Debug Utils Setup Editor.
///
/// <para>
/// Displays as a mask the filter on DebugUtilsSetup.cs.
/// </para>
///
/// <para> By Javier García | @jvrgms | 2019 </para>
/// </summary>
[CustomEditor (typeof (DebugUtilsSetup))]
public class DebugUtilsSetupEditor: Editor {
private SerializedProperty filterProperty; // Ref to DebugUtilsSetup.filter.
private readonly string[] levelArray = {
"Debug", // Level with debugging purposes.
"Info", // Level for public important info.
"DevInfo", // Level for info just for development.
"Warning", // Level for public warnings.
"DevWarning", // Level for development warnings.
"Error", // Level for public errors.
"DevError" // Level for development errors.
};
/// <summary> Called On Enable </summary>
public void OnEnable () {
// Initialize the property.
filterProperty = serializedObject.FindProperty ("filter");
}
/// <summary> Called on Inspector </summary>
public override void OnInspectorGUI () {
serializedObject.Update ();
EditorGUILayout.Space ();
// Draw and get the value for the mask.
filterProperty.intValue = EditorGUILayout.MaskField (
label: new GUIContent ("Filter"),
mask: filterProperty.intValue,
displayedOptions: levelArray
);
serializedObject.ApplyModifiedProperties ();
}
}
MIT License
Copyright (c) 2019 Javier García
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
using System.Text;
using System.IO;
/// <summary>
///
/// StringUtils.
///
/// <para>
/// Usefull utilities to work with strings. | The May 24th, 2019 version
/// contains:
/// 1.- Concatenation methods.
/// </para>
///
/// <para> By Javier García | @jvrgms | 2019 </para>
/// </summary>
public static class StringUtils {
#region Concatenation
/// <summary> Concatenate the specified strings. </summary>
/// <returns>The concatenated string.</returns>
/// <param name="array">String array.</param>
public static string Concat (params object[] array) {
/*
* Encapsulation of string builder to not interact with other
* code and avoid unusal behaviour.
*/
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < array.Length; i++)
stringBuilder.Append (array[i]);
return stringBuilder.ToString ();
}
/// <summary> Concatenates with the format. </summary>
/// <returns>The format.</returns>
/// <param name="format">Format.</param>
/// <param name="array">Array.</param>
public static string ConcatFormat (string format, params object[] array) {
/*
* Encapsulation of string builder to not interact with other
* code and avoid unusal behaviour.
*/
StringBuilder stringBuilder = new StringBuilder ();
stringBuilder.AppendFormat (format, array);
return stringBuilder.ToString ();
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment