Clear Editor Console logs from script

The Debug.ClearDeveloperConsole() function is used when you clear logs from an application that was built while Debug Build is enabled in your project. There is no official API for clearing the Editor log.

Most Editor functionality can be replicated with Reflection just like hiding Gizmos and toggling the Stats Panel. I was going to write one but found this one.

This should clear every log on the Console tab.

using System.Reflection;

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

Now, you can call ClearLog(); in your else statements.

EDIT:

This has changed recently in about ~Unity 2017. Since it is done with reflection, I consider it to change again anytime if any class, variable or function used in this code is renamed by Unity. Below is the new way to do this:

public void ClearLog()
{
    var assembly = Assembly.GetAssembly(typeof(UnityEditor.Editor));
    var type = assembly.GetType("UnityEditor.LogEntries");
    var method = type.GetMethod("Clear");
    method.Invoke(new object(), null);
}

Tags:

C#

Unity3D