- Home /
Clear Console Window
I want to clear content of console window. At present, I have to press clear button each time to rub content. Because by default console window clears when we play our project.
I want keyboard shortcut for this so that I can reduce my labour work.
Answer by Bunny83 · May 14, 2014 at 11:42 AM
frarees idea goes in the right direction, however "Debug.ClearDeveloperConsole" is related to the developer console which is built into a Unity build itself when you create a debug-build.
Like ever so often Unity made the LogEntries class, which contains the needed Clear method, an internal class, but thanks to reflection you can call it like this:
// C# editor script
using UnityEngine;
using UnityEditor;
static class UsefulShortcuts
{
[MenuItem ("Tools/Clear Console %#c")] // CMD + SHIFT + C
static void ClearConsole () {
// This simply does "LogEntries.Clear()" the long way:
var logEntries = System.Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll");
var clearMethod = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
clearMethod.Invoke(null,null);
}
}
Thanks! Also note that if, like me, your console shortcut is already cmd+shift+C (I'm on Unity 5.2), choose another one for ClearConsole.
In unity 2017.1 LogEntries are located in UnityEditor namespace, not in UnityEditorInternal. So just replace UnityEditorInternal with UnityEditor to make this code work for unity 2017.1.
Hi! Thanks for the script. However, I'm not sure how to use it. Should I add this to, say, the $$anonymous$$ain Camera object for the shortcut to be available?
Thanks in advance.
This script is not a $$anonymous$$onoBehaviour and therefore not a component. So it can't be "attached" to anything. It's an editor script which you just have to put in a subfolder called "editor" inside your assets folder. You don't have to do anything with that script. It's a static class and the "$$anonymous$$enuItem" attribute will add a new menu item to the Unity editor main menu. Specifically once you have that script in an editor folder you should have the "Clear Console" menu item inside the Tools menu.
$$anonymous$$eep in $$anonymous$$d that as you can read in the comment above since about Unity 2017 they moved the LogEntries class from the UnityEditorInternal namespace to the UnityEditor namespace. If you use an older version of Unity (at least up to Unity 5.6) you have to use my version. If you use Unity 2017 or newer you have to change "UnityEditorInternal" to "UnityEditor".
I just noticed the "Remember to place this script in an Editor folder", sorry. It works now.
Thanks a lot!
Answer by KirillKuzyk · Jul 29, 2017 at 09:47 PM
In unity 2017.1 LogEntries are located in UnityEditor namespace, not in UnityEditorInternal. So you should use this code from now on:
using System.Reflection;
public static void ClearConsole()
{
var assembly = Assembly.GetAssembly(typeof(SceneView));
var type = assembly.GetType("UnityEditor.LogEntries");
var method = type.GetMethod("Clear");
method.Invoke(new object(), null);
}
Thank you so much for posting this answer. I have been searching for the solution for a long time. :D
The function does its job. But don't know why I got logged out from the unity hub. And now I cannot sign in from the hub. it just shows the blank page but it never loads. This never happens to me but today, after using this code. also, the Unity Hub lost the editor's path. does anyone know why this happened? @KirillKuzyk @Yo318 @Vadol @astracat111
Answer by astracat111 · Feb 25, 2017 at 08:19 AM
This is the code that works using Unity 5. Remember to put using System.Reflection; and using UnityEditor; at the top:
public static void Misc_ClearLogConsole() {
Assembly assembly = Assembly.GetAssembly (typeof(SceneView));
Type logEntries = assembly.GetType ("UnityEditorInternal.LogEntries");
MethodInfo clearConsoleMethod = logEntries.GetMethod ("Clear");
clearConsoleMethod.Invoke (new object (), null);
}
Answer by frarees · May 14, 2014 at 09:56 AM
Check how MenuItem works to define custom shortcuts.
using UnityEngine;
using UnityEditor;
static class UsefulShortcuts {
[MenuItem ("Tools/Clear Console %#c")] // CMD + SHIFT + C
static void ClearConsole () {
Debug.ClearDeveloperConsole ();
}
}
Remember to place this script in an Editor
folder. More info here.
Using this I can't able to clear console. Though Tools menu created but it can't perform any action on click or key press.
So please give some guidance and thanks for reply.
@siddharth3322 It only clear errors, not all logs will be cleared
Your answer
Follow this Question
Related Questions
Intercepting keystrokes in Internet Explorer 0 Answers
Internal Compiler Error 1 Answer
How can I activate the context menu in the Unity Editor via the keyboard? 0 Answers
Load Scene When... 1 Answer