- Home /
Clear console through code? (In development build)
Hi.
I have been playing around with the audio settings in the Unity editor, and I found out something weird - if I set the 'Default Speaker Mode' to 'Prologic DTS', it makes it makes it sound like an actual surround sound, on my stereo headphones. At the beginning of the game, an error will appear, telling me that the speaker mode is not supported, yet, it makes it sound so much better.
Question: How can I clear the console automatically, rather than having the player click "Clear" in the little console window that shows up on the lower left?
Thanks in advance!
Answer by ArkaneX · Nov 19, 2013 at 01:46 AM
You need to use Debug.ClearDeveloperConsole() But simply calling this method at Awake/Start will not work correctly. I did some tests, and it seems that developer console can't be cleared the same frame it became visible.
You can achieve what you want by using below sample (C#, but if you need it in JS, then you should be able to convert it fairly easily):
void Start ()
{
StartCoroutine(ClearConsole());
}
IEnumerator ClearConsole()
{
// wait until console visible
while(!Debug.developerConsoleVisible)
{
yield return null;
}
yield return null; // this is required to wait for an additional frame, without this clearing doesn't work (at least for me)
Debug.ClearDeveloperConsole();
}
Please be careful though, because this will clear any additional errors that might occur at the beginning of your game.
Thanks, that did the trick! I created an empty scene with a script that calls the function every frame, and loads the next level after the console is not visible, so there is no need to be concerned about missing any errors.
Edit: +1 for detailed answer
That's odd, it doesn't work for me, the only solution that works is : http://answers.unity3d.com/questions/707636/clear-console-window.html
Answer by netlander · Apr 29, 2015 at 01:44 PM
For recent versions of Unity (4.x) try this:
public static void ClearLog()
{
var assembly = Assembly.GetAssembly(typeof(UnityEditor.ActiveEditorTracker));
var type = assembly.GetType("UnityEditorInternal.LogEntries");
var method = type.GetMethod("Clear");
method.Invoke(new object(), null);
}
Don't forget to add:
using System.Reflection;
using UnityEditor;
Answer by Mr_Mow · Oct 23, 2018 at 01:52 AM
According to EditorSourceCode on Github, you should now use:
using UnityEditor;
using System.Reflection;
using System;
public static class Utils {
static MethodInfo _clearConsoleMethod;
static MethodInfo clearConsoleMethod {
get {
if (_clearConsoleMethod == null) {
Assembly assembly = Assembly.GetAssembly (typeof(SceneView));
Type logEntries = assembly.GetType ("UnityEditor.LogEntries");
_clearConsoleMethod = logEntries.GetMethod ("Clear");
}
return _clearConsoleMethod;
}
}
public static void ClearLogConsole() {
clearConsoleMethod.Invoke (new object (), null);
}
}
Do not use new object(), it can create a memory leak. You can use the GameObject class though I believe, but you'll have to check this:
GameObject invoker = new GameObject();
invoker.name = "Invoke Clear Console";
clearConsoleMethod.Invoke(invoker,null);
GameObject.DestroyImmediate(invoker,false); //destroyimmediate because it's in editor
I had to do this for enumerators recently, I didn't know that new object() creates a memory leak in C#, so with the Editor Coroutines package I passed in a game object instead that just re-creates itself at all times in the editor.
Answer by astracat111 · Feb 25, 2017 at 08:18 AM
This works differently now in Unity 5.5. They changed it to "UnityEditorInternal.LogEntries". Here's the code that will work using reflection for 5.5+. They'll probably change it again in 6 or something so consider yourself warned lol:
using UnityEditor;
using System.Reflection;
public static void ClearLogConsole() {
Assembly assembly = Assembly.GetAssembly (typeof(SceneView));
Type logEntries = assembly.GetType ("UnityEditorInternal.LogEntries");
MethodInfo clearConsoleMethod = logEntries.GetMethod ("Clear");
clearConsoleMethod.Invoke (new object (), null);
}
Change it to "UnityEditor.LogEntries" and it works for me in 2021.1.7f1
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
UnauthorizedAccessException | I Need Help Here. 1 Answer
1 exception was raised by workers: See the Console for details. 0 Answers
Game not working after build 0 Answers