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!

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);
	}
}

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;

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.

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);
}

Debug.Log(“”);

This doesn’t actually clear the log, it just replaces the most recent line with a blank, which works fine when the console is minimized at the bottom. Just run it a few seconds after the game starts to clear the barrage of stupid warnings about things that aren’t really problems that often crop up when you hit Play. My favourites are warnings about time codes for every mp4 video in my scene. What would be nice though would be a way to selectively clear useless errors that crop up later on during game-play, like the “Screen position out of view frustum” error that keeps spamming my console when playing full screen in the editor. Why this one even exists is a mystery to me, since its only purpose seems to be to annoy everyone.