- Home /
Unity Debug Console - I want to input things during Runtime
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.
Sure, add this to your code
Debug.Log("On the unity answers site, it is essential to" +
"TIC$$anonymous$$ questions that have been answered.");
Debug.Log("This is the only way to keep the site tidy.");
note that you can also easily include almost any variable,
Debug.Log("The value of enormousTank is " +enormousTank);
Also try Debug.LogError() for another effect
FInally look at the doco page for "Debug" for many other tips.
I appreciate the response @Fattie. I think my question may have been a bit unclear perhaps. I was thinking more along the lines of hitting ~ tilde or something and bringing up a special console where I can type variable names and bool values to turn them on and off during runtime.
Fair enough. If you want to do that it is entirely built-in to Unity. Hit play. Now look at the Inspector. Click on your Player, Enemy, etc. In the Inspector, you can change booleans, type in new values etc. as the game is running. So that's all done. Hope it helps.
Answer by Jamora · Sep 29, 2013 at 10:45 AM
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.
Excellent. This is everything I was looking for and the code to boot - much appreciated. I hope many get great use of it.
Your answer
Follow this Question
Related Questions
Double clicking console output does not take me to the corresponding line in code. 1 Answer
Is there any way to view the console in a build? 7 Answers
Input Button is not setup Error 1 Answer
How to use Webgl Debug Symbols? 0 Answers
How to clear a specific error/warning in the console output window? 0 Answers