I’d like to turn on/off debugging features in Unity during runtime to test for different features. Have I just been google incompetent or is there a way to do this? I’m assuming you cannot directly, so I’ve started writing a class where I can easily turn on and off specific debugging information (to test different parts of the game)… But, without being able to write to the console during runtime (such as in CryEngine) it only half serves it’s purpose. Any useful feedback appreciated.
Edit: If you find the selected answer useful, please thumb-it-up, it really deserves it.
You can do much better than writing to the console when working with Unity.
You can create your own debugger, which prints out whatever you’ve set it to print.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class DebuggerEditor : EditorWindow
{
enum bools{
False,
True
}
string newTag = "";
bools newBools = bools.False;
bool newValue = false;
[MenuItem("MyDebugger/debug")]
public static void Init(){
GetWindow<DebuggerEditor>();
}
void OnEnable(){
MyDebugger.tags.Add("TheImportantThings",true);
MyDebugger.tags.Add("RandomThings",true);
}
void OnGUI(){
foreach(KeyValuePair<string,bool> kvp in MyDebugger.IteratableDictionary()){
GUILayout.BeginHorizontal();
EditorGUI.BeginChangeCheck();
newValue = GUILayout.Toggle(MyDebugger.tags[kvp.Key], kvp.Key);
if(EditorGUI.EndChangeCheck()){
MyDebugger.SetTag(kvp.Key,newValue);
}
GUILayout.EndHorizontal();
}
GUILayout.BeginHorizontal();
newTag = GUILayout.TextField(newTag);
newBools = (bools)EditorGUILayout.EnumPopup(newBools);
GUILayout.EndHorizontal();
if(GUILayout.Button("Add"))
MyDebugger.SetTag(newTag,bool.Parse(newValue.ToString()));
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class MyDebugger {
#if UNITY_EDITOR
public static Dictionary<string,bool> tags = new Dictionary<string,bool>();
#endif
public static void Log(string tag, string message){
#if UNITY_EDITOR
if(!tags.ContainsKey(tag)){
Debug.LogWarning("Tag "+ tag +" not found in MyDebugger's TagDictionary! Adding it...");
tags.Add(tag,true);
}
if(tags[tag] == true)
Debug.Log(message);
#endif
}
public static Dictionary<string,bool> IteratableDictionary(){
#if UNITY_EDITOR
return new Dictionary<string, bool>(tags);
#endif
}
public static void SetTag(string tag, bool flag){
#if UNITY_EDITOR
if(tags.ContainsKey(tag))
tags[tag]= flag;
else
tags.Add(tag,flag);
#endif
}
}
Now, you can control what gets debugged at any given moment in runtime from the EditorWindow. You just call MyDebugger.Log(tag, message). If the tag doesn’t exist, you’ll get a warning and it’ll get added automatically, though the idea is you know beforehand what will get debugged and add it to the OnEnable in the EditorWindow.
You can use MyDebugger in normal game code, as it will not cause any performance drop because of useless debugs when built because of the preprocessor directives.
A dictionary is not the best choice here, as they do not get serialized by Unity. If you need the serialization functionality, then you’d be better off with e.g. two lists instead.